Skip to content

Commit

Permalink
Add locking around test logger to avoid data race (#9326)
Browse files Browse the repository at this point in the history
Fixes #9325

Signed-off-by: Nick Pillitteri <[email protected]>
  • Loading branch information
56quarters authored Sep 18, 2024
1 parent 01185a8 commit c061e0b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/storage/ingest/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/twmb/franz-go/pkg/kmsg"
"go.uber.org/atomic"

mimirtest "github.com/grafana/mimir/pkg/util/test"
"github.com/grafana/mimir/pkg/util/testkafka"
)

Expand Down Expand Up @@ -1713,7 +1714,7 @@ func withLogger(logger log.Logger) func(cfg *readerTestCfg) {
func defaultReaderTestConfig(t *testing.T, addr string, topicName string, partitionID int32, consumer recordConsumer) *readerTestCfg {
return &readerTestCfg{
registry: prometheus.NewPedanticRegistry(),
logger: testutil.NewLogger(t),
logger: mimirtest.NewTestingLogger(t),
kafka: createTestKafkaConfig(addr, topicName),
partitionID: partitionID,
consumer: consumer,
Expand Down
8 changes: 7 additions & 1 deletion pkg/util/test/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
package test

import (
"sync"
"testing"
"time"

"github.com/go-kit/log"
)

type testingLogger struct {
t testing.TB
t testing.TB
mtx sync.Mutex
}

func NewTestingLogger(t testing.TB) log.Logger {
Expand All @@ -22,6 +24,10 @@ func NewTestingLogger(t testing.TB) log.Logger {
func (l *testingLogger) Log(keyvals ...interface{}) error {
// Prepend log with timestamp.
keyvals = append([]interface{}{time.Now().String()}, keyvals...)

l.mtx.Lock()
l.t.Log(keyvals...)
l.mtx.Unlock()

return nil
}

0 comments on commit c061e0b

Please sign in to comment.