From b5cbb7520df9c2dbe5314a1ede721886c2986a38 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Thu, 11 Sep 2025 18:33:06 +0100 Subject: [PATCH] lib/pool: fix unreliable TestPoolMaxBufferMemory test This turned out to be a problem in the tests. The tests used to do 1. allocate 2. increment 3. free 4. decrement But if one goroutine had just completed 2 and another had just completed 3 then this can cause the test to register too many allocations. This was fixed by doing the test in this order instead: 1. allocate 2. increment 3. decrement 4. free The 4 operations are atomic. Fixes #8813 --- lib/pool/pool_test.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/pool/pool_test.go b/lib/pool/pool_test.go index 5fa05da7c..5c0434e82 100644 --- a/lib/pool/pool_test.go +++ b/lib/pool/pool_test.go @@ -288,22 +288,24 @@ func TestPoolMaxBufferMemory(t *testing.T) { } } ) - for i := 0; i < 20; i++ { + const trials = 50 + for i := range trials { wg.Add(1) go func() { defer wg.Done() - if i < 4 { - buf := bp.GetN(i + 1) - countBuf(i + 1) - time.Sleep(100 * time.Millisecond) + if i < trials/2 { + n := i%4 + 1 + buf := bp.GetN(n) + countBuf(n) + time.Sleep(1 * time.Millisecond) + countBuf(-n) bp.PutN(buf) - countBuf(-(i + 1)) } else { buf := bp.Get() countBuf(1) - time.Sleep(100 * time.Millisecond) - bp.Put(buf) + time.Sleep(1 * time.Millisecond) countBuf(-1) + bp.Put(buf) } }() }