Go语言并发之道学习十 心跳单元测试

心跳单元测试

package main

import (
	"time"
	"testing"
)

func DoWork(
	done <-chan interface{},
	pulseInterval time.Duration,
	nums ...int,
	)(<-chan interface{},<-chan int){
	heartbeat := make(chan interface{},1)
	intStream := make(chan int)

	go func() {
		defer close(heartbeat)
		defer close(intStream)

		time.Sleep(2*time.Second)

		pulse := time.Tick(pulseInterval)
		numLoop:
			for _,n := range nums {
				for {
					select {
					case <- done:
						return
					case <-pulse:
						select {
						case heartbeat<- struct {}{}:
						default:
						}
					case intStream <- n:
						continue numLoop
					}
				}
			}
	}()
	return heartbeat,intStream
}

func TestDoWork_GeneratesAllNumbers(t *testing.T){
	done := make(chan interface{})
	defer close(done)

	intSlice := []int{0,1,2,3,5}
	const timeout = 2*time.Second
	heartbeat,results := DoWork(done,timeout/2,intSlice...)

	<-heartbeat
	i:=0
	for {
		select {
		case r,ok := <-results:
			if ok == false{
				return
			}else if expected := intSlice[i];r != expected{
				t.Errorf(
					"index %v: expected %v,but received %v,",
					i,
					expected,
					r,
					)
			}
			i++
		case <-heartbeat:
		case <-time.After(timeout):
			t.Fatal("test timed out")
		}
	}
	/*
	·numLoop:
	使用一个标签来标识内部循环

	·for _,n := range nums {
	  for {
	我们需要两个循环,一个外部循环遍历数列,另一个内部循环持续执行,直到intStream 中的数字成功发送。

	·continue numLoop
	跳回numLoop 标签继续执行外部循环

	·<-heartbeat
	等待第一次心跳到达,来确认goroutine 已经进入了循环。

	·case <-heartbeat:
	接收心跳,以防止超时。

	*/
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值