Skip to content

Commit

Permalink
Get rid of baseGoroutines. It's now zero
Browse files Browse the repository at this point in the history
  • Loading branch information
julienduchesne committed Oct 4, 2024
1 parent 12bb935 commit a982594
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions concurrency/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,38 @@ func TestReusableGoroutinesPool(t *testing.T) {
return strings.Count(string(buf), " in goroutine "+string(testGoroutine))
}

baseGoroutines := countGoroutines()
const workers = 2
w := NewReusableGoroutinesPool(workers)

require.Equal(t, baseGoroutines+workers, countGoroutines())
const workerCount = 2
w := NewReusableGoroutinesPool(workerCount)
require.Equal(t, workerCount, countGoroutines())

// Wait a little bit so both goroutines would be waiting on the jobs chan.
time.Sleep(10 * time.Millisecond)

ch := make(chan struct{})
w.Go(func() { <-ch })
require.Equal(t, baseGoroutines+workers, countGoroutines())
require.Equal(t, workerCount, countGoroutines())
w.Go(func() { <-ch })
require.Equal(t, baseGoroutines+workers, countGoroutines())
require.Equal(t, workerCount, countGoroutines())
w.Go(func() { <-ch })
require.Equal(t, baseGoroutines+workers+1, countGoroutines())
require.Equal(t, workerCount+1, countGoroutines())

// end workloads, we should have only the workers again.
close(ch)
for i := 0; i < 1000; i++ {
if countGoroutines() == baseGoroutines+workers {
if countGoroutines() == workerCount {
break
}
time.Sleep(time.Millisecond)
}
require.Equal(t, baseGoroutines+workers, countGoroutines())
require.Equal(t, workerCount, countGoroutines())

// close the workers, eventually they should be gone.
w.Close()
for i := 0; i < 1000; i++ {
if countGoroutines() == baseGoroutines {
if countGoroutines() == 0 {
return
}
time.Sleep(time.Millisecond)
}
t.Fatalf("expected %d goroutines after closing, got %d", baseGoroutines, countGoroutines())
t.Fatalf("expected %d goroutines after closing, got %d", 0, countGoroutines())
}

0 comments on commit a982594

Please sign in to comment.