Skip to content

Commit 6e0e414

Browse files
authored
Ensure that the testlogger has its final test removal safely (#16907)
It is possible to get a data race right at the end of the TestMain in integrations during the final removal of the test from the testlogger. 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 <[email protected]>
1 parent de2e96e commit 6e0e414

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

integrations/integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func TestMain(m *testing.M) {
120120
}
121121
exitCode := m.Run()
122122

123-
writerCloser.t = nil
123+
writerCloser.Reset()
124124

125125
if err = util.RemoveAll(setting.Indexer.IssuePath); err != nil {
126126
fmt.Printf("util.RemoveAll: %v\n", err)

integrations/testlogger.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ func (w *testLoggerWriterCloser) Close() error {
9090
return nil
9191
}
9292

93+
func (w *testLoggerWriterCloser) Reset() {
94+
w.Lock()
95+
if len(w.t) > 0 {
96+
for _, t := range w.t {
97+
if t == nil {
98+
continue
99+
}
100+
fmt.Fprintf(os.Stdout, "Unclosed logger writer in test: %s", (*t).Name())
101+
(*t).Errorf("Unclosed logger writer in test: %s", (*t).Name())
102+
}
103+
w.t = nil
104+
}
105+
w.Unlock()
106+
}
107+
93108
// PrintCurrentTest prints the current test to os.Stdout
94109
func PrintCurrentTest(t testing.TB, skip ...int) func() {
95110
start := time.Now()

0 commit comments

Comments
 (0)