Skip to content

Commit

Permalink
prevent a data race caused by unprotected access to testing.*.Log
Browse files Browse the repository at this point in the history
The following commit

golang/go@5c83e65

which was released in go 1.7, fixes a possible data race that can happen
during testing.

The fix is in common.flushToParent which is now used to protect read
access to `output`.
  • Loading branch information
imkira committed Nov 29, 2016
1 parent 636041e commit ff6f6b5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func newSession(t *testing.T) (io.ReadWriteCloser, *server) {
rs, wc := io.Pipe()
rc, ws := io.Pipe()

rws := &logIO{t, "server", &pipe{r: rs, w: ws}}
rwc := &logIO{t, "client", &pipe{r: rc, w: wc}}
rws := &logIO{t: t, prefix: "server", proxy: &pipe{r: rs, w: ws}}
rwc := &logIO{t: t, prefix: "client", proxy: &pipe{r: rc, w: wc}}

server := server{
T: t,
Expand Down
2 changes: 1 addition & 1 deletion integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1708,7 +1708,7 @@ func integrationURLFromEnv() string {

func loggedConnection(t *testing.T, conn *Connection, name string) *Connection {
if name != "" {
conn.conn = &logIO{t, name, conn.conn}
conn.conn = &logIO{t: t, prefix: name, proxy: conn.conn}
}
return conn
}
Expand Down
10 changes: 9 additions & 1 deletion shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ func (p *pipe) Close() error {
type logIO struct {
t *testing.T
prefix string
proxy io.ReadWriteCloser

m sync.Mutex
proxy io.ReadWriteCloser
}

func (me *logIO) Read(p []byte) (n int, err error) {
me.m.Lock()
defer me.m.Unlock()
me.t.Logf("%s reading %d\n", me.prefix, len(p))
n, err = me.proxy.Read(p)
if err != nil {
Expand All @@ -64,6 +68,8 @@ func (me *logIO) Read(p []byte) (n int, err error) {
}

func (me *logIO) Write(p []byte) (n int, err error) {
me.m.Lock()
defer me.m.Unlock()
me.t.Logf("%s writing %d\n", me.prefix, len(p))
n, err = me.proxy.Write(p)
if err != nil {
Expand All @@ -76,6 +82,8 @@ func (me *logIO) Write(p []byte) (n int, err error) {
}

func (me *logIO) Close() (err error) {
me.m.Lock()
defer me.m.Unlock()
err = me.proxy.Close()
if err != nil {
me.t.Logf("%s close : %v\n", me.prefix, err)
Expand Down

0 comments on commit ff6f6b5

Please sign in to comment.