I searched around, but so far only gone a similar article written by Ariejan de Vroom.
I would like to know if I can bring goroutine into unit testing so that it can accurately count the number of concurrent # goroutines and can tell me if they spawned goroutine correctly in the number I specified.
I have the following code, for example.
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 }
source share