Skip to content

Commit 91f7ae5

Browse files
committed
compattest: log connstate changes if ctx not done
The tests on PR#318 flaked because of the log-after-test-completion race-detection. Avoid logging if the context is canceled. (this won't 100% eliminate the race, but it'll bring it down to a super-rare race)
1 parent 2b339d2 commit 91f7ae5

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

compattest/head_server_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,14 @@ func runPeer(ctx context.Context, t testing.TB, u *galaxycache.Universe, priL, s
109109
Handler: &mux,
110110
ErrorLog: log.New(&prefixedTestLogWriter{t: t, prefix: "http: "}, cfg.SelfName+": ", log.Ltime|log.Ldate|log.Lmicroseconds),
111111
ConnState: func(conn net.Conn, cs http.ConnState) {
112-
t.Logf("%s: connection from %s to %s changed state to %s", cfg.SelfName, conn.RemoteAddr(), conn.LocalAddr(), cs)
112+
// Try not to log if the context has already been canceled. (likely because the test finished)
113+
// (logging after a test completes from another goroutine (intentionally) triggers the race detector)
114+
// Checking the context _may_ provide a synchronization edge that avoids that issue.
115+
select {
116+
case <-ctx.Done():
117+
default:
118+
t.Logf("%s: connection from %s to %s changed state to %s", cfg.SelfName, conn.RemoteAddr(), conn.LocalAddr(), cs)
119+
}
113120
},
114121
}
115122

0 commit comments

Comments
 (0)