Skip to content

Commit 4b2ba69

Browse files
committed
fix: add a mutex around reading logs from scaletests
1 parent fcde77b commit 4b2ba69

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

scaletest/harness/run.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"io"
7+
"sync"
78
"time"
89

910
"golang.org/x/xerrors"
@@ -65,7 +66,7 @@ type TestRun struct {
6566
id string
6667
runner Runnable
6768

68-
logs *bytes.Buffer
69+
logs *syncBuffer
6970
done chan struct{}
7071
started time.Time
7172
duration time.Duration
@@ -87,7 +88,10 @@ func (r *TestRun) FullID() string {
8788
// Run executes the Run function with a self-managed log writer, panic handler,
8889
// error recording and duration recording. The test error is returned.
8990
func (r *TestRun) Run(ctx context.Context) (err error) {
90-
r.logs = new(bytes.Buffer)
91+
r.logs = &syncBuffer{
92+
buf: new(bytes.Buffer),
93+
mut: new(sync.Mutex),
94+
}
9195
r.done = make(chan struct{})
9296
defer close(r.done)
9397

@@ -132,3 +136,20 @@ func (r *TestRun) Cleanup(ctx context.Context) (err error) {
132136
//nolint:revive // we use named returns because we mutate it in a defer
133137
return
134138
}
139+
140+
type syncBuffer struct {
141+
buf *bytes.Buffer
142+
mut *sync.Mutex
143+
}
144+
145+
func (sb *syncBuffer) Write(p []byte) (n int, err error) {
146+
sb.mut.Lock()
147+
defer sb.mut.Unlock()
148+
return sb.buf.Write(p)
149+
}
150+
151+
func (sb *syncBuffer) String() string {
152+
sb.mut.Lock()
153+
defer sb.mut.Unlock()
154+
return sb.buf.String()
155+
}

0 commit comments

Comments
 (0)