I've been searching around, but so far only gone similar article written here by Ariejan de Vroom.
I would like to know if I can bring goroutine into unit testing such that it can precisely count the concurrent # of goroutines is running and can tell me if they are correctly spawned goroutine in the number I have stated.
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
}