@@ -85,7 +85,23 @@ func TestSDAMErrorHandling(t *testing.T) {
85
85
assert .NotNil (mt , err , "expected InsertOne error, got nil" )
86
86
assert .True (mt , mongo .IsTimeout (err ), "expected timeout error, got %v" , err )
87
87
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 )
89
105
})
90
106
91
107
mt .RunOpts ("pool cleared on non-timeout network error" , noClientOpts , func (mt * mtest.T ) {
@@ -114,9 +130,23 @@ func TestSDAMErrorHandling(t *testing.T) {
114
130
// Set minPoolSize to enable the background pool maintenance goroutine.
115
131
SetMinPoolSize (5 ))
116
132
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 )
120
150
})
121
151
122
152
mt .Run ("foreground" , func (mt * mtest.T ) {
@@ -143,7 +173,24 @@ func TestSDAMErrorHandling(t *testing.T) {
143
173
_ , err := mt .Coll .InsertOne (context .Background (), bson.D {{"x" , 1 }})
144
174
assert .NotNil (mt , err , "expected InsertOne error, got nil" )
145
175
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 )
147
194
})
148
195
})
149
196
})
0 commit comments