Skip to content

Commit 83f95b8

Browse files
committed
sync: deflake TestWaitGroupMisuse2
Also runs 100X faster on average, because it takes so many fewer attempts to trigger the failure. Fixes #11443. Change-Id: I8c39ee48bb3ff6c36fa63083e04076771b65a80d Reviewed-on: https://go-review.googlesource.com/36841 Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Dmitry Vyukov <[email protected]>
1 parent 863035e commit 83f95b8

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/sync/waitgroup_test.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,8 @@ func TestWaitGroupMisuse(t *testing.T) {
7070

7171
func TestWaitGroupMisuse2(t *testing.T) {
7272
knownRacy(t)
73-
if testing.Short() {
74-
t.Skip("skipping flaky test in short mode; see issue 11443")
75-
}
76-
if runtime.NumCPU() <= 2 {
77-
t.Skip("NumCPU<=2, skipping: this test requires parallelism")
73+
if runtime.NumCPU() <= 4 {
74+
t.Skip("NumCPU<=4, skipping: this test requires parallelism")
7875
}
7976
defer func() {
8077
err := recover()
@@ -86,24 +83,37 @@ func TestWaitGroupMisuse2(t *testing.T) {
8683
}()
8784
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(4))
8885
done := make(chan interface{}, 2)
89-
// The detection is opportunistically, so we want it to panic
86+
// The detection is opportunistic, so we want it to panic
9087
// at least in one run out of a million.
9188
for i := 0; i < 1e6; i++ {
9289
var wg WaitGroup
90+
var here uint32
9391
wg.Add(1)
9492
go func() {
9593
defer func() {
9694
done <- recover()
9795
}()
96+
atomic.AddUint32(&here, 1)
97+
for atomic.LoadUint32(&here) != 3 {
98+
// spin
99+
}
98100
wg.Wait()
99101
}()
100102
go func() {
101103
defer func() {
102104
done <- recover()
103105
}()
106+
atomic.AddUint32(&here, 1)
107+
for atomic.LoadUint32(&here) != 3 {
108+
// spin
109+
}
104110
wg.Add(1) // This is the bad guy.
105111
wg.Done()
106112
}()
113+
atomic.AddUint32(&here, 1)
114+
for atomic.LoadUint32(&here) != 3 {
115+
// spin
116+
}
107117
wg.Done()
108118
for j := 0; j < 2; j++ {
109119
if err := <-done; err != nil {

0 commit comments

Comments
 (0)