|
21 | 21 | import threading |
22 | 22 | from test.asynchronous.utils import async_set_fail_point |
23 | 23 |
|
24 | | -from pymongo.errors import OperationFailure |
| 24 | +from pymongo import AsyncMongoClient, MongoClient |
| 25 | +from pymongo.common import MAX_ADAPTIVE_RETRIES |
| 26 | +from pymongo.errors import OperationFailure, PyMongoError |
25 | 27 |
|
26 | 28 | sys.path[0:0] = [""] |
27 | 29 |
|
|
38 | 40 | ) |
39 | 41 |
|
40 | 42 | from pymongo.monitoring import ( |
| 43 | + CommandFailedEvent, |
41 | 44 | ConnectionCheckedOutEvent, |
42 | 45 | ConnectionCheckOutFailedEvent, |
43 | 46 | ConnectionCheckOutFailedReason, |
@@ -145,6 +148,20 @@ async def test_pool_paused_error_is_retryable(self): |
145 | 148 |
|
146 | 149 |
|
147 | 150 | class TestRetryableReads(AsyncIntegrationTest): |
| 151 | + async def asyncSetUp(self) -> None: |
| 152 | + await super().asyncSetUp() |
| 153 | + self.setup_client = MongoClient(**async_client_context.default_client_options) |
| 154 | + self.addCleanup(self.setup_client.close) |
| 155 | + |
| 156 | + # TODO: After PYTHON-4595 we can use async event handlers and remove this workaround. |
| 157 | + def configure_fail_point_sync(self, command_args, off=False) -> None: |
| 158 | + cmd = {"configureFailPoint": "failCommand"} |
| 159 | + cmd.update(command_args) |
| 160 | + if off: |
| 161 | + cmd["mode"] = "off" |
| 162 | + cmd.pop("data", None) |
| 163 | + self.setup_client.admin.command(cmd) |
| 164 | + |
148 | 165 | @async_client_context.require_multiple_mongoses |
149 | 166 | @async_client_context.require_failCommand_fail_point |
150 | 167 | async def test_retryable_reads_are_retried_on_a_different_mongos_when_one_is_available(self): |
@@ -383,6 +400,58 @@ async def test_03_03_retryable_reads_caused_by_overload_errors_are_retried_on_th |
383 | 400 | # 6. Assert that both events occurred on the same server. |
384 | 401 | assert listener.failed_events[0].connection_id == listener.succeeded_events[0].connection_id |
385 | 402 |
|
| 403 | + @async_client_context.require_failCommand_fail_point |
| 404 | + @async_client_context.require_version_min(4, 4, 0) |
| 405 | + async def test_overload_then_nonoverload_retries_increased_reads(self) -> None: |
| 406 | + # Create a client. |
| 407 | + listener = OvertCommandListener() |
| 408 | + |
| 409 | + # Configure the client to listen to CommandFailedEvents. In the attached listener, configure a fail point with error |
| 410 | + # code `91` (NotWritablePrimary) and `RetryableError` and `SystemOverloadedError` labels. |
| 411 | + overload_fail_point = { |
| 412 | + "configureFailPoint": "failCommand", |
| 413 | + "mode": {"times": 1}, |
| 414 | + "data": { |
| 415 | + "failCommands": ["find"], |
| 416 | + "errorLabels": ["RetryableError", "SystemOverloadedError"], |
| 417 | + "errorCode": 91, |
| 418 | + }, |
| 419 | + } |
| 420 | + |
| 421 | + # Configure a fail point with error code `91` (ShutdownInProgress) with only the `RetryableError` error label. |
| 422 | + non_overload_fail_point = { |
| 423 | + "configureFailPoint": "failCommand", |
| 424 | + "mode": "alwaysOn", |
| 425 | + "data": { |
| 426 | + "failCommands": ["find"], |
| 427 | + "errorCode": 91, |
| 428 | + "errorLabels": ["RetryableError"], |
| 429 | + }, |
| 430 | + } |
| 431 | + |
| 432 | + def failed(event: CommandFailedEvent) -> None: |
| 433 | + # Configure the fail point command only if the failed event is for the 91 error configured in step 2. |
| 434 | + if listener.failed_events: |
| 435 | + return |
| 436 | + assert event.failure["code"] == 91 |
| 437 | + self.configure_fail_point_sync(non_overload_fail_point) |
| 438 | + self.addCleanup(self.configure_fail_point_sync, {}, off=True) |
| 439 | + listener.failed_events.append(event) |
| 440 | + |
| 441 | + listener.failed = failed |
| 442 | + |
| 443 | + client = await self.async_rs_client(event_listeners=[listener]) |
| 444 | + await client.test.test.insert_one({}) |
| 445 | + |
| 446 | + self.configure_fail_point_sync(overload_fail_point) |
| 447 | + self.addCleanup(self.configure_fail_point_sync, {}, off=True) |
| 448 | + |
| 449 | + with self.assertRaises(PyMongoError): |
| 450 | + await client.test.test.find_one() |
| 451 | + |
| 452 | + started_finds = [e for e in listener.started_events if e.command_name == "find"] |
| 453 | + self.assertEqual(len(started_finds), MAX_ADAPTIVE_RETRIES + 1) |
| 454 | + |
386 | 455 |
|
387 | 456 | if __name__ == "__main__": |
388 | 457 | unittest.main() |
0 commit comments