Skip to content

Commit

Permalink
prevent race while write logs
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby committed Dec 8, 2023
1 parent 00489d1 commit 882a296
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion tests/integration/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"context"
"database/sql"
"fmt"
"os"
"path"
"strings"
Expand All @@ -18,6 +19,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/ydb-platform/ydb-go-sdk/v3"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync"
"github.com/ydb-platform/ydb-go-sdk/v3/log"
"github.com/ydb-platform/ydb-go-sdk/v3/sugar"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
Expand Down Expand Up @@ -250,7 +252,10 @@ func newLoggerWithMinLevel(t testing.TB, level log.Level) *testLogger {
return &testLogger{test: t, minLevel: level}
}

var loggerMutex xsync.Mutex

func (t testLogger) Log(ctx context.Context, msg string, fields ...log.Field) {
t.test.Helper()
lvl := log.LevelFromContext(ctx)
if lvl < t.minLevel {
return
Expand All @@ -264,5 +269,8 @@ func (t testLogger) Log(ctx context.Context, msg string, fields ...log.Field) {
values[field.Key()] = field.String()
}

t.test.Logf("[%s] %s: %v (%v)", lvl, loggerName, msg, values)
message := fmt.Sprintf("[%s] %s: %v (%v)", lvl, loggerName, msg, values)
loggerMutex.WithLock(func() {
t.test.Log(message)
})
}

0 comments on commit 882a296

Please sign in to comment.