title: Test Golang Goroutine
body: <p>I've been searching around, but so far only gone similar article written <a href="https://ariejan.net/2014/08/29/synchronize-goroutines-in-your-tests/" rel="noreferrer">here</a> by Ariejan de Vroom.</p>
  
  <p>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.</p>
  
  <p>I have the following code for example..</p>
  
  <pre><code>import (
      "testing"
      "github.com/stretchr/testify/assert"
  )
  
  func createList(job int, done chan bool) {
      time.Sleep(500)
      // do something
      time.Sleep(500)
      done &lt;- 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 &lt;= numGoRoutines; j++ {
          jobs &lt;- j
          go createList(j, done)
          fmt.Println("sent job", j)
      }
      close(jobs)
      fmt.Println("sent all jobs")
      &lt;-done
  }
  </code></pre>