Skip to content

Commit 5b6582c

Browse files
alarbadaguillemus
andauthored
add connector logs to tests (#220)
* add connector logs to tests * run go mod tidy * fix linter * handle -test.short -test.v flags --------- Co-authored-by: Guillem <[email protected]>
1 parent fffd486 commit 5b6582c

13 files changed

+46
-32
lines changed

destination_integration_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727

2828
func TestDestination_Write(t *testing.T) {
2929
is := is.New(t)
30-
ctx := context.Background()
30+
ctx := test.Context(t)
3131
conn := test.ConnectSimple(ctx, t, test.RegularConnString)
3232
tableName := test.SetupTestTable(ctx, t, conn)
3333

@@ -144,7 +144,7 @@ func TestDestination_Write(t *testing.T) {
144144

145145
func TestDestination_Batch(t *testing.T) {
146146
is := is.New(t)
147-
ctx := context.Background()
147+
ctx := test.Context(t)
148148
conn := test.ConnectSimple(ctx, t, test.RegularConnString)
149149
tableName := test.SetupTestTable(ctx, t, conn)
150150

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/jackc/pglogrepl v0.0.0-20240307033717-828fbfe908e9
1717
github.com/jackc/pgx/v5 v5.7.1
1818
github.com/matryer/is v1.4.1
19+
github.com/rs/zerolog v1.33.0
1920
golang.org/x/tools v0.26.0
2021
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637
2122
mvdan.cc/gofumpt v0.7.0
@@ -163,7 +164,6 @@ require (
163164
github.com/quasilyte/regex/syntax v0.0.0-20210819130434-b3f0c404a727 // indirect
164165
github.com/quasilyte/stdinfo v0.0.0-20220114132959-f7386bf02567 // indirect
165166
github.com/rivo/uniseg v0.4.7 // indirect
166-
github.com/rs/zerolog v1.33.0 // indirect
167167
github.com/ryancurrah/gomodguard v1.3.5 // indirect
168168
github.com/ryanrolds/sqlclosecheck v0.5.1 // indirect
169169
github.com/sagikazarmark/locafero v0.6.0 // indirect

source/logrepl/cdc_test.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
)
3737

3838
func TestCDCIterator_New(t *testing.T) {
39-
ctx := context.Background()
39+
ctx := test.Context(t)
4040
pool := test.ConnectPool(ctx, t, test.RepmgrConnString)
4141

4242
tests := []struct {
@@ -120,7 +120,7 @@ func TestCDCIterator_New(t *testing.T) {
120120
}
121121

122122
func TestCDCIterator_Next(t *testing.T) {
123-
ctx := context.Background()
123+
ctx := test.Context(t)
124124
is := is.New(t)
125125

126126
pool := test.ConnectPool(ctx, t, test.RepmgrConnString)
@@ -342,7 +342,7 @@ func TestCDCIterator_Next(t *testing.T) {
342342
}
343343

344344
func TestCDCIterator_Next_Fail(t *testing.T) {
345-
ctx := context.Background()
345+
ctx := test.Context(t)
346346

347347
pool := test.ConnectPool(ctx, t, test.RepmgrConnString)
348348
table := test.SetupTestTable(ctx, t, pool)
@@ -376,7 +376,7 @@ func TestCDCIterator_Next_Fail(t *testing.T) {
376376
}
377377

378378
func TestCDCIterator_EnsureLSN(t *testing.T) {
379-
ctx := context.Background()
379+
ctx := test.Context(t)
380380
is := is.New(t)
381381

382382
pool := test.ConnectPool(ctx, t, test.RepmgrConnString)
@@ -416,7 +416,7 @@ func TestCDCIterator_EnsureLSN(t *testing.T) {
416416
}
417417

418418
func TestCDCIterator_Ack(t *testing.T) {
419-
ctx := context.Background()
419+
ctx := test.Context(t)
420420

421421
tests := []struct {
422422
name string
@@ -501,7 +501,8 @@ func testCDCIterator(ctx context.Context, t *testing.T, pool *pgxpool.Pool, tabl
501501
func fetchSlotStats(t *testing.T, c test.Querier, slotName string) (pglogrepl.LSN, pglogrepl.LSN, error) {
502502
t.Helper()
503503

504-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
504+
ctx := test.Context(t)
505+
ctx, cancel := context.WithTimeout(ctx, time.Second*15)
505506
defer cancel()
506507

507508
var writeLSN, flushLSN pglogrepl.LSN
@@ -522,7 +523,7 @@ func fetchSlotStats(t *testing.T, c test.Querier, slotName string) (pglogrepl.LS
522523
}
523524

524525
func TestCDCIterator_Schema(t *testing.T) {
525-
ctx := context.Background()
526+
ctx := test.Context(t)
526527

527528
pool := test.ConnectPool(ctx, t, test.RepmgrConnString)
528529
table := test.SetupTestTable(ctx, t, pool)

source/logrepl/combined_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestConfig_Validate(t *testing.T) {
4747
}
4848

4949
func TestCombinedIterator_New(t *testing.T) {
50-
ctx := context.Background()
50+
ctx := test.Context(t)
5151
pool := test.ConnectPool(ctx, t, test.RepmgrConnString)
5252
table := test.SetupTestTable(ctx, t, pool)
5353

@@ -137,7 +137,8 @@ func TestCombinedIterator_New(t *testing.T) {
137137
}
138138

139139
func TestCombinedIterator_Next(t *testing.T) {
140-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
140+
ctx := test.Context(t)
141+
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
141142
defer cancel()
142143

143144
is := is.New(t)

source/logrepl/internal/publication_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package internal
1616

1717
import (
18-
"context"
1918
"fmt"
2019
"strings"
2120
"testing"
@@ -25,7 +24,7 @@ import (
2524
)
2625

2726
func TestCreatePublication(t *testing.T) {
28-
ctx := context.Background()
27+
ctx := test.Context(t)
2928
pool := test.ConnectPool(ctx, t, test.RegularConnString)
3029

3130
pubNames := []string{"testpub", "123", "test-hyphen", "test=equal"}
@@ -71,7 +70,7 @@ func TestCreatePublication(t *testing.T) {
7170
}
7271

7372
func TestCreatePublicationForTables(t *testing.T) {
74-
ctx := context.Background()
73+
ctx := test.Context(t)
7574
pub := test.RandomIdentifier(t)
7675
pool := test.ConnectPool(ctx, t, test.RegularConnString)
7776

@@ -100,7 +99,7 @@ func TestCreatePublicationForTables(t *testing.T) {
10099
}
101100

102101
func TestDropPublication(t *testing.T) {
103-
ctx := context.Background()
102+
ctx := test.Context(t)
104103
is := is.New(t)
105104
pub := test.RandomIdentifier(t)
106105

source/logrepl/internal/relationset_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestRelationSetAllTypes(t *testing.T) {
4646
// any machine (CI or local)
4747
time.Local = nil
4848

49-
ctx := context.Background()
49+
ctx := test.Context(t)
5050
is := is.New(t)
5151

5252
pool := test.ConnectPool(ctx, t, test.RepmgrConnString)

source/logrepl/internal/replication_slot_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package internal
1616

1717
import (
18-
"context"
1918
"errors"
2019
"fmt"
2120
"testing"
@@ -26,7 +25,7 @@ import (
2625

2726
func Test_ReadReplicationSlot(t *testing.T) {
2827
var (
29-
ctx = context.Background()
28+
ctx = test.Context(t)
3029
pool = test.ConnectPool(ctx, t, test.RepmgrConnString)
3130
slotName = test.RandomIdentifier(t)
3231
)

source/logrepl/internal/subscription_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

3131
func TestSubscription_Create(t *testing.T) {
32-
ctx := context.Background()
32+
ctx := test.Context(t)
3333
is := is.New(t)
3434
pool := test.ConnectPool(ctx, t, test.RepmgrConnString)
3535
pool.Close()
@@ -40,7 +40,7 @@ func TestSubscription_Create(t *testing.T) {
4040

4141
func TestSubscription_WithRepmgr(t *testing.T) {
4242
var (
43-
ctx = context.Background()
43+
ctx = test.Context(t)
4444
pool = test.ConnectPool(ctx, t, test.RepmgrConnString)
4545
table1 = test.SetupTestTable(ctx, t, pool)
4646
table2 = test.SetupTestTable(ctx, t, pool)
@@ -150,7 +150,8 @@ func TestSubscription_WithRepmgr(t *testing.T) {
150150
}
151151

152152
func TestSubscription_ClosedContext(t *testing.T) {
153-
ctx, cancel := context.WithCancel(context.Background())
153+
ctx := test.Context(t)
154+
ctx, cancel := context.WithCancel(ctx)
154155

155156
var (
156157
is = is.New(t)

source/schema/avro_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
)
3333

3434
func Test_AvroExtract(t *testing.T) {
35-
ctx := context.Background()
35+
ctx := test.Context(t)
3636
is := is.New(t)
3737

3838
c := test.ConnectSimple(ctx, t, test.RegularConnString)

source/snapshot/fetch_worker_test.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func Test_FetchConfigValidate(t *testing.T) {
119119

120120
func Test_FetcherValidate(t *testing.T) {
121121
var (
122-
ctx = context.Background()
122+
ctx = test.Context(t)
123123
pool = test.ConnectPool(ctx, t, test.RegularConnString)
124124
table = test.SetupTestTable(ctx, t, pool)
125125
)
@@ -207,7 +207,7 @@ func Test_FetcherRun_Initial(t *testing.T) {
207207
table = test.SetupTestTable(context.Background(), t, pool)
208208
is = is.New(t)
209209
out = make(chan FetchData)
210-
ctx = context.Background()
210+
ctx = test.Context(t)
211211
tt = &tomb.Tomb{}
212212
)
213213

@@ -262,7 +262,7 @@ func Test_FetcherRun_Resume(t *testing.T) {
262262
table = test.SetupTestTable(context.Background(), t, pool)
263263
is = is.New(t)
264264
out = make(chan FetchData)
265-
ctx = context.Background()
265+
ctx = test.Context(t)
266266
tt = &tomb.Tomb{}
267267
)
268268

@@ -320,7 +320,7 @@ func Test_FetcherRun_Resume(t *testing.T) {
320320
func Test_withSnapshot(t *testing.T) {
321321
var (
322322
is = is.New(t)
323-
ctx = context.Background()
323+
ctx = test.Context(t)
324324
pool = test.ConnectPool(ctx, t, test.RegularConnString)
325325
)
326326

@@ -388,7 +388,8 @@ func Test_withSnapshot(t *testing.T) {
388388
func Test_send(t *testing.T) {
389389
is := is.New(t)
390390

391-
ctx, cancel := context.WithCancel(context.Background())
391+
ctx := test.Context(t)
392+
ctx, cancel := context.WithCancel(ctx)
392393
f := FetchWorker{conf: FetchConfig{}}
393394

394395
cancel()
@@ -426,7 +427,7 @@ func Test_FetchWorker_buildRecordData(t *testing.T) {
426427
func Test_FetchWorker_updateSnapshotEnd(t *testing.T) {
427428
var (
428429
is = is.New(t)
429-
ctx = context.Background()
430+
ctx = test.Context(t)
430431
pool = test.ConnectPool(ctx, t, test.RegularConnString)
431432
table = test.SetupTestTable(ctx, t, pool)
432433
)
@@ -485,7 +486,7 @@ func Test_FetchWorker_createCursor(t *testing.T) {
485486
pool = test.ConnectPool(context.Background(), t, test.RegularConnString)
486487
table = test.SetupTestTable(context.Background(), t, pool)
487488
is = is.New(t)
488-
ctx = context.Background()
489+
ctx = test.Context(t)
489490
)
490491

491492
f := FetchWorker{

source/snapshot/iterator_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828

2929
func Test_Iterator_Next(t *testing.T) {
3030
var (
31-
ctx = context.Background()
31+
ctx = test.Context(t)
3232
pool = test.ConnectPool(ctx, t, test.RegularConnString)
3333
table = test.SetupTestTable(ctx, t, pool)
3434
)

source_integration_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
func TestSource_Open(t *testing.T) {
2727
is := is.New(t)
28-
ctx := context.Background()
28+
ctx := test.Context(t)
2929
conn := test.ConnectSimple(ctx, t, test.RepmgrConnString)
3030
tableName := test.SetupTestTable(ctx, t, conn)
3131
slotName := "conduitslot1"

test/helper.go

+12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/jackc/pgx/v5/pgconn"
2929
"github.com/jackc/pgx/v5/pgxpool"
3030
"github.com/matryer/is"
31+
"github.com/rs/zerolog"
3132
)
3233

3334
// RepmgrConnString is a replication user connection string for the test postgres.
@@ -250,3 +251,14 @@ func IsPgError(is *is.I, err error, wantCode string) {
250251
is.True(ok) // expected err to be a *pgconn.PgError
251252
is.Equal(pgerr.Code, wantCode)
252253
}
254+
255+
func Context(t *testing.T) context.Context {
256+
ctx := context.Background()
257+
if testing.Short() || !testing.Verbose() {
258+
return ctx
259+
}
260+
261+
writer := zerolog.NewTestWriter(t)
262+
logger := zerolog.New(writer).Level(zerolog.InfoLevel)
263+
return logger.WithContext(ctx)
264+
}

0 commit comments

Comments
 (0)