Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions scaletest/harness/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"io"
"sync"
"time"

"golang.org/x/xerrors"
Expand Down Expand Up @@ -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
Expand All @@ -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)

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