Skip to content

Commit bd9dcdb

Browse files
committed
Fix data race
1 parent a372363 commit bd9dcdb

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

integrations/testlogger.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"runtime"
1212
"strings"
13+
"sync"
1314
"testing"
1415

1516
"code.gitea.io/gitea/modules/log"
@@ -25,11 +26,21 @@ type TestLogger struct {
2526
var writerCloser = &testLoggerWriterCloser{}
2627

2728
type testLoggerWriterCloser struct {
29+
sync.RWMutex
2830
t testing.TB
2931
}
3032

33+
func (w *testLoggerWriterCloser) setT(t *testing.TB) {
34+
w.Lock()
35+
w.t = *t
36+
w.Unlock()
37+
}
38+
3139
func (w *testLoggerWriterCloser) Write(p []byte) (int, error) {
32-
if w.t != nil {
40+
w.RLock()
41+
t := w.t
42+
w.RUnlock()
43+
if t != nil {
3344
if len(p) > 0 && p[len(p)-1] == '\n' {
3445
p = p[:len(p)-1]
3546
}
@@ -54,7 +65,7 @@ func (w *testLoggerWriterCloser) Write(p []byte) (int, error) {
5465
}
5566
}()
5667

57-
w.t.Log(string(p))
68+
t.Log(string(p))
5869
return len(p), nil
5970
}
6071
return len(p), nil
@@ -77,7 +88,7 @@ func PrintCurrentTest(t testing.TB, skip ...int) {
7788
} else {
7889
fmt.Fprintf(os.Stdout, "=== %s (%s:%d)\n", t.Name(), strings.TrimPrefix(filename, prefix), line)
7990
}
80-
writerCloser.t = t
91+
writerCloser.setT(&t)
8192
}
8293

8394
// Printf takes a format and args and prints the string to os.Stdout

0 commit comments

Comments
 (0)