TestReusableGoroutinesPool_Race tests that Close() and Go() can be called concurrently.
(t *testing.T)
| 61 | |
| 62 | // TestReusableGoroutinesPool_Race tests that Close() and Go() can be called concurrently. |
| 63 | func TestReusableGoroutinesPool_Race(t *testing.T) { |
| 64 | w := NewReusableGoroutinesPool(2) |
| 65 | |
| 66 | var runCountAtomic atomic.Int32 |
| 67 | const maxMsgCount = 10 |
| 68 | |
| 69 | var testWG sync.WaitGroup |
| 70 | testWG.Add(1) |
| 71 | go func() { |
| 72 | defer testWG.Done() |
| 73 | for i := 0; i < maxMsgCount; i++ { |
| 74 | w.Go(func() { |
| 75 | runCountAtomic.Add(1) |
| 76 | }) |
| 77 | time.Sleep(10 * time.Millisecond) |
| 78 | } |
| 79 | }() |
| 80 | time.Sleep(10 * time.Millisecond) |
| 81 | w.Close() // close the pool |
| 82 | testWG.Wait() // wait for the test to finish |
| 83 | |
| 84 | runCt := int(runCountAtomic.Load()) |
| 85 | require.Equal(t, runCt, 10, "expected all functions to run") |
| 86 | } |