21
21
import threading
22
22
from test .asynchronous .utils import async_set_fail_point
23
23
24
- from pymongo .errors import AutoReconnect
24
+ from pymongo .errors import OperationFailure
25
25
26
26
sys .path [0 :0 ] = ["" ]
27
27
@@ -147,15 +147,11 @@ async def test_pool_paused_error_is_retryable(self):
147
147
class TestRetryableReads (AsyncIntegrationTest ):
148
148
@async_client_context .require_multiple_mongoses
149
149
@async_client_context .require_failCommand_fail_point
150
- async def test_retryable_reads_in_sharded_cluster_multiple_available (self ):
150
+ async def test_retryable_reads_are_retried_on_a_different_mongos_when_one_is_available (self ):
151
151
fail_command = {
152
152
"configureFailPoint" : "failCommand" ,
153
153
"mode" : {"times" : 1 },
154
- "data" : {
155
- "failCommands" : ["find" ],
156
- "closeConnection" : True ,
157
- "appName" : "retryableReadTest" ,
158
- },
154
+ "data" : {"failCommands" : ["find" ], "errorCode" : 6 },
159
155
}
160
156
161
157
mongos_clients = []
@@ -168,12 +164,11 @@ async def test_retryable_reads_in_sharded_cluster_multiple_available(self):
168
164
listener = OvertCommandListener ()
169
165
client = await self .async_rs_or_single_client (
170
166
async_client_context .mongos_seeds (),
171
- appName = "retryableReadTest" ,
172
167
event_listeners = [listener ],
173
168
retryReads = True ,
174
169
)
175
170
176
- with self .assertRaises (AutoReconnect ):
171
+ with self .assertRaises (OperationFailure ):
177
172
await client .t .t .find_one ({})
178
173
179
174
# Disable failpoints on each mongos
@@ -184,6 +179,45 @@ async def test_retryable_reads_in_sharded_cluster_multiple_available(self):
184
179
self .assertEqual (len (listener .failed_events ), 2 )
185
180
self .assertEqual (len (listener .succeeded_events ), 0 )
186
181
182
+ # Assert that both events occurred on different mongos.
183
+ assert listener .failed_events [0 ].connection_id != listener .failed_events [1 ].connection_id
184
+
185
+ @async_client_context .require_multiple_mongoses
186
+ @async_client_context .require_failCommand_fail_point
187
+ async def test_retryable_reads_are_retried_on_the_same_mongos_when_no_others_are_available (
188
+ self
189
+ ):
190
+ fail_command = {
191
+ "configureFailPoint" : "failCommand" ,
192
+ "mode" : {"times" : 1 },
193
+ "data" : {"failCommands" : ["find" ], "errorCode" : 6 },
194
+ }
195
+
196
+ host = async_client_context .mongos_seeds ().split ("," )[0 ]
197
+ mongos_client = await self .async_rs_or_single_client (host )
198
+ await async_set_fail_point (mongos_client , fail_command )
199
+
200
+ listener = OvertCommandListener ()
201
+ client = await self .async_rs_or_single_client (
202
+ host ,
203
+ directConnection = False ,
204
+ event_listeners = [listener ],
205
+ retryReads = True ,
206
+ )
207
+
208
+ await client .t .t .find_one ({})
209
+
210
+ # Disable failpoint.
211
+ fail_command ["mode" ] = "off"
212
+ await async_set_fail_point (mongos_client , fail_command )
213
+
214
+ # Assert that exactly one failed command event and one succeeded command event occurred.
215
+ self .assertEqual (len (listener .failed_events ), 1 )
216
+ self .assertEqual (len (listener .succeeded_events ), 1 )
217
+
218
+ # Assert that both events occurred on the same mongos.
219
+ assert listener .succeeded_events [0 ].connection_id == listener .failed_events [0 ].connection_id
220
+
187
221
188
222
if __name__ == "__main__" :
189
223
unittest .main ()
0 commit comments