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 93127f4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 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
26 changes: 17 additions & 9 deletions shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,32 @@ func (p *pipe) Close() error {
}

type logIO struct {
t *testing.T
m sync.Mutex
t *testing.T

prefix string
proxy io.ReadWriteCloser
}

func (me *logIO) Read(p []byte) (n int, err error) {
me.t.Logf("%s reading %d\n", me.prefix, len(p))
me.logf("%s reading %d\n", me.prefix, len(p))
n, err = me.proxy.Read(p)
if err != nil {
me.t.Logf("%s read %x: %v\n", me.prefix, p[0:n], err)
me.logf("%s read %x: %v\n", me.prefix, p[0:n], err)
} else {
me.t.Logf("%s read:\n%s\n", me.prefix, hex.Dump(p[0:n]))
me.logf("%s read:\n%s\n", me.prefix, hex.Dump(p[0:n]))
//fmt.Printf("%s read:\n%s\n", me.prefix, hex.Dump(p[0:n]))
}
return
}

func (me *logIO) Write(p []byte) (n int, err error) {
me.t.Logf("%s writing %d\n", me.prefix, len(p))
me.logf("%s writing %d\n", me.prefix, len(p))
n, err = me.proxy.Write(p)
if err != nil {
me.t.Logf("%s write %d, %x: %v\n", me.prefix, len(p), p[0:n], err)
me.logf("%s write %d, %x: %v\n", me.prefix, len(p), p[0:n], err)
} else {
me.t.Logf("%s write %d:\n%s", me.prefix, len(p), hex.Dump(p[0:n]))
me.logf("%s write %d:\n%s", me.prefix, len(p), hex.Dump(p[0:n]))
//fmt.Printf("%s write %d:\n%s", me.prefix, len(p), hex.Dump(p[0:n]))
}
return
Expand All @@ -78,9 +80,15 @@ func (me *logIO) Write(p []byte) (n int, err error) {
func (me *logIO) Close() (err error) {
err = me.proxy.Close()
if err != nil {
me.t.Logf("%s close : %v\n", me.prefix, err)
me.logf("%s close : %v\n", me.prefix, err)
} else {
me.t.Logf("%s close\n", me.prefix)
me.logf("%s close\n", me.prefix)
}
return
}

func (me *logIO) logf(format string, args ...interface{}) {
me.m.Lock()
me.t.Logf(format, args...)
me.m.Unlock()
}

0 comments on commit 93127f4

Please sign in to comment.