]> Cypherpunks repositories - gostls13.git/commitdiff
sync: make TestPoolDequeue termination condition more robust
authorAustin Clements <austin@google.com>
Wed, 26 Jun 2019 17:20:05 +0000 (13:20 -0400)
committerAustin Clements <austin@google.com>
Wed, 26 Jun 2019 19:48:40 +0000 (19:48 +0000)
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>
src/sync/pool_test.go

index 090f1a8c6abc620dbb5d88ac4bdeb62427273bba..814c4a6812018abb288f3f52f2403bcc99ccddc1 100644 (file)
@@ -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))
                                }
                        }
                }