Skip to content

Commit fb39da8

Browse files
GODRIVER-2667 Replacing the assert.Soon with the assert.Eventually function (#1668)
1 parent 6e75330 commit fb39da8

10 files changed

+220
-249
lines changed

internal/test/compilecheck/go.mod

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ replace go.mongodb.org/mongo-driver => ../../../
99
require go.mongodb.org/mongo-driver v1.11.7
1010

1111
require (
12-
github.com/golang/snappy v0.0.1 // indirect
12+
github.com/golang/snappy v0.0.4 // indirect
1313
github.com/klauspost/compress v1.13.6 // indirect
14-
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
14+
github.com/montanaflynn/stats v0.7.1 // indirect
1515
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
1616
github.com/xdg-go/scram v1.1.2 // indirect
1717
github.com/xdg-go/stringprep v1.0.4 // indirect
1818
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
19-
golang.org/x/crypto v0.17.0 // indirect
20-
golang.org/x/sync v0.1.0 // indirect
19+
golang.org/x/crypto v0.22.0 // indirect
20+
golang.org/x/sync v0.7.0 // indirect
2121
golang.org/x/text v0.14.0 // indirect
2222
)

internal/test/compilecheck/go.sum

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2-
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
3-
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
4-
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
2+
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
3+
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
4+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
55
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
66
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
7-
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
8-
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
7+
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
8+
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
99
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
1010
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
1111
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
@@ -17,16 +17,16 @@ github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7Jul
1717
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
1818
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
1919
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
20-
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
21-
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
20+
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
21+
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
2222
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
2323
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
2424
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
2525
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
2626
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
2727
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
28-
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
29-
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
28+
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
29+
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
3030
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3131
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3232
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -44,4 +44,3 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
4444
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
4545
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
4646
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
47-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=

mongo/integration/client_test.go

+60-92
Original file line numberDiff line numberDiff line change
@@ -526,31 +526,23 @@ func TestClient(t *testing.T) {
526526

527527
// Assert that the minimum RTT is eventually >250ms.
528528
topo := getTopologyFromClient(mt.Client)
529-
assert.Soon(mt, func(ctx context.Context) {
530-
for {
531-
// Stop loop if callback has been canceled.
532-
select {
533-
case <-ctx.Done():
534-
return
535-
default:
536-
}
537-
538-
time.Sleep(100 * time.Millisecond)
539-
540-
// Wait for all of the server's minimum RTTs to be >250ms.
541-
done := true
542-
for _, desc := range topo.Description().Servers {
543-
server, err := topo.FindServer(desc)
544-
assert.Nil(mt, err, "FindServer error: %v", err)
545-
if server.RTTMonitor().Min() <= 250*time.Millisecond {
546-
done = false
547-
}
548-
}
549-
if done {
550-
return
529+
callback := func() bool {
530+
// Wait for all of the server's minimum RTTs to be >250ms.
531+
for _, desc := range topo.Description().Servers {
532+
server, err := topo.FindServer(desc)
533+
assert.NoError(mt, err, "FindServer error: %v", err)
534+
if server.RTTMonitor().Min() <= 250*time.Millisecond {
535+
return false // the tick should wait for 100ms in this case
551536
}
552537
}
553-
}, 10*time.Second)
538+
539+
return true
540+
}
541+
assert.Eventually(t,
542+
callback,
543+
10*time.Second,
544+
100*time.Millisecond,
545+
"expected that the minimum RTT is eventually >250ms")
554546
})
555547

556548
// Test that if the minimum RTT is greater than the remaining timeout for an operation, the
@@ -574,31 +566,23 @@ func TestClient(t *testing.T) {
574566

575567
// Assert that the minimum RTT is eventually >250ms.
576568
topo := getTopologyFromClient(mt.Client)
577-
assert.Soon(mt, func(ctx context.Context) {
578-
for {
579-
// Stop loop if callback has been canceled.
580-
select {
581-
case <-ctx.Done():
582-
return
583-
default:
584-
}
585-
586-
time.Sleep(100 * time.Millisecond)
587-
588-
// Wait for all of the server's minimum RTTs to be >250ms.
589-
done := true
590-
for _, desc := range topo.Description().Servers {
591-
server, err := topo.FindServer(desc)
592-
assert.Nil(mt, err, "FindServer error: %v", err)
593-
if server.RTTMonitor().Min() <= 250*time.Millisecond {
594-
done = false
595-
}
596-
}
597-
if done {
598-
return
569+
callback := func() bool {
570+
// Wait for all of the server's minimum RTTs to be >250ms.
571+
for _, desc := range topo.Description().Servers {
572+
server, err := topo.FindServer(desc)
573+
assert.NoError(mt, err, "FindServer error: %v", err)
574+
if server.RTTMonitor().Min() <= 250*time.Millisecond {
575+
return false
599576
}
600577
}
601-
}, 10*time.Second)
578+
579+
return true
580+
}
581+
assert.Eventually(t,
582+
callback,
583+
10*time.Second,
584+
100*time.Millisecond,
585+
"expected that the minimum RTT is eventually >250ms")
602586

603587
// Once we've waited for the minimum RTT for the single server to be >250ms, run a bunch of
604588
// Ping operations with a timeout of 250ms and expect that they return errors.
@@ -625,31 +609,23 @@ func TestClient(t *testing.T) {
625609

626610
// Assert that RTT90s are eventually >300ms.
627611
topo := getTopologyFromClient(mt.Client)
628-
assert.Soon(mt, func(ctx context.Context) {
629-
for {
630-
// Stop loop if callback has been canceled.
631-
select {
632-
case <-ctx.Done():
633-
return
634-
default:
635-
}
636-
637-
time.Sleep(100 * time.Millisecond)
638-
639-
// Wait for all of the server's RTT90s to be >300ms.
640-
done := true
641-
for _, desc := range topo.Description().Servers {
642-
server, err := topo.FindServer(desc)
643-
assert.Nil(mt, err, "FindServer error: %v", err)
644-
if server.RTTMonitor().P90() <= 300*time.Millisecond {
645-
done = false
646-
}
647-
}
648-
if done {
649-
return
612+
callback := func() bool {
613+
// Wait for all of the server's RTT90s to be >300ms.
614+
for _, desc := range topo.Description().Servers {
615+
server, err := topo.FindServer(desc)
616+
assert.NoError(mt, err, "FindServer error: %v", err)
617+
if server.RTTMonitor().P90() <= 300*time.Millisecond {
618+
return false
650619
}
651620
}
652-
}, 10*time.Second)
621+
622+
return true
623+
}
624+
assert.Eventually(t,
625+
callback,
626+
10*time.Second,
627+
100*time.Millisecond,
628+
"expected that the RTT90s are eventually >300ms")
653629
})
654630

655631
// Test that if Timeout is set and the RTT90 is greater than the remaining timeout for an operation, the
@@ -676,31 +652,23 @@ func TestClient(t *testing.T) {
676652

677653
// Assert that RTT90s are eventually >275ms.
678654
topo := getTopologyFromClient(mt.Client)
679-
assert.Soon(mt, func(ctx context.Context) {
680-
for {
681-
// Stop loop if callback has been canceled.
682-
select {
683-
case <-ctx.Done():
684-
return
685-
default:
686-
}
687-
688-
time.Sleep(100 * time.Millisecond)
689-
690-
// Wait for all of the server's RTT90s to be >275ms.
691-
done := true
692-
for _, desc := range topo.Description().Servers {
693-
server, err := topo.FindServer(desc)
694-
assert.Nil(mt, err, "FindServer error: %v", err)
695-
if server.RTTMonitor().P90() <= 275*time.Millisecond {
696-
done = false
697-
}
698-
}
699-
if done {
700-
return
655+
callback := func() bool {
656+
// Wait for all of the server's RTT90s to be >275ms.
657+
for _, desc := range topo.Description().Servers {
658+
server, err := topo.FindServer(desc)
659+
assert.NoError(mt, err, "FindServer error: %v", err)
660+
if server.RTTMonitor().P90() <= 275*time.Millisecond {
661+
return false
701662
}
702663
}
703-
}, 10*time.Second)
664+
665+
return true
666+
}
667+
assert.Eventually(t,
668+
callback,
669+
10*time.Second,
670+
100*time.Millisecond,
671+
"expected that the RTT90s are eventually >275ms")
704672

705673
// Once we've waited for the RTT90 for the servers to be >275ms, run 10 Ping operations
706674
// with a timeout of 275ms and expect that they return timeout errors.

mongo/integration/csot_prose_test.go

+36-16
Original file line numberDiff line numberDiff line change
@@ -89,55 +89,75 @@ func TestCSOTProse(t *testing.T) {
8989
mt.RunOpts("serverSelectionTimeoutMS honored if timeoutMS is not set", mtOpts, func(mt *mtest.T) {
9090
mt.Parallel()
9191

92-
callback := func(ctx context.Context) {
93-
err := mt.Client.Ping(ctx, nil)
94-
assert.NotNil(mt, err, "expected Ping error, got nil")
92+
callback := func() bool {
93+
err := mt.Client.Ping(context.Background(), nil)
94+
assert.Error(mt, err, "expected Ping error, got nil")
95+
return true
9596
}
9697

9798
// Assert that Ping fails within 150ms due to server selection timeout.
98-
assert.Soon(mt, callback, 150*time.Millisecond)
99+
assert.Eventually(t,
100+
callback,
101+
150*time.Millisecond,
102+
time.Millisecond,
103+
"expected ping to fail within 150ms")
99104
})
100105

101106
cliOpts = options.Client().ApplyURI("mongodb://invalid/?timeoutMS=100&serverSelectionTimeoutMS=200")
102107
mtOpts = mtest.NewOptions().ClientOptions(cliOpts).CreateCollection(false)
103108
mt.RunOpts("timeoutMS honored for server selection if it's lower than serverSelectionTimeoutMS", mtOpts, func(mt *mtest.T) {
104109
mt.Parallel()
105110

106-
callback := func(ctx context.Context) {
107-
err := mt.Client.Ping(ctx, nil)
108-
assert.NotNil(mt, err, "expected Ping error, got nil")
111+
callback := func() bool {
112+
err := mt.Client.Ping(context.Background(), nil)
113+
assert.Error(mt, err, "expected Ping error, got nil")
114+
return true
109115
}
110116

111117
// Assert that Ping fails within 150ms due to timeout.
112-
assert.Soon(mt, callback, 150*time.Millisecond)
118+
assert.Eventually(t,
119+
callback,
120+
150*time.Millisecond,
121+
time.Millisecond,
122+
"expected ping to fail within 150ms")
113123
})
114124

115125
cliOpts = options.Client().ApplyURI("mongodb://invalid/?timeoutMS=200&serverSelectionTimeoutMS=100")
116126
mtOpts = mtest.NewOptions().ClientOptions(cliOpts).CreateCollection(false)
117127
mt.RunOpts("serverSelectionTimeoutMS honored for server selection if it's lower than timeoutMS", mtOpts, func(mt *mtest.T) {
118128
mt.Parallel()
119129

120-
callback := func(ctx context.Context) {
121-
err := mt.Client.Ping(ctx, nil)
122-
assert.NotNil(mt, err, "expected Ping error, got nil")
130+
callback := func() bool {
131+
err := mt.Client.Ping(context.Background(), nil)
132+
assert.Error(mt, err, "expected Ping error, got nil")
133+
return true
123134
}
124135

125136
// Assert that Ping fails within 150ms due to server selection timeout.
126-
assert.Soon(mt, callback, 150*time.Millisecond)
137+
assert.Eventually(t,
138+
callback,
139+
150*time.Millisecond,
140+
time.Millisecond,
141+
"expected ping to fail within 150ms")
127142
})
128143

129144
cliOpts = options.Client().ApplyURI("mongodb://invalid/?timeoutMS=0&serverSelectionTimeoutMS=100")
130145
mtOpts = mtest.NewOptions().ClientOptions(cliOpts).CreateCollection(false)
131146
mt.RunOpts("serverSelectionTimeoutMS honored for server selection if timeoutMS=0", mtOpts, func(mt *mtest.T) {
132147
mt.Parallel()
133148

134-
callback := func(ctx context.Context) {
135-
err := mt.Client.Ping(ctx, nil)
136-
assert.NotNil(mt, err, "expected Ping error, got nil")
149+
callback := func() bool {
150+
err := mt.Client.Ping(context.Background(), nil)
151+
assert.Error(mt, err, "expected Ping error, got nil")
152+
return true
137153
}
138154

139155
// Assert that Ping fails within 150ms due to server selection timeout.
140-
assert.Soon(mt, callback, 150*time.Millisecond)
156+
assert.Eventually(t,
157+
callback,
158+
150*time.Millisecond,
159+
time.Millisecond,
160+
"expected ping to fail within 150ms")
141161
})
142162
})
143163
}

0 commit comments

Comments
 (0)