mirror of https://github.com/golang/go.git
[release-branch.go1.16] 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 #50832.
Updates #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>
(cherry picked from commit aaed6cbced)
Reviewed-on: https://go-review.googlesource.com/c/go/+/381034
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
This commit is contained in:
parent
57d5d8f987
commit
6cbcf581af
|
|
@ -78,16 +78,23 @@ func TestNoRaceMutexPureHappensBefore(t *testing.T) {
|
||||||
var mu sync.Mutex
|
var mu sync.Mutex
|
||||||
var x int16 = 0
|
var x int16 = 0
|
||||||
_ = x
|
_ = x
|
||||||
|
written := false
|
||||||
ch := make(chan bool, 2)
|
ch := make(chan bool, 2)
|
||||||
go func() {
|
go func() {
|
||||||
x = 1
|
x = 1
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
|
written = true
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
ch <- true
|
ch <- true
|
||||||
}()
|
}()
|
||||||
go func() {
|
go func() {
|
||||||
<-time.After(1e5)
|
time.Sleep(100 * time.Microsecond)
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
|
for !written {
|
||||||
|
mu.Unlock()
|
||||||
|
time.Sleep(100 * time.Microsecond)
|
||||||
|
mu.Lock()
|
||||||
|
}
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
x = 1
|
x = 1
|
||||||
ch <- true
|
ch <- true
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue