netchan: Fix race condition in test.

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
This commit is contained in:
Ian Lance Taylor 2010-08-26 15:42:18 -07:00
parent 3b226f9886
commit b279c048e3

View 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)
}