mirror of
https://github.com/golang/go
synced 2024-11-17 11:04:53 -07:00
sync: make TestPoolDequeue termination condition more robust
TestPoolDequeue creates P-1 consumer goroutines and 1 producer goroutine. Currently, if a consumer goroutine pops the last value from the dequeue, it sets a flag that stops all consumers, but the producer also periodically pops from the dequeue and doesn't set this flag. Hence, if the producer were to pop the last element, the consumers will continue to run and the test won't terminate. This CL fixes this by also setting the termination flag in the producer. I believe it's impossible for this to happen right now because the producer only pops after pushing an element for which j%10==0 and the last element is either 999 or 1999999, which means it should never try to pop after pushing the last element. However, we shouldn't depend on this reasoning. Change-Id: Icd2bc8d7cb9a79ebfcec99e367c8a2ba76e027d8 Reviewed-on: https://go-review.googlesource.com/c/go/+/183980 Run-TryBot: Austin Clements <austin@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
This commit is contained in:
parent
93c05627f9
commit
c19b3a60da
@ -180,6 +180,12 @@ func testPoolDequeue(t *testing.T, d PoolDequeue) {
|
||||
have := make([]int32, N)
|
||||
var stop int32
|
||||
var wg WaitGroup
|
||||
record := func(val int) {
|
||||
atomic.AddInt32(&have[val], 1)
|
||||
if val == N-1 {
|
||||
atomic.StoreInt32(&stop, 1)
|
||||
}
|
||||
}
|
||||
|
||||
// Start P-1 consumers.
|
||||
for i := 1; i < P; i++ {
|
||||
@ -190,10 +196,7 @@ func testPoolDequeue(t *testing.T, d PoolDequeue) {
|
||||
val, ok := d.PopTail()
|
||||
if ok {
|
||||
fail = 0
|
||||
atomic.AddInt32(&have[val.(int)], 1)
|
||||
if val.(int) == N-1 {
|
||||
atomic.StoreInt32(&stop, 1)
|
||||
}
|
||||
record(val.(int))
|
||||
} else {
|
||||
// Speed up the test by
|
||||
// allowing the pusher to run.
|
||||
@ -219,7 +222,7 @@ func testPoolDequeue(t *testing.T, d PoolDequeue) {
|
||||
val, ok := d.PopHead()
|
||||
if ok {
|
||||
nPopHead++
|
||||
atomic.AddInt32(&have[val.(int)], 1)
|
||||
record(val.(int))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user