MCPcopy
hub / github.com/grafana/dskit / TestReusableGoroutinesPool_Race

Function TestReusableGoroutinesPool_Race

concurrency/worker_test.go:63–86  ·  view source on GitHub ↗

TestReusableGoroutinesPool_Race tests that Close() and Go() can be called concurrently.

(t *testing.T)

Source from the content-addressed store, hash-verified

61
62// TestReusableGoroutinesPool_Race tests that Close() and Go() can be called concurrently.
63func 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}

Callers

nothing calls this directly

Calls 8

GoMethod · 0.95
CloseMethod · 0.95
AddMethod · 0.65
DoneMethod · 0.65
SleepMethod · 0.65
WaitMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected