]> Cypherpunks repositories - gostls13.git/commitdiff
runtime/race: fix failing tests
authorAustin Clements <austin@google.com>
Mon, 27 Apr 2015 21:15:30 +0000 (17:15 -0400)
committerAustin Clements <austin@google.com>
Mon, 27 Apr 2015 23:12:00 +0000 (23:12 +0000)
Some race tests were sensitive to the goroutine scheduling order.
When this changed in commit e870f06, these tests started to fail.

Fix TestRaceHeapParam by ensuring that the racing goroutine has
run before the test exits. Fix TestRaceRWMutexMultipleReaders by
adding a third reader to ensure that two readers wind up on the
same side of the writer (and race with each other) regardless of
the schedule. Fix TestRaceRange by ensuring that the racing
goroutine runs before the main goroutine exits the loop it races
with.

Change-Id: Iaf002f8730ea42227feaf2f3c51b9a1e57ccffdd
Reviewed-on: https://go-review.googlesource.com/9402
Reviewed-by: Russ Cox <rsc@golang.org>
src/runtime/race/testdata/mop_test.go
src/runtime/race/testdata/rwmutex_test.go

index 095ead6c9ad81c6b898068b044a6e5d370a7ca92..7f95051a8c30d30d6f58fdcbd5352d13d49bb309 100644 (file)
@@ -335,6 +335,8 @@ func TestRaceRange(t *testing.T) {
                        }
                        done <- true
                }(i)
+               // Ensure the goroutine runs before we continue the loop.
+               runtime.Gosched()
        }
        for i := 0; i < N; i++ {
                <-done
@@ -1727,13 +1729,16 @@ func TestNoRaceAsFunc4(t *testing.T) {
 }
 
 func TestRaceHeapParam(t *testing.T) {
+       done := make(chan bool)
        x := func() (x int) {
                go func() {
                        x = 42
+                       done <- true
                }()
                return
        }()
        _ = x
+       <-done
 }
 
 func TestNoRaceEmptyStruct(t *testing.T) {
index 85cb5df3cb86cf4183706a5c232da522ee710d44..7ac829d759ff7ff532b8ee55717afd1b6b390a36 100644 (file)
@@ -54,13 +54,16 @@ func TestNoRaceRWMutex(t *testing.T) {
 func TestRaceRWMutexMultipleReaders(t *testing.T) {
        var mu sync.RWMutex
        var x, y int64 = 0, 1
-       ch := make(chan bool, 3)
+       ch := make(chan bool, 4)
        go func() {
                mu.Lock()
                defer mu.Unlock()
                x = 2
                ch <- true
        }()
+       // Use three readers so that no matter what order they're
+       // scheduled in, two will be on the same side of the write
+       // lock above.
        go func() {
                mu.RLock()
                y = x + 1
@@ -73,6 +76,13 @@ func TestRaceRWMutexMultipleReaders(t *testing.T) {
                mu.RUnlock()
                ch <- true
        }()
+       go func() {
+               mu.RLock()
+               y = x + 3
+               mu.RUnlock()
+               ch <- true
+       }()
+       <-ch
        <-ch
        <-ch
        <-ch
@@ -82,7 +92,7 @@ func TestRaceRWMutexMultipleReaders(t *testing.T) {
 func TestNoRaceRWMutexMultipleReaders(t *testing.T) {
        var mu sync.RWMutex
        x := int64(0)
-       ch := make(chan bool, 3)
+       ch := make(chan bool, 4)
        go func() {
                mu.Lock()
                defer mu.Unlock()
@@ -103,6 +113,14 @@ func TestNoRaceRWMutexMultipleReaders(t *testing.T) {
                mu.RUnlock()
                ch <- true
        }()
+       go func() {
+               mu.RLock()
+               y := x + 3
+               _ = y
+               mu.RUnlock()
+               ch <- true
+       }()
+       <-ch
        <-ch
        <-ch
        <-ch