Skip to content

Commit 98b0638

Browse files
authored
Merge pull request #1298 fix race between logger in tests
2 parents a00e662 + dcd4b8f commit 98b0638

15 files changed

+136
-88
lines changed

internal/xtest/logger.go

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,110 +5,129 @@ import (
55
"testing"
66
)
77

8-
func MakeSyncedTest(t testing.TB) *SyncedTest {
8+
func MakeSyncedTest(t *testing.T) *SyncedTest {
99
return &SyncedTest{
10-
TB: t,
10+
T: t,
1111
}
1212
}
1313

1414
type SyncedTest struct {
1515
m sync.Mutex
16-
testing.TB
16+
*testing.T
1717
}
1818

1919
func (s *SyncedTest) Cleanup(f func()) {
2020
s.m.Lock()
2121
defer s.m.Unlock()
22-
s.TB.Helper()
22+
s.T.Helper()
2323

24-
s.TB.Cleanup(f)
24+
s.T.Cleanup(f)
2525
}
2626

2727
func (s *SyncedTest) Error(args ...interface{}) {
2828
s.m.Lock()
2929
defer s.m.Unlock()
30-
s.TB.Helper()
30+
s.T.Helper()
3131

32-
s.TB.Error(args...)
32+
s.T.Error(args...)
3333
}
3434

3535
func (s *SyncedTest) Errorf(format string, args ...interface{}) {
3636
s.m.Lock()
3737
defer s.m.Unlock()
38-
s.TB.Helper()
38+
s.T.Helper()
3939

40-
s.TB.Errorf(format, args...)
40+
s.T.Errorf(format, args...)
4141
}
4242

4343
func (s *SyncedTest) Fail() {
4444
s.m.Lock()
4545
defer s.m.Unlock()
46-
s.TB.Helper()
46+
s.T.Helper()
4747

48-
s.TB.Fail()
48+
s.T.Fail()
4949
}
5050

5151
func (s *SyncedTest) FailNow() {
5252
s.m.Lock()
5353
defer s.m.Unlock()
54-
s.TB.Helper()
54+
s.T.Helper()
5555

56-
s.TB.FailNow()
56+
s.T.FailNow()
5757
}
5858

5959
func (s *SyncedTest) Failed() bool {
6060
s.m.Lock()
6161
defer s.m.Unlock()
62-
s.TB.Helper()
62+
s.T.Helper()
6363

64-
return s.TB.Failed()
64+
return s.T.Failed()
6565
}
6666

6767
func (s *SyncedTest) Fatal(args ...interface{}) {
6868
s.m.Lock()
6969
defer s.m.Unlock()
70-
s.TB.Helper()
70+
s.T.Helper()
7171

72-
s.TB.Fatal(args...)
72+
s.T.Fatal(args...)
7373
}
7474

7575
func (s *SyncedTest) Fatalf(format string, args ...interface{}) {
7676
s.m.Lock()
7777
defer s.m.Unlock()
78-
s.TB.Helper()
78+
s.T.Helper()
7979

80-
s.TB.Fatalf(format, args...)
80+
s.T.Fatalf(format, args...)
8181
}
8282

8383
// must direct called
8484
// func (s *SyncedTest) Helper() {
8585
// s.m.Lock()
8686
// defer s.m.Unlock()
87-
// s.TB.Helper()
87+
// s.T.Helper()
8888
//}
8989

9090
func (s *SyncedTest) Log(args ...interface{}) {
9191
s.m.Lock()
9292
defer s.m.Unlock()
93-
s.TB.Helper()
93+
s.T.Helper()
9494

95-
s.TB.Log(args...)
95+
s.T.Log(args...)
9696
}
9797

9898
func (s *SyncedTest) Logf(format string, args ...interface{}) {
9999
s.m.Lock()
100100
defer s.m.Unlock()
101-
s.TB.Helper()
101+
s.T.Helper()
102102

103-
s.TB.Logf(format, args...)
103+
s.T.Logf(format, args...)
104104
}
105105

106106
func (s *SyncedTest) Name() string {
107107
s.m.Lock()
108108
defer s.m.Unlock()
109-
s.TB.Helper()
109+
s.T.Helper()
110110

111-
return s.TB.Name()
111+
return s.T.Name()
112+
}
113+
114+
func (s *SyncedTest) Run(name string, f func(t *testing.T)) bool {
115+
s.m.Lock()
116+
defer s.m.Unlock()
117+
s.T.Helper()
118+
119+
return s.T.Run(name, f)
120+
}
121+
122+
func (s *SyncedTest) RunSynced(name string, f func(t *SyncedTest)) bool {
123+
s.m.Lock()
124+
defer s.m.Unlock()
125+
s.T.Helper()
126+
127+
return s.T.Run(name, func(t *testing.T) {
128+
syncedTest := MakeSyncedTest(t)
129+
f(syncedTest)
130+
})
112131
}
113132

114133
func (s *SyncedTest) Setenv(key, value string) {
@@ -118,38 +137,38 @@ func (s *SyncedTest) Setenv(key, value string) {
118137
func (s *SyncedTest) Skip(args ...interface{}) {
119138
s.m.Lock()
120139
defer s.m.Unlock()
121-
s.TB.Helper()
140+
s.T.Helper()
122141

123-
s.TB.Skip(args...)
142+
s.T.Skip(args...)
124143
}
125144

126145
func (s *SyncedTest) SkipNow() {
127146
s.m.Lock()
128147
defer s.m.Unlock()
129-
s.TB.Helper()
148+
s.T.Helper()
130149

131-
s.TB.SkipNow()
150+
s.T.SkipNow()
132151
}
133152

134153
func (s *SyncedTest) Skipf(format string, args ...interface{}) {
135154
s.m.Lock()
136155
defer s.m.Unlock()
137-
s.TB.Helper()
138-
s.TB.Skipf(format, args...)
156+
s.T.Helper()
157+
s.T.Skipf(format, args...)
139158
}
140159

141160
func (s *SyncedTest) Skipped() bool {
142161
s.m.Lock()
143162
defer s.m.Unlock()
144-
s.TB.Helper()
163+
s.T.Helper()
145164

146-
return s.TB.Skipped()
165+
return s.T.Skipped()
147166
}
148167

149168
func (s *SyncedTest) TempDir() string {
150169
s.m.Lock()
151170
defer s.m.Unlock()
152-
s.TB.Helper()
171+
s.T.Helper()
153172

154-
return s.TB.TempDir()
173+
return s.T.TempDir()
155174
}

tests/integration/basic_example_native_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/ydb-platform/ydb-go-sdk/v3/balancers"
2727
"github.com/ydb-platform/ydb-go-sdk/v3/config"
2828
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync"
29+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
2930
"github.com/ydb-platform/ydb-go-sdk/v3/log"
3031
"github.com/ydb-platform/ydb-go-sdk/v3/meta"
3132
"github.com/ydb-platform/ydb-go-sdk/v3/sugar"
@@ -154,7 +155,8 @@ func (s *stats) removeFromInFlight(t testing.TB, id string) {
154155
delete(s.inFlightSessions, id)
155156
}
156157

157-
func TestBasicExampleNative(t *testing.T) { //nolint:gocyclo
158+
func TestBasicExampleNative(sourceTest *testing.T) { //nolint:gocyclo
159+
t := xtest.MakeSyncedTest(sourceTest)
158160
folder := t.Name()
159161

160162
ctx, cancel := context.WithTimeout(context.Background(), 42*time.Second)

tests/integration/connection_secure_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ import (
3030
)
3131

3232
//nolint:gocyclo
33-
func TestConnectionSecure(t *testing.T) {
33+
func TestConnectionSecure(sourceTest *testing.T) {
34+
t := xtest.MakeSyncedTest(sourceTest)
3435
dsn, has := os.LookupEnv("YDB_CONNECTION_STRING_SECURE")
3536
if !has {
3637
t.Skipf("require YDB_CONNECTION_STRING_SECURE env")

tests/integration/connection_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import (
3636
)
3737

3838
//nolint:gocyclo
39-
func TestConnection(t *testing.T) {
39+
func TestConnection(sourceTest *testing.T) {
40+
t := xtest.MakeSyncedTest(sourceTest)
4041
const sumColumn = "sum"
4142
var (
4243
userAgent = "connection user agent"
@@ -71,7 +72,7 @@ func TestConnection(t *testing.T) {
7172
ctx = xtest.Context(t)
7273
)
7374

74-
t.Run("ydb.New", func(t *testing.T) {
75+
t.RunSynced("ydb.New", func(t *xtest.SyncedTest) {
7576
db, err := ydb.New(ctx, //nolint:gocritic
7677
ydb.WithConnectionString(os.Getenv("YDB_CONNECTION_STRING")),
7778
ydb.WithAccessTokenCredentials(
@@ -98,7 +99,7 @@ func TestConnection(t *testing.T) {
9899
}
99100
}()
100101
})
101-
t.Run("ydb.Open", func(t *testing.T) {
102+
t.RunSynced("ydb.Open", func(t *xtest.SyncedTest) {
102103
db, err := ydb.Open(ctx,
103104
os.Getenv("YDB_CONNECTION_STRING"),
104105
ydb.WithAccessTokenCredentials(

tests/integration/connection_with_compression_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ import (
3535
)
3636

3737
//nolint:gocyclo
38-
func TestConnectionWithCompression(t *testing.T) {
38+
func TestConnectionWithCompression(sourceTest *testing.T) {
39+
t := xtest.MakeSyncedTest(sourceTest)
3940
const sumColumn = "sum"
4041
var (
4142
userAgent = "connection user agent"

tests/integration/discovery_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import (
2222
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
2323
)
2424

25-
func TestDiscovery(t *testing.T) {
25+
func TestDiscovery(sourceTest *testing.T) {
26+
t := xtest.MakeSyncedTest(sourceTest)
2627
var (
2728
userAgent = "connection user agent"
2829
requestType = "connection request type"

tests/integration/helpers_test.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ import (
1717

1818
"github.com/rekby/fixenv"
1919
"github.com/stretchr/testify/require"
20+
"google.golang.org/grpc"
2021

2122
"github.com/ydb-platform/ydb-go-sdk/v3"
23+
"github.com/ydb-platform/ydb-go-sdk/v3/config"
2224
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync"
25+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
2326
"github.com/ydb-platform/ydb-go-sdk/v3/log"
2427
"github.com/ydb-platform/ydb-go-sdk/v3/sugar"
2528
"github.com/ydb-platform/ydb-go-sdk/v3/table"
@@ -35,21 +38,22 @@ type scopeT struct {
3538
Ctx context.Context
3639
fixenv.Env
3740
Require *require.Assertions
38-
t testing.TB
41+
t *xtest.SyncedTest
3942
}
4043

4144
func newScope(t *testing.T) *scopeT {
42-
at := require.New(t)
43-
fEnv := fixenv.NewEnv(t)
45+
st := xtest.MakeSyncedTest(t)
46+
at := require.New(st)
47+
fEnv := fixenv.New(st)
4448
ctx, ctxCancel := context.WithCancel(context.Background())
45-
t.Cleanup(func() {
49+
st.Cleanup(func() {
4650
ctxCancel()
4751
})
4852
res := &scopeT{
4953
Ctx: ctx,
5054
Env: fEnv,
5155
Require: at,
52-
t: t,
56+
t: st,
5357
}
5458
return res
5559
}
@@ -79,9 +83,21 @@ func (scope *scopeT) AuthToken() string {
7983
}
8084

8185
func (scope *scopeT) Driver(opts ...ydb.Option) *ydb.Driver {
86+
return scope.driverNamed("default", opts...)
87+
}
88+
89+
func (scope *scopeT) DriverWithGRPCLogging() *ydb.Driver {
90+
return scope.driverNamed("grpc-logged", ydb.With(config.WithGrpcOptions(
91+
grpc.WithChainUnaryInterceptor(xtest.NewGrpcLogger(scope.t).UnaryClientInterceptor),
92+
grpc.WithChainStreamInterceptor(xtest.NewGrpcLogger(scope.t).StreamClientInterceptor),
93+
)),
94+
)
95+
}
96+
97+
func (scope *scopeT) driverNamed(name string, opts ...ydb.Option) *ydb.Driver {
8298
f := func() (*fixenv.GenericResult[*ydb.Driver], error) {
8399
connectionString := scope.ConnectionString()
84-
scope.Logf("Connect with connection string: %v", connectionString)
100+
scope.Logf("Connect with connection string, driver name %q: %v", name, connectionString)
85101

86102
token := scope.AuthToken()
87103
if token == "" {
@@ -111,7 +127,7 @@ func (scope *scopeT) Driver(opts ...ydb.Option) *ydb.Driver {
111127
return fixenv.NewGenericResultWithCleanup(driver, clean), err
112128
}
113129

114-
return fixenv.CacheResult(scope.Env, f)
130+
return fixenv.CacheResult(scope.Env, f, fixenv.CacheOptions{CacheKey: name})
115131
}
116132

117133
func (scope *scopeT) SQLDriver(opts ...ydb.ConnectorOption) *sql.DB {
@@ -319,7 +335,7 @@ func (scope *scopeT) TablePath(opts ...func(t *tableNameParams)) string {
319335

320336
// logger for tests
321337
type testLogger struct {
322-
test testing.TB
338+
test *xtest.SyncedTest
323339
testName string
324340
minLevel log.Level
325341

@@ -328,11 +344,11 @@ type testLogger struct {
328344
messages []string
329345
}
330346

331-
func newLogger(t testing.TB) *testLogger {
347+
func newLogger(t *xtest.SyncedTest) *testLogger {
332348
return newLoggerWithMinLevel(t, 0)
333349
}
334350

335-
func newLoggerWithMinLevel(t testing.TB, level log.Level) *testLogger {
351+
func newLoggerWithMinLevel(t *xtest.SyncedTest, level log.Level) *testLogger {
336352
logger := &testLogger{
337353
test: t,
338354
testName: t.Name(),

tests/integration/metrics_registry_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ func (registry *registryConfig) Details() trace.Details {
430430
return registry.details
431431
}
432432

433-
func withMetrics(t *testing.T, details trace.Details, interval time.Duration) ydb.Option {
433+
func withMetrics(t testing.TB, details trace.Details, interval time.Duration) ydb.Option {
434434
registry := &registryConfig{
435435
details: details,
436436
gauges: newVec[gaugeVec](),

tests/integration/ratelimiter_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import (
1818
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
1919
)
2020

21-
func TestRatelimiter(t *testing.T) {
21+
func TestRatelimiter(sourceTest *testing.T) {
22+
t := xtest.MakeSyncedTest(sourceTest)
2223
const (
2324
testCoordinationNodePath = "/local/ratelimiter_test"
2425
testResource = "test_resource"

0 commit comments

Comments
 (0)