Commit b7f3c178 authored by Michael Munday's avatar Michael Munday Committed by Brad Fitzpatrick

sync: deflake TestWaitGroupMisuse2

We need to yield to the runtime every now and again to avoid
deadlock. This doesn't show up on most machines because the test
only runs when you have 5 or more CPUs.

Fixes #20072.

Change-Id: Ibf5ed370e919943395f3418487188df0b2be160b
Reviewed-on: https://go-review.googlesource.com/112978
Run-TryBot: Michael Munday <mike.munday@ibm.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 91f07c57
......@@ -68,6 +68,21 @@ func TestWaitGroupMisuse(t *testing.T) {
t.Fatal("Should panic")
}
// pollUntilEqual blocks until v, loaded atomically, is
// equal to the target.
func pollUntilEqual(v *uint32, target uint32) {
for {
for i := 0; i < 1e3; i++ {
if atomic.LoadUint32(v) == target {
return
}
}
// yield to avoid deadlock with the garbage collector
// see issue #20072
runtime.Gosched()
}
}
func TestWaitGroupMisuse2(t *testing.T) {
knownRacy(t)
if runtime.NumCPU() <= 4 {
......@@ -94,9 +109,7 @@ func TestWaitGroupMisuse2(t *testing.T) {
done <- recover()
}()
atomic.AddUint32(&here, 1)
for atomic.LoadUint32(&here) != 3 {
// spin
}
pollUntilEqual(&here, 3)
wg.Wait()
}()
go func() {
......@@ -104,16 +117,12 @@ func TestWaitGroupMisuse2(t *testing.T) {
done <- recover()
}()
atomic.AddUint32(&here, 1)
for atomic.LoadUint32(&here) != 3 {
// spin
}
pollUntilEqual(&here, 3)
wg.Add(1) // This is the bad guy.
wg.Done()
}()
atomic.AddUint32(&here, 1)
for atomic.LoadUint32(&here) != 3 {
// spin
}
pollUntilEqual(&here, 3)
wg.Done()
for j := 0; j < 2; j++ {
if err := <-done; err != nil {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment