From 7a7a73f60222951b6f1f0d84c161d1a52f63be70 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Tue, 31 Aug 2021 19:54:46 +0100 Subject: [PATCH] Ensure that the testlogger has its final test removal safely It is possible to get final data race right at the end of the TestMain in integrations during the final removal of the test from it. This PR uses a Reset function to remove any final tests but adds some extra logging which will forcibly fail if there is an unclosed logger. Signed-off-by: Andrew Thornton --- integrations/integration_test.go | 2 +- integrations/testlogger.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/integrations/integration_test.go b/integrations/integration_test.go index 7ddcf3e1cde46..1003f136ddfd9 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -120,7 +120,7 @@ func TestMain(m *testing.M) { } exitCode := m.Run() - writerCloser.t = nil + writerCloser.Reset() if err = util.RemoveAll(setting.Indexer.IssuePath); err != nil { fmt.Printf("util.RemoveAll: %v\n", err) diff --git a/integrations/testlogger.go b/integrations/testlogger.go index ff408b314c868..69db480c159f1 100644 --- a/integrations/testlogger.go +++ b/integrations/testlogger.go @@ -90,6 +90,21 @@ func (w *testLoggerWriterCloser) Close() error { return nil } +func (w *testLoggerWriterCloser) Reset() { + w.Lock() + if len(w.t) > 0 { + for _, t := range w.t { + if t == nil { + continue + } + fmt.Fprintf(os.Stdout, "Unclosed logger writer in test: %s", (*t).Name()) + (*t).Errorf("Unclosed logger writer in test: %s", (*t).Name()) + } + w.t = nil + } + w.Unlock() +} + // PrintCurrentTest prints the current test to os.Stdout func PrintCurrentTest(t testing.TB, skip ...int) func() { start := time.Now()