]> Cypherpunks repositories - gostls13.git/commitdiff
netchan: Fix race condition in test.
authorIan Lance Taylor <iant@golang.org>
Thu, 26 Aug 2010 22:42:18 +0000 (15:42 -0700)
committerIan Lance Taylor <iant@golang.org>
Thu, 26 Aug 2010 22:42:18 +0000 (15:42 -0700)
Two tests start a goroutine which runs exportSend, and then
the tests run importReceive.  exportSend creates an export
channel.  importReceive asks to receive values on that
channel.  Because exportSend runs in a separate goroutine,
it's possible for the export client to receive the request for
values on the channel, from importReceive, before the
goroutine actually creates the export channel.  That causes an
error: "export: no such channel: exportedSend".  This patch
avoids the race by creating the export channel before starting
the goroutine.

There does not seem to be a similar race condition in the
tests which send data in the other direction.

R=r
CC=golang-dev
https://golang.org/cl/2026045

src/pkg/netchan/netchan_test.go

index 6b5c67c3caee8023e2151e3c2063390e5faaac01..eb5a11ea449e0a4cf8ae1f1b25527cbebcc35c9b 100644 (file)
@@ -15,10 +15,12 @@ func exportSend(exp *Exporter, n int, t *testing.T) {
        if err != nil {
                t.Fatal("exportSend:", err)
        }
-       for i := 0; i < n; i++ {
-               ch <- 23+i
-       }
-       close(ch)
+       go func() {
+               for i := 0; i < n; i++ {
+                       ch <- 23+i
+               }
+               close(ch)
+       }()
 }
 
 func exportReceive(exp *Exporter, t *testing.T) {
@@ -75,7 +77,7 @@ func TestExportSendImportReceive(t *testing.T) {
        if err != nil {
                t.Fatal("new importer:", err)
        }
-       go exportSend(exp, count, t)
+       exportSend(exp, count, t)
        importReceive(imp, t)
 }
 
@@ -101,6 +103,6 @@ func TestClosingExportSendImportReceive(t *testing.T) {
        if err != nil {
                t.Fatal("new importer:", err)
        }
-       go exportSend(exp, closeCount, t)
+       exportSend(exp, closeCount, t)
        importReceive(imp, t)
 }