summaryrefslogtreecommitdiffstats
path: root/integrations/testlogger.go
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2019-11-25 23:21:37 +0000
committerLauris BH <lauris@nix.lv>2019-11-26 01:21:37 +0200
commit055f6d229672524e3933940f95aa28939d94961a (patch)
tree72d1d427ce694517b70b6b751e35fdf6d5874428 /integrations/testlogger.go
parentf5bd0884d2adea8ef7ba1bbd9dacb240d7c6b0f7 (diff)
downloadgitea-055f6d229672524e3933940f95aa28939d94961a.tar.gz
gitea-055f6d229672524e3933940f95aa28939d94961a.zip
Fix "data race" in testlogger (#9159)
* Fix data race in testlogger * Update git_helper_for_declarative_test.go
Diffstat (limited to 'integrations/testlogger.go')
-rw-r--r--integrations/testlogger.go23
1 files changed, 17 insertions, 6 deletions
diff --git a/integrations/testlogger.go b/integrations/testlogger.go
index 91b94ac9f3..624abf3f81 100644
--- a/integrations/testlogger.go
+++ b/integrations/testlogger.go
@@ -27,20 +27,23 @@ var writerCloser = &testLoggerWriterCloser{}
type testLoggerWriterCloser struct {
sync.RWMutex
- t testing.TB
+ t []*testing.TB
}
func (w *testLoggerWriterCloser) setT(t *testing.TB) {
w.Lock()
- w.t = *t
+ w.t = append(w.t, t)
w.Unlock()
}
func (w *testLoggerWriterCloser) Write(p []byte) (int, error) {
w.RLock()
- t := w.t
+ var t *testing.TB
+ if len(w.t) > 0 {
+ t = w.t[len(w.t)-1]
+ }
w.RUnlock()
- if t != nil {
+ if t != nil && *t != nil {
if len(p) > 0 && p[len(p)-1] == '\n' {
p = p[:len(p)-1]
}
@@ -65,18 +68,23 @@ func (w *testLoggerWriterCloser) Write(p []byte) (int, error) {
}
}()
- t.Log(string(p))
+ (*t).Log(string(p))
return len(p), nil
}
return len(p), nil
}
func (w *testLoggerWriterCloser) Close() error {
+ w.Lock()
+ if len(w.t) > 0 {
+ w.t = w.t[:len(w.t)-1]
+ }
+ w.Unlock()
return nil
}
// PrintCurrentTest prints the current test to os.Stdout
-func PrintCurrentTest(t testing.TB, skip ...int) {
+func PrintCurrentTest(t testing.TB, skip ...int) func() {
actualSkip := 1
if len(skip) > 0 {
actualSkip = skip[0]
@@ -89,6 +97,9 @@ func PrintCurrentTest(t testing.TB, skip ...int) {
fmt.Fprintf(os.Stdout, "=== %s (%s:%d)\n", t.Name(), strings.TrimPrefix(filename, prefix), line)
}
writerCloser.setT(&t)
+ return func() {
+ _ = writerCloser.Close()
+ }
}
// Printf takes a format and args and prints the string to os.Stdout