diff --git a/scaletest/harness/run.go b/scaletest/harness/run.go index 8a3bd4e4d3..4ee4ee976c 100644 --- a/scaletest/harness/run.go +++ b/scaletest/harness/run.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "io" + "sync" "time" "golang.org/x/xerrors" @@ -65,7 +66,7 @@ type TestRun struct { id string runner Runnable - logs *bytes.Buffer + logs *syncBuffer done chan struct{} started time.Time duration time.Duration @@ -87,7 +88,9 @@ func (r *TestRun) FullID() string { // Run executes the Run function with a self-managed log writer, panic handler, // error recording and duration recording. The test error is returned. func (r *TestRun) Run(ctx context.Context) (err error) { - r.logs = new(bytes.Buffer) + r.logs = &syncBuffer{ + buf: new(bytes.Buffer), + } r.done = make(chan struct{}) defer close(r.done) @@ -132,3 +135,20 @@ func (r *TestRun) Cleanup(ctx context.Context) (err error) { //nolint:revive // we use named returns because we mutate it in a defer return } + +type syncBuffer struct { + buf *bytes.Buffer + mut sync.Mutex +} + +func (sb *syncBuffer) Write(p []byte) (n int, err error) { + sb.mut.Lock() + defer sb.mut.Unlock() + return sb.buf.Write(p) +} + +func (sb *syncBuffer) String() string { + sb.mut.Lock() + defer sb.mut.Unlock() + return sb.buf.String() +}