|
21 | 21 | import sys |
22 | 22 | import threading |
23 | 23 | from test.asynchronous.utils import async_set_fail_point, flaky |
| 24 | +from unittest import mock |
24 | 25 |
|
25 | 26 | from pymongo.common import MAX_ADAPTIVE_RETRIES |
26 | 27 |
|
@@ -835,6 +836,62 @@ def failed(event: CommandFailedEvent) -> None: |
835 | 836 | started_inserts = [e for e in listener.started_events if e.command_name == "insert"] |
836 | 837 | self.assertEqual(len(started_inserts), MAX_ADAPTIVE_RETRIES + 1) |
837 | 838 |
|
| 839 | + async def test_backoff_is_not_applied_for_non_overload_errors(self): |
| 840 | + if _IS_SYNC: |
| 841 | + mock_target = "pymongo.synchronous.helpers._RetryPolicy.backoff" |
| 842 | + else: |
| 843 | + mock_target = "pymongo.asynchronous.helpers._RetryPolicy.backoff" |
| 844 | + |
| 845 | + # Create a client. |
| 846 | + listener = OvertCommandListener() |
| 847 | + |
| 848 | + # Configure the client to listen to CommandFailedEvents. In the attached listener, configure a fail point with error |
| 849 | + # code `91` (ShutdownInProgress) and `RetryableError` and `SystemOverloadedError` labels. |
| 850 | + overload_fail_point = { |
| 851 | + "configureFailPoint": "failCommand", |
| 852 | + "mode": {"times": 1}, |
| 853 | + "data": { |
| 854 | + "failCommands": ["insert"], |
| 855 | + "errorLabels": ["RetryableError", "SystemOverloadedError"], |
| 856 | + "errorCode": 91, |
| 857 | + }, |
| 858 | + } |
| 859 | + |
| 860 | + # Configure a fail point with error code `91` (ShutdownInProgress) with only the `RetryableError` error label. |
| 861 | + non_overload_fail_point = { |
| 862 | + "configureFailPoint": "failCommand", |
| 863 | + "mode": "alwaysOn", |
| 864 | + "data": { |
| 865 | + "failCommands": ["insert"], |
| 866 | + "errorCode": 91, |
| 867 | + "errorLabels": ["RetryableError", "RetryableWriteError"], |
| 868 | + }, |
| 869 | + } |
| 870 | + |
| 871 | + def failed(event: CommandFailedEvent) -> None: |
| 872 | + # Configure the fail point command only if the failed event is for the 91 error configured in step 2. |
| 873 | + if listener.failed_events: |
| 874 | + return |
| 875 | + assert event.failure["code"] == 91 |
| 876 | + self.configure_fail_point_sync(non_overload_fail_point) |
| 877 | + self.addCleanup(self.configure_fail_point_sync, {}, off=True) |
| 878 | + listener.failed_events.append(event) |
| 879 | + |
| 880 | + listener.failed = failed |
| 881 | + |
| 882 | + client = await self.async_rs_client(event_listeners=[listener]) |
| 883 | + |
| 884 | + self.configure_fail_point_sync(overload_fail_point) |
| 885 | + self.addCleanup(self.configure_fail_point_sync, {}, off=True) |
| 886 | + |
| 887 | + # Perform a findOne operation with coll. Expect the operation to fail. |
| 888 | + with mock.patch(mock_target, return_value=0) as mock_backoff: |
| 889 | + with self.assertRaises(PyMongoError): |
| 890 | + await client.test.test.insert_one({}) |
| 891 | + |
| 892 | + # Assert that backoff was applied only once for the initial overload error and not for the subsequent non-overload retryable errors. |
| 893 | + self.assertEqual(mock_backoff.call_count, 1) |
| 894 | + |
838 | 895 |
|
839 | 896 | if __name__ == "__main__": |
840 | 897 | unittest.main() |
0 commit comments