【发布时间】:2015-05-06 06:37:27
【问题描述】:
我一直在四处寻找,但到目前为止只有 Ariejan de Vroom here 写的类似文章。
我想知道我是否可以将 goroutine 带入单元测试中,这样它就可以精确地计算并发运行的 goroutines 的数量,并可以告诉我它们是否是正确生成的 goroutine 在我所说的数量中。
例如,我有以下代码..
import (
"testing"
"github.com/stretchr/testify/assert"
)
func createList(job int, done chan bool) {
time.Sleep(500)
// do something
time.Sleep(500)
done <- true
return
}
func TestNewList(t *testing.T) {
list := NewList()
if assert.NotNil(t, list) {
const numGoRoutines = 16
jobs := make(chan int, numGoRoutines)
done := make(chan bool, 1)
for j := 1; j <= numGoRoutines; j++ {
jobs <- j
go createList(j, done)
fmt.Println("sent job", j)
}
close(jobs)
fmt.Println("sent all jobs")
<-done
}
【问题讨论】:
-
您到底想验证什么?你正在启动 16 个 goroutine?我没有完全关注您要解决的问题。
-
为什么要将 int 发送到工作频道?那里好像有 2 个设计。
-
链接失效
标签: unit-testing go tdd goroutine