|
16 | 16 |
|
17 | 17 | from __future__ import annotations |
18 | 18 |
|
| 19 | +import logging |
19 | 20 | import sys |
| 21 | +from unittest.mock import patch |
20 | 22 |
|
21 | 23 | sys.path[0:0] = [""] |
22 | 24 |
|
23 | 25 | import pymongo |
24 | 26 | from bson.codec_options import DEFAULT_CODEC_OPTIONS |
25 | 27 | from pymongo import _op_id |
| 28 | +from pymongo._telemetry import _CommandTelemetry |
| 29 | +from pymongo.asynchronous import mongo_client |
26 | 30 | from pymongo.asynchronous.encryption import _Encrypter |
27 | 31 | from pymongo.asynchronous.helpers import _handle_reauth |
28 | 32 | from pymongo.asynchronous.pool import AsyncConnection |
29 | 33 | from pymongo.errors import OperationFailure |
30 | 34 | from pymongo.helpers_shared import _REAUTHENTICATION_REQUIRED_CODE |
| 35 | +from pymongo.logger import _COMMAND_LOGGER, _SERVER_SELECTION_LOGGER |
31 | 36 | from pymongo.operations import InsertOne |
32 | 37 | from test.asynchronous import AsyncIntegrationTest, async_client_context, unittest |
33 | 38 | from test.utils_shared import AllowListEventListener |
@@ -134,6 +139,49 @@ async def test_retryable_reads_reuse_operation_id(self): |
134 | 139 | with self.subTest(command=name, index=i): |
135 | 140 | await self._check_stable_operation_id(name, f, self.RETRIES) |
136 | 141 |
|
| 142 | + async def test_retry_without_listeners_or_logging_creates_no_operation_id(self): |
| 143 | + appname = _APP_NAME + "noapm" |
| 144 | + client = await self.async_rs_or_single_client(appname=appname) |
| 145 | + |
| 146 | + # Make sure APM and logging are disabled |
| 147 | + for logger in (_COMMAND_LOGGER, _SERVER_SELECTION_LOGGER): |
| 148 | + self.assertFalse(logger.isEnabledFor(logging.DEBUG)) |
| 149 | + self.assertFalse(client._event_listeners.enabled_for_commands) |
| 150 | + |
| 151 | + find_op_ids = [] |
| 152 | + original_init = _CommandTelemetry.__init__ |
| 153 | + |
| 154 | + def recording_init(self, topology_id, conn, listeners, cmd, dbname, request_id, op_id): |
| 155 | + if next(iter(cmd)) == "find": |
| 156 | + find_op_ids.append(op_id) |
| 157 | + original_init(self, topology_id, conn, listeners, cmd, dbname, request_id, op_id) |
| 158 | + |
| 159 | + fail_point = { |
| 160 | + "mode": {"times": 1}, |
| 161 | + "data": { |
| 162 | + "failCommands": ["find"], |
| 163 | + "closeConnection": True, |
| 164 | + "appName": appname, |
| 165 | + }, |
| 166 | + } |
| 167 | + async with self.fail_point(fail_point): |
| 168 | + with ( |
| 169 | + patch.object(mongo_client, "_randint") as randint, |
| 170 | + patch.object(_CommandTelemetry, "__init__", recording_init), |
| 171 | + ): |
| 172 | + self.assertIsNotNone( |
| 173 | + await client.pymongo_test.test_operation_id_retry.find_one({"_id": 1}) |
| 174 | + ) |
| 175 | + |
| 176 | + self.assertEqual( |
| 177 | + randint.call_count, 0, "generated an operation id without APM/logging enabled" |
| 178 | + ) |
| 179 | + self.assertEqual( |
| 180 | + find_op_ids, |
| 181 | + [None, None], |
| 182 | + "expected two attempts, neither carrying a shared operation id", |
| 183 | + ) |
| 184 | + |
137 | 185 | async def test_reauth_does_not_reuse_operation_id(self): |
138 | 186 | class FakeConnection(AsyncConnection): |
139 | 187 | def __init__(self): |
|
0 commit comments