|
20 | 20 | import sys |
21 | 21 | import threading |
22 | 22 | from test.asynchronous.utils import async_set_fail_point |
| 23 | +from unittest import mock |
23 | 24 |
|
24 | 25 | from pymongo import MongoClient |
25 | 26 | from pymongo.common import MAX_ADAPTIVE_RETRIES |
@@ -452,6 +453,64 @@ def failed(event: CommandFailedEvent) -> None: |
452 | 453 | started_finds = [e for e in listener.started_events if e.command_name == "find"] |
453 | 454 | self.assertEqual(len(started_finds), MAX_ADAPTIVE_RETRIES + 1) |
454 | 455 |
|
| 456 | + @async_client_context.require_failCommand_fail_point |
| 457 | + async def test_backoff_is_not_applied_for_non_overload_errors(self): |
| 458 | + if _IS_SYNC: |
| 459 | + mock_target = "pymongo.synchronous.helpers._RetryPolicy.backoff" |
| 460 | + else: |
| 461 | + mock_target = "pymongo.asynchronous.helpers._RetryPolicy.backoff" |
| 462 | + |
| 463 | + # Create a client. |
| 464 | + listener = OvertCommandListener() |
| 465 | + |
| 466 | + # Configure the client to listen to CommandFailedEvents. In the attached listener, configure a fail point with error |
| 467 | + # code `91` (ShutdownInProgress) and `RetryableError` and `SystemOverloadedError` labels. |
| 468 | + overload_fail_point = { |
| 469 | + "configureFailPoint": "failCommand", |
| 470 | + "mode": {"times": 1}, |
| 471 | + "data": { |
| 472 | + "failCommands": ["find"], |
| 473 | + "errorLabels": ["RetryableError", "SystemOverloadedError"], |
| 474 | + "errorCode": 91, |
| 475 | + }, |
| 476 | + } |
| 477 | + |
| 478 | + # Configure a fail point with error code `91` (ShutdownInProgress) with only the `RetryableError` error label. |
| 479 | + non_overload_fail_point = { |
| 480 | + "configureFailPoint": "failCommand", |
| 481 | + "mode": "alwaysOn", |
| 482 | + "data": { |
| 483 | + "failCommands": ["find"], |
| 484 | + "errorCode": 91, |
| 485 | + "errorLabels": ["RetryableError"], |
| 486 | + }, |
| 487 | + } |
| 488 | + |
| 489 | + def failed(event: CommandFailedEvent) -> None: |
| 490 | + # Configure the fail point command only if the failed event is for the 91 error configured in step 2. |
| 491 | + if listener.failed_events: |
| 492 | + return |
| 493 | + assert event.failure["code"] == 91 |
| 494 | + self.configure_fail_point_sync(non_overload_fail_point) |
| 495 | + self.addCleanup(self.configure_fail_point_sync, {}, off=True) |
| 496 | + listener.failed_events.append(event) |
| 497 | + |
| 498 | + listener.failed = failed |
| 499 | + |
| 500 | + client = await self.async_rs_client(event_listeners=[listener]) |
| 501 | + await client.test.test.insert_one({}) |
| 502 | + |
| 503 | + self.configure_fail_point_sync(overload_fail_point) |
| 504 | + self.addCleanup(self.configure_fail_point_sync, {}, off=True) |
| 505 | + |
| 506 | + # Perform a findOne operation with coll. Expect the operation to fail. |
| 507 | + with mock.patch(mock_target, return_value=0) as mock_backoff: |
| 508 | + with self.assertRaises(PyMongoError): |
| 509 | + await client.test.test.find_one() |
| 510 | + |
| 511 | + # Assert that backoff was applied only once for the initial overload error and not for the subsequent non-overload retryable errors. |
| 512 | + self.assertEqual(mock_backoff.call_count, 1) |
| 513 | + |
455 | 514 |
|
456 | 515 | if __name__ == "__main__": |
457 | 516 | unittest.main() |
0 commit comments