(t *testing.T)
| 15 | ) |
| 16 | |
| 17 | func TestWaitPool(t *testing.T) { |
| 18 | t.Skip("Currently disabled") |
| 19 | var wg sync.WaitGroup |
| 20 | var trials atomic.Int32 |
| 21 | startTrials := int32(100000) |
| 22 | if raceEnabled { |
| 23 | // This test can be very slow with -race. |
| 24 | startTrials /= 10 |
| 25 | } |
| 26 | trials.Store(startTrials) |
| 27 | workers := runtime.NumCPU() + 2 |
| 28 | if workers-4 <= 0 { |
| 29 | t.Skip("Not enough cores") |
| 30 | } |
| 31 | p := NewWaitPool(uint32(workers-4), func() any { return make([]byte, 16) }) |
| 32 | wg.Add(workers) |
| 33 | var max atomic.Uint32 |
| 34 | updateMax := func() { |
| 35 | p.lock.Lock() |
| 36 | count := p.count |
| 37 | p.lock.Unlock() |
| 38 | if count > p.max { |
| 39 | t.Errorf("count (%d) > max (%d)", count, p.max) |
| 40 | } |
| 41 | for { |
| 42 | old := max.Load() |
| 43 | if count <= old { |
| 44 | break |
| 45 | } |
| 46 | if max.CompareAndSwap(old, count) { |
| 47 | break |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | for i := 0; i < workers; i++ { |
| 52 | go func() { |
| 53 | defer wg.Done() |
| 54 | for trials.Add(-1) > 0 { |
| 55 | updateMax() |
| 56 | x := p.Get() |
| 57 | updateMax() |
| 58 | time.Sleep(time.Duration(rand.Intn(100)) * time.Microsecond) |
| 59 | updateMax() |
| 60 | p.Put(x) |
| 61 | updateMax() |
| 62 | } |
| 63 | }() |
| 64 | } |
| 65 | wg.Wait() |
| 66 | if max.Load() != p.max { |
| 67 | t.Errorf("Actual maximum count (%d) != ideal maximum count (%d)", max, p.max) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func BenchmarkWaitPool(b *testing.B) { |
| 72 | var wg sync.WaitGroup |
nothing calls this directly
no test coverage detected