Skip to content

Commit aaed6cb

Browse files
dfavaBryan C. Mills
authored andcommitted
testing/race: fixing intermittent test failure
Test NoRaceMutexPureHappensBefore in runtime/race/testdata/mutex_test.go expects the second spawned goroutine to run after the first. The test attempts to force this scheduling with a 10 millisecond wait. Following a suggestion by Bryan Mills, we force this scheduling using a shared variable whose access take place within the existing mutex. Fixes golang#35745. Change-Id: Ib23ec51492ecfeed4752e020401dd25755a669ed Reviewed-on: https://go-review.googlesource.com/c/go/+/291292 Reviewed-by: Bryan C. Mills <bcmills@google.com> Reviewed-by: Dmitry Vyukov <dvyukov@google.com> Trust: Bryan C. Mills <bcmills@google.com> Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Go Bot <gobot@golang.org>
1 parent fbed561 commit aaed6cb

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/runtime/race/testdata/mutex_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,23 @@ func TestNoRaceMutexPureHappensBefore(t *testing.T) {
7878
var mu sync.Mutex
7979
var x int16 = 0
8080
_ = x
81+
written := false
8182
ch := make(chan bool, 2)
8283
go func() {
8384
x = 1
8485
mu.Lock()
86+
written = true
8587
mu.Unlock()
8688
ch <- true
8789
}()
8890
go func() {
89-
<-time.After(1e5)
91+
time.Sleep(100 * time.Microsecond)
9092
mu.Lock()
93+
for !written {
94+
mu.Unlock()
95+
time.Sleep(100 * time.Microsecond)
96+
mu.Lock()
97+
}
9198
mu.Unlock()
9299
x = 1
93100
ch <- true

0 commit comments

Comments
 (0)