Skip to content

Commit 9ab5016

Browse files
authored
Improve reliability of SDAM error 'before handshake completes' tests. (#1095)
1 parent 6ca2dfd commit 9ab5016

File tree

1 file changed

+52
-5
lines changed

1 file changed

+52
-5
lines changed

mongo/integration/sdam_error_handling_test.go

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,23 @@ func TestSDAMErrorHandling(t *testing.T) {
8585
assert.NotNil(mt, err, "expected InsertOne error, got nil")
8686
assert.True(mt, mongo.IsTimeout(err), "expected timeout error, got %v", err)
8787
assert.True(mt, mongo.IsNetworkError(err), "expected network error, got %v", err)
88-
assert.True(mt, tpm.IsPoolCleared(), "expected pool to be cleared but was not")
88+
// Assert that the pool is cleared within 2 seconds.
89+
assert.Soon(mt, func(ctx context.Context) {
90+
ticker := time.NewTicker(100 * time.Millisecond)
91+
defer ticker.Stop()
92+
93+
for {
94+
select {
95+
case <-ticker.C:
96+
case <-ctx.Done():
97+
return
98+
}
99+
100+
if tpm.IsPoolCleared() {
101+
return
102+
}
103+
}
104+
}, 2*time.Second)
89105
})
90106

91107
mt.RunOpts("pool cleared on non-timeout network error", noClientOpts, func(mt *mtest.T) {
@@ -114,9 +130,23 @@ func TestSDAMErrorHandling(t *testing.T) {
114130
// Set minPoolSize to enable the background pool maintenance goroutine.
115131
SetMinPoolSize(5))
116132

117-
time.Sleep(200 * time.Millisecond)
118-
119-
assert.True(mt, tpm.IsPoolCleared(), "expected pool to be cleared but was not")
133+
// Assert that the pool is cleared within 2 seconds.
134+
assert.Soon(mt, func(ctx context.Context) {
135+
ticker := time.NewTicker(100 * time.Millisecond)
136+
defer ticker.Stop()
137+
138+
for {
139+
select {
140+
case <-ticker.C:
141+
case <-ctx.Done():
142+
return
143+
}
144+
145+
if tpm.IsPoolCleared() {
146+
return
147+
}
148+
}
149+
}, 2*time.Second)
120150
})
121151

122152
mt.Run("foreground", func(mt *mtest.T) {
@@ -143,7 +173,24 @@ func TestSDAMErrorHandling(t *testing.T) {
143173
_, err := mt.Coll.InsertOne(context.Background(), bson.D{{"x", 1}})
144174
assert.NotNil(mt, err, "expected InsertOne error, got nil")
145175
assert.False(mt, mongo.IsTimeout(err), "expected non-timeout error, got %v", err)
146-
assert.True(mt, tpm.IsPoolCleared(), "expected pool to be cleared but was not")
176+
177+
// Assert that the pool is cleared within 2 seconds.
178+
assert.Soon(mt, func(ctx context.Context) {
179+
ticker := time.NewTicker(100 * time.Millisecond)
180+
defer ticker.Stop()
181+
182+
for {
183+
select {
184+
case <-ticker.C:
185+
case <-ctx.Done():
186+
return
187+
}
188+
189+
if tpm.IsPoolCleared() {
190+
return
191+
}
192+
}
193+
}, 2*time.Second)
147194
})
148195
})
149196
})

0 commit comments

Comments
 (0)