单元测试 – 测试Golang Goroutine
发布时间:2020-12-16 09:28:56 所属栏目:大数据 来源:网络整理
导读:我一直在寻找,但到目前为止只有Ariejan de Vroom写的类似文章 here. 我想知道我是否可以将goroutine引入单元测试,以便它可以精确计算正在运行的goroutine的并发数量,并且可以告诉我它们是否正确地产生了我所说的数字goroutine. 我有以下代码例如.. import (
我一直在寻找,但到目前为止只有Ariejan de Vroom写的类似文章
here.
我想知道我是否可以将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 } 解决方法
一个可能的方法是使用
runtime.Stack() 或分析
runtime.debug.PrintStack() 的输出,以便在给定时间查看所有goroutine.
这些选项在“How to dump goroutine stacktraces?”中有详细说明. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |