Skip to content

Commit 7584831

Browse files
committed
PYTHON-5071 Fix configureFailPoint retry
1 parent ad9f557 commit 7584831

File tree

7 files changed

+43
-60
lines changed

7 files changed

+43
-60
lines changed

test/__init__.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -933,25 +933,29 @@ def assertEqualCommand(self, expected, actual, msg=None):
933933
def assertEqualReply(self, expected, actual, msg=None):
934934
self.assertEqual(sanitize_reply(expected), sanitize_reply(actual), msg)
935935

936+
@staticmethod
937+
def configure_fail_point(client, command_args):
938+
cmd = {"configureFailPoint": "failCommand"}
939+
cmd.update(command_args)
940+
try:
941+
client.admin.command(cmd)
942+
return
943+
except pymongo.errors.ConnectionFailure:
944+
# Workaround PyPy bug described in PYTHON-5011.
945+
if not _IS_SYNC and "PyPy" in sys.version:
946+
client.admin.command(cmd)
947+
return
948+
raise
949+
936950
@contextmanager
937951
def fail_point(self, command_args):
938-
cmd_on = SON([("configureFailPoint", "failCommand")])
939-
cmd_on.update(command_args)
940-
client_context.client.admin.command(cmd_on)
952+
name = command_args.get("configureFailPoint", "failCommand")
953+
self.configure_fail_point(client_context.client, command_args)
941954
try:
942955
yield
943956
finally:
944-
try:
945-
client_context.client.admin.command(
946-
"configureFailPoint", cmd_on["configureFailPoint"], mode="off"
947-
)
948-
except pymongo.errors.ConnectionFailure:
949-
# Workaround PyPy bug described in PYTHON-5011.
950-
if not _IS_SYNC and "PyPy" in sys.version:
951-
client_context.client.admin.command(
952-
"configureFailPoint", cmd_on["configureFailPoint"], mode="off"
953-
)
954-
raise
957+
cmd_off = {"configureFailPoint": name, "mode": "off"}
958+
self.configure_fail_point(client_context.client, cmd_off)
955959

956960
@contextmanager
957961
def fork(

test/asynchronous/__init__.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -935,25 +935,29 @@ def assertEqualCommand(self, expected, actual, msg=None):
935935
def assertEqualReply(self, expected, actual, msg=None):
936936
self.assertEqual(sanitize_reply(expected), sanitize_reply(actual), msg)
937937

938+
@staticmethod
939+
async def configure_fail_point(client, command_args):
940+
cmd = {"configureFailPoint": "failCommand"}
941+
cmd.update(command_args)
942+
try:
943+
await client.admin.command(cmd)
944+
return
945+
except pymongo.errors.ConnectionFailure:
946+
# Workaround PyPy bug described in PYTHON-5011.
947+
if not _IS_SYNC and "PyPy" in sys.version:
948+
await client.admin.command(cmd)
949+
return
950+
raise
951+
938952
@asynccontextmanager
939953
async def fail_point(self, command_args):
940-
cmd_on = SON([("configureFailPoint", "failCommand")])
941-
cmd_on.update(command_args)
942-
await async_client_context.client.admin.command(cmd_on)
954+
name = command_args.get("configureFailPoint", "failCommand")
955+
await self.configure_fail_point(async_client_context.client, command_args)
943956
try:
944957
yield
945958
finally:
946-
try:
947-
await async_client_context.client.admin.command(
948-
"configureFailPoint", cmd_on["configureFailPoint"], mode="off"
949-
)
950-
except pymongo.errors.ConnectionFailure:
951-
# Workaround PyPy bug described in PYTHON-5011.
952-
if not _IS_SYNC and "PyPy" in sys.version:
953-
await async_client_context.client.admin.command(
954-
"configureFailPoint", cmd_on["configureFailPoint"], mode="off"
955-
)
956-
raise
959+
cmd_off = {"configureFailPoint": name, "mode": "off"}
960+
await self.configure_fail_point(async_client_context.client, cmd_off)
957961

958962
@contextmanager
959963
def fork(

test/asynchronous/test_transactions.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,10 @@ async def asyncSetUp(self) -> None:
410410
for address in async_client_context.mongoses:
411411
self.mongos_clients.append(await self.async_single_client("{}:{}".format(*address)))
412412

413-
async def _set_fail_point(self, client, command_args):
414-
cmd = {"configureFailPoint": "failCommand"}
415-
cmd.update(command_args)
416-
await client.admin.command(cmd)
417-
418413
async def set_fail_point(self, command_args):
419414
clients = self.mongos_clients if self.mongos_clients else [self.client]
420415
for client in clients:
421-
await self._set_fail_point(client, command_args)
416+
await self.configure_fail_point(client, command_args)
422417

423418
@async_client_context.require_transactions
424419
async def test_callback_raises_custom_error(self):

test/asynchronous/utils_spec_runner.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,15 +265,10 @@ async def asyncSetUp(self) -> None:
265265
async def asyncTearDown(self) -> None:
266266
self.knobs.disable()
267267

268-
async def _set_fail_point(self, client, command_args):
269-
cmd = SON([("configureFailPoint", "failCommand")])
270-
cmd.update(command_args)
271-
await client.admin.command(cmd)
272-
273268
async def set_fail_point(self, command_args):
274269
clients = self.mongos_clients if self.mongos_clients else [self.client]
275270
for client in clients:
276-
await self._set_fail_point(client, command_args)
271+
await self.configure_fail_point(client, command_args)
277272

278273
async def targeted_fail_point(self, session, fail_point):
279274
"""Run the targetedFailPoint test operation.
@@ -282,7 +277,7 @@ async def targeted_fail_point(self, session, fail_point):
282277
"""
283278
clients = {c.address: c for c in self.mongos_clients}
284279
client = clients[session._pinned_address]
285-
await self._set_fail_point(client, fail_point)
280+
await self.configure_fail_point(client, fail_point)
286281
self.addAsyncCleanup(self.set_fail_point, {"mode": "off"})
287282

288283
def assert_session_pinned(self, session):

test/test_connection_monitoring.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,10 @@ def check_error(self, actual, expected):
204204
self.check_object(actual, expected)
205205
self.assertIn(message, str(actual))
206206

207-
def _set_fail_point(self, client, command_args):
208-
cmd = SON([("configureFailPoint", "failCommand")])
209-
cmd.update(command_args)
210-
client.admin.command(cmd)
211-
212207
def set_fail_point(self, command_args):
213208
if not client_context.supports_failCommand_fail_point:
214209
self.skipTest("failCommand fail point must be supported")
215-
self._set_fail_point(self.client, command_args)
210+
self.configure_fail_point(self.client, command_args)
216211

217212
def run_scenario(self, scenario_def, test):
218213
"""Run a CMAP spec test."""

test/test_transactions.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,15 +402,10 @@ def setUp(self) -> None:
402402
for address in client_context.mongoses:
403403
self.mongos_clients.append(self.single_client("{}:{}".format(*address)))
404404

405-
def _set_fail_point(self, client, command_args):
406-
cmd = {"configureFailPoint": "failCommand"}
407-
cmd.update(command_args)
408-
client.admin.command(cmd)
409-
410405
def set_fail_point(self, command_args):
411406
clients = self.mongos_clients if self.mongos_clients else [self.client]
412407
for client in clients:
413-
self._set_fail_point(client, command_args)
408+
self.configure_fail_point(client, command_args)
414409

415410
@client_context.require_transactions
416411
def test_callback_raises_custom_error(self):

test/utils_spec_runner.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,15 +265,10 @@ def setUp(self) -> None:
265265
def tearDown(self) -> None:
266266
self.knobs.disable()
267267

268-
def _set_fail_point(self, client, command_args):
269-
cmd = SON([("configureFailPoint", "failCommand")])
270-
cmd.update(command_args)
271-
client.admin.command(cmd)
272-
273268
def set_fail_point(self, command_args):
274269
clients = self.mongos_clients if self.mongos_clients else [self.client]
275270
for client in clients:
276-
self._set_fail_point(client, command_args)
271+
self.configure_fail_point(client, command_args)
277272

278273
def targeted_fail_point(self, session, fail_point):
279274
"""Run the targetedFailPoint test operation.
@@ -282,7 +277,7 @@ def targeted_fail_point(self, session, fail_point):
282277
"""
283278
clients = {c.address: c for c in self.mongos_clients}
284279
client = clients[session._pinned_address]
285-
self._set_fail_point(client, fail_point)
280+
self.configure_fail_point(client, fail_point)
286281
self.addCleanup(self.set_fail_point, {"mode": "off"})
287282

288283
def assert_session_pinned(self, session):

0 commit comments

Comments
 (0)