]> Cypherpunks repositories - gostls13.git/commitdiff
runtime/race: add tests for channels
authordvyukov <dvyukov@google.com>
Wed, 28 Oct 2015 09:49:17 +0000 (17:49 +0800)
committerRuss Cox <rsc@golang.org>
Tue, 24 Nov 2015 17:00:43 +0000 (17:00 +0000)
These tests were failing on one of the versions of cl/9345
("runtime: simplify buffered channels").

Change-Id: I920ffcd28de428bcb7c2d5a300068644260e1017
Reviewed-on: https://go-review.googlesource.com/16416
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Dmitry Vyukov <dvyukov@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/runtime/race/testdata/chan_test.go

index eabd81f40ce4a4b63e17b099829b78b3a4eb335d..cddd9a6e782f2a2e5d0aded50f9032225baf7eef 100644 (file)
@@ -657,3 +657,42 @@ func TestNoRaceChanWaitGroup(t *testing.T) {
                _ = data[i]
        }
 }
+
+// Test that sender synchronizes with receiver even if the sender was blocked.
+func TestNoRaceBlockedSendSync(t *testing.T) {
+       c := make(chan *int, 1)
+       c <- nil
+       go func() {
+               i := 42
+               c <- &i
+       }()
+       // Give the sender time to actually block.
+       // This sleep is completely optional: race report must not be printed
+       // regardless of whether the sender actually blocks or not.
+       // It cannot lead to flakiness.
+       time.Sleep(10 * time.Millisecond)
+       <-c
+       p := <-c
+       if *p != 42 {
+               t.Fatal()
+       }
+}
+
+// The same as TestNoRaceBlockedSendSync above, but sender unblock happens in a select.
+func TestNoRaceBlockedSelectSendSync(t *testing.T) {
+       c := make(chan *int, 1)
+       c <- nil
+       go func() {
+               i := 42
+               c <- &i
+       }()
+       time.Sleep(10 * time.Millisecond)
+       <-c
+       select {
+       case p := <-c:
+               if *p != 42 {
+                       t.Fatal()
+               }
+       case <-make(chan int):
+       }
+}