mirror of
https://github.com/rclone/rclone.git
synced 2025-12-11 13:53:15 +00:00
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
This commit is contained in:
@@ -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)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
if i < 4 {
|
if i < trials/2 {
|
||||||
buf := bp.GetN(i + 1)
|
n := i%4 + 1
|
||||||
countBuf(i + 1)
|
buf := bp.GetN(n)
|
||||||
time.Sleep(100 * time.Millisecond)
|
countBuf(n)
|
||||||
|
time.Sleep(1 * time.Millisecond)
|
||||||
|
countBuf(-n)
|
||||||
bp.PutN(buf)
|
bp.PutN(buf)
|
||||||
countBuf(-(i + 1))
|
|
||||||
} else {
|
} else {
|
||||||
buf := bp.Get()
|
buf := bp.Get()
|
||||||
countBuf(1)
|
countBuf(1)
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(1 * time.Millisecond)
|
||||||
bp.Put(buf)
|
|
||||||
countBuf(-1)
|
countBuf(-1)
|
||||||
|
bp.Put(buf)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user