Fix "data race" in testlogger (#9159)

* Fix data race in testlogger

* Update git_helper_for_declarative_test.go
This commit is contained in:
zeripath 2019-11-25 23:21:37 +00:00 committed by Lauris BH
parent f5bd0884d2
commit 055f6d2296
67 changed files with 213 additions and 200 deletions

View file

@ -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