fix: add a mutex around reading logs from scaletests (#7521)

This commit is contained in:
Kyle Carberry
2023-05-14 12:16:00 -05:00
committed by GitHub
parent fcde77b35f
commit 50f2d0c7e9

View File

@ -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()
}