Skip to content

Commit b5cbb75

Browse files
committed
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 rclone#8813
1 parent a170dfa commit b5cbb75

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

lib/pool/pool_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,22 +288,24 @@ func TestPoolMaxBufferMemory(t *testing.T) {
288288
}
289289
}
290290
)
291-
for i := 0; i < 20; i++ {
291+
const trials = 50
292+
for i := range trials {
292293
wg.Add(1)
293294
go func() {
294295
defer wg.Done()
295-
if i < 4 {
296-
buf := bp.GetN(i + 1)
297-
countBuf(i + 1)
298-
time.Sleep(100 * time.Millisecond)
296+
if i < trials/2 {
297+
n := i%4 + 1
298+
buf := bp.GetN(n)
299+
countBuf(n)
300+
time.Sleep(1 * time.Millisecond)
301+
countBuf(-n)
299302
bp.PutN(buf)
300-
countBuf(-(i + 1))
301303
} else {
302304
buf := bp.Get()
303305
countBuf(1)
304-
time.Sleep(100 * time.Millisecond)
305-
bp.Put(buf)
306+
time.Sleep(1 * time.Millisecond)
306307
countBuf(-1)
308+
bp.Put(buf)
307309
}
308310
}()
309311
}

0 commit comments

Comments
 (0)