mirror of
https://github.com/coder/coder.git
synced 2025-07-23 21:32:07 +00:00
fix: add a mutex around reading logs from scaletests (#7521)
This commit is contained in:
@ -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()
|
||||
}
|
||||
|
Reference in New Issue
Block a user