]> Cypherpunks repositories - gostls13.git/commitdiff
runtime/race: make test more robust
authorDmitry Vyukov <dvyukov@google.com>
Thu, 7 May 2015 09:33:45 +0000 (12:33 +0300)
committerDmitry Vyukov <dvyukov@google.com>
Wed, 24 Jun 2015 17:53:25 +0000 (17:53 +0000)
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/9803
Reviewed-by: Austin Clements <austin@google.com>
src/runtime/race/testdata/sync_test.go

index 93af0b1e600f6ee967e8ba227f48f193ab8b697d..d48680d5e6a34841e80e9831ee4d76347f1213c5 100644 (file)
@@ -10,72 +10,52 @@ import (
        "time"
 )
 
-func TestNoRaceCond(t *testing.T) { // tsan's test02
-       ch := make(chan bool, 1)
-       var x int = 0
+func TestNoRaceCond(t *testing.T) {
+       x := 0
+       condition := 0
        var mu sync.Mutex
-       var cond *sync.Cond = sync.NewCond(&mu)
-       var condition int = 0
-       var waker func()
-       waker = func() {
+       cond := sync.NewCond(&mu)
+       go func() {
                x = 1
                mu.Lock()
                condition = 1
                cond.Signal()
                mu.Unlock()
+       }()
+       mu.Lock()
+       for condition != 1 {
+               cond.Wait()
        }
-
-       var waiter func()
-       waiter = func() {
-               go waker()
-               cond.L.Lock()
-               for condition != 1 {
-                       cond.Wait()
-               }
-               cond.L.Unlock()
-               x = 2
-               ch <- true
-       }
-       go waiter()
-       <-ch
+       mu.Unlock()
+       x = 2
 }
 
-func TestRaceCond(t *testing.T) { // tsan's test50
-       ch := make(chan bool, 2)
-
-       var x int = 0
+func TestRaceCond(t *testing.T) {
+       done := make(chan bool)
        var mu sync.Mutex
-       var condition int = 0
-       var cond *sync.Cond = sync.NewCond(&mu)
-
-       var waker func() = func() {
-               <-time.After(1e5)
+       cond := sync.NewCond(&mu)
+       x := 0
+       condition := 0
+       go func() {
+               time.Sleep(10 * time.Millisecond) // Enter cond.Wait loop
                x = 1
                mu.Lock()
                condition = 1
                cond.Signal()
                mu.Unlock()
-               <-time.After(1e5)
+               time.Sleep(10 * time.Millisecond) // Exit cond.Wait loop
                mu.Lock()
                x = 3
                mu.Unlock()
-               ch <- true
-       }
-
-       var waiter func() = func() {
-               mu.Lock()
-               for condition != 1 {
-                       cond.Wait()
-               }
-               mu.Unlock()
-               x = 2
-               ch <- true
+               done <- true
+       }()
+       mu.Lock()
+       for condition != 1 {
+               cond.Wait()
        }
-       x = 0
-       go waker()
-       go waiter()
-       <-ch
-       <-ch
+       mu.Unlock()
+       x = 2
+       <-done
 }
 
 // We do not currently automatically