From c19b3a60da360a4e02b18dcb3430ec3485a0a831 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Wed, 26 Jun 2019 13:20:05 -0400 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Bryan C. Mills --- src/sync/pool_test.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/sync/pool_test.go b/src/sync/pool_test.go index 090f1a8c6ab..814c4a68120 100644 --- a/src/sync/pool_test.go +++ b/src/sync/pool_test.go @@ -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)) } } }