Skip to content

Commit

Permalink
pkg/transport: fix a data race in TestReadWriteTimeoutDialer
Browse files Browse the repository at this point in the history
Accessing test.T async will cause data race.

Change to use select to coordinate the access of test.T.
  • Loading branch information
yichengq committed Oct 1, 2015
1 parent 533e728 commit de1a16e
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions pkg/transport/timeout_dialer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,40 @@ func TestReadWriteTimeoutDialer(t *testing.T) {

// fill the socket buffer
data := make([]byte, 5*1024*1024)
timer := time.AfterFunc(d.wtimeoutd*5, func() {
done := make(chan struct{})
go func() {
_, err = conn.Write(data)
done <- struct{}{}
}()

select {
case <-done:
case <-time.After(d.wtimeoutd * 5):
t.Fatal("wait timeout")
})
defer timer.Stop()
}

_, err = conn.Write(data)
if operr, ok := err.(*net.OpError); !ok || operr.Op != "write" || !operr.Timeout() {
t.Errorf("err = %v, want write i/o timeout error", err)
}

timer.Reset(d.rdtimeoutd * 5)

conn, err = d.Dial("tcp", ln.Addr().String())
if err != nil {
t.Fatalf("unexpected dial error: %v", err)
}
defer conn.Close()

buf := make([]byte, 10)
_, err = conn.Read(buf)
go func() {
_, err = conn.Read(buf)
done <- struct{}{}
}()

select {
case <-done:
case <-time.After(d.rdtimeoutd * 5):
t.Fatal("wait timeout")
}

if operr, ok := err.(*net.OpError); !ok || operr.Op != "read" || !operr.Timeout() {
t.Errorf("err = %v, want write i/o timeout error", err)
}
Expand Down

0 comments on commit de1a16e

Please sign in to comment.