xbe*_*eta 6 tdd unit-testing go goroutine
我一直在寻找周围,但到目前为止只进行了类似的文章写到这里通过Ariejan德弗鲁姆.
我想知道我是否可以将goroutine引入单元测试,以便它可以精确计算正在运行的goroutine的并发数量,并且可以告诉我它们是否正确地产生了我所说的数字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
}
Run Code Online (Sandbox Code Playgroud)
一种可能的方法是使用runtime.Stack()或分析 的输出,runtime.debug.PrintStack()以便查看给定时间的所有 goroutine。
这些选项在“如何转储 goroutine 堆栈跟踪? ”中详细介绍。