Commit 77082481 authored by Dmitry Vyukov's avatar Dmitry Vyukov

runtime/race: make test more robust

The test is flaky on builders lately. I don't see any issues other than
usage of very small sleeps. So increase the sleeps. Also take opportunity
to refactor the code.
On my machine this change significantly reduces failure rate with GOMAXPROCS=2.
I can't reproduce the failure with GOMAXPROCS=1.

Fixes #10726

Change-Id: Iea6f10cf3ce1be5c112a2375d51c13687a8ab4c9
Reviewed-on: https://go-review.googlesource.com/9803Reviewed-by: default avatarAustin Clements <austin@google.com>
parent 703166ea
...@@ -10,72 +10,52 @@ import ( ...@@ -10,72 +10,52 @@ import (
"time" "time"
) )
func TestNoRaceCond(t *testing.T) { // tsan's test02 func TestNoRaceCond(t *testing.T) {
ch := make(chan bool, 1) x := 0
var x int = 0 condition := 0
var mu sync.Mutex var mu sync.Mutex
var cond *sync.Cond = sync.NewCond(&mu) cond := sync.NewCond(&mu)
var condition int = 0 go func() {
var waker func()
waker = func() {
x = 1 x = 1
mu.Lock() mu.Lock()
condition = 1 condition = 1
cond.Signal() cond.Signal()
mu.Unlock() mu.Unlock()
}()
mu.Lock()
for condition != 1 {
cond.Wait()
} }
mu.Unlock()
var waiter func() x = 2
waiter = func() {
go waker()
cond.L.Lock()
for condition != 1 {
cond.Wait()
}
cond.L.Unlock()
x = 2
ch <- true
}
go waiter()
<-ch
} }
func TestRaceCond(t *testing.T) { // tsan's test50 func TestRaceCond(t *testing.T) {
ch := make(chan bool, 2) done := make(chan bool)
var x int = 0
var mu sync.Mutex var mu sync.Mutex
var condition int = 0 cond := sync.NewCond(&mu)
var cond *sync.Cond = sync.NewCond(&mu) x := 0
condition := 0
var waker func() = func() { go func() {
<-time.After(1e5) time.Sleep(10 * time.Millisecond) // Enter cond.Wait loop
x = 1 x = 1
mu.Lock() mu.Lock()
condition = 1 condition = 1
cond.Signal() cond.Signal()
mu.Unlock() mu.Unlock()
<-time.After(1e5) time.Sleep(10 * time.Millisecond) // Exit cond.Wait loop
mu.Lock() mu.Lock()
x = 3 x = 3
mu.Unlock() mu.Unlock()
ch <- true done <- true
} }()
mu.Lock()
var waiter func() = func() { for condition != 1 {
mu.Lock() cond.Wait()
for condition != 1 {
cond.Wait()
}
mu.Unlock()
x = 2
ch <- true
} }
x = 0 mu.Unlock()
go waker() x = 2
go waiter() <-done
<-ch
<-ch
} }
// We do not currently automatically // We do not currently automatically
......
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