From 18471a8792b74826672f0b0e2f2ab01c09cd1dfe Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Fri, 8 Jan 2021 16:50:03 -0800 Subject: [PATCH] ipn: close logger at the end of TestLocalLogLines If any goroutine continues to use the logger in TestLocalLogLines after the test finishes, the test panics. The culprit for this was wireguard-go; the previous commit fixed that. This commit adds suspenders: When the test is done, make logging calls into no-ops. Signed-off-by: Josh Bleecher Snyder --- ipn/loglines_test.go | 1 + tstest/log.go | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/ipn/loglines_test.go b/ipn/loglines_test.go index 0ea12d386..75e830dc0 100644 --- a/ipn/loglines_test.go +++ b/ipn/loglines_test.go @@ -26,6 +26,7 @@ func TestLocalLogLines(t *testing.T) { "[v1] peer keys: %s", "[v1] v%v peers: %v", }) + defer logListen.Close() logid := func(hex byte) logtail.PublicID { var ret logtail.PublicID diff --git a/tstest/log.go b/tstest/log.go index 7b6ac2b47..b7750cf12 100644 --- a/tstest/log.go +++ b/tstest/log.go @@ -75,13 +75,18 @@ type LogLineTracker struct { logf logger.Logf listenFor []string - mu sync.Mutex - seen map[string]bool // format string => false (if not yet seen but wanted) or true (once seen) + mu sync.Mutex + closed bool + seen map[string]bool // format string => false (if not yet seen but wanted) or true (once seen) } // Logf logs to its underlying logger and also tracks that the given format pattern has been seen. func (lt *LogLineTracker) Logf(format string, args ...interface{}) { lt.mu.Lock() + if lt.closed { + lt.mu.Unlock() + return + } if v, ok := lt.seen[format]; ok && !v { lt.seen[format] = true } @@ -101,3 +106,10 @@ func (lt *LogLineTracker) Check() []string { } return notSeen } + +// Close closes lt. After calling Close, calls to Logf become no-ops. +func (lt *LogLineTracker) Close() { + lt.mu.Lock() + defer lt.mu.Unlock() + lt.closed = true +}