Skip to content

Commit 2b667df

Browse files
authored
PYTHON-5120 Reduce configureFailPoint duplication in tests (#2131)
1 parent eaae22c commit 2b667df

10 files changed

+34
-62
lines changed

test/__init__.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -933,17 +933,22 @@ 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, off=False):
938+
cmd = {"configureFailPoint": "failCommand"}
939+
cmd.update(command_args)
940+
if off:
941+
cmd["mode"] = "off"
942+
cmd.pop("data", None)
943+
client.admin.command(cmd)
944+
936945
@contextmanager
937946
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)
947+
self.configure_fail_point(client_context.client, command_args)
941948
try:
942949
yield
943950
finally:
944-
client_context.client.admin.command(
945-
"configureFailPoint", cmd_on["configureFailPoint"], mode="off"
946-
)
951+
self.configure_fail_point(client_context.client, command_args, off=True)
947952

948953
@contextmanager
949954
def fork(

test/asynchronous/__init__.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -935,17 +935,22 @@ 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, off=False):
940+
cmd = {"configureFailPoint": "failCommand"}
941+
cmd.update(command_args)
942+
if off:
943+
cmd["mode"] = "off"
944+
cmd.pop("data", None)
945+
await client.admin.command(cmd)
946+
938947
@asynccontextmanager
939948
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)
949+
await self.configure_fail_point(async_client_context.client, command_args)
943950
try:
944951
yield
945952
finally:
946-
await async_client_context.client.admin.command(
947-
"configureFailPoint", cmd_on["configureFailPoint"], mode="off"
948-
)
953+
await self.configure_fail_point(async_client_context.client, command_args, off=True)
949954

950955
@contextmanager
951956
def fork(

test/asynchronous/test_connection_monitoring.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,10 @@ def check_error(self, actual, expected):
211211
self.check_object(actual, expected)
212212
self.assertIn(message, str(actual))
213213

214-
async def _set_fail_point(self, client, command_args):
215-
cmd = SON([("configureFailPoint", "failCommand")])
216-
cmd.update(command_args)
217-
await client.admin.command(cmd)
218-
219214
async def set_fail_point(self, command_args):
220215
if not async_client_context.supports_failCommand_fail_point:
221216
self.skipTest("failCommand fail point must be supported")
222-
await self._set_fail_point(self.client, command_args)
217+
await self.configure_fail_point(self.client, command_args)
223218

224219
async def run_scenario(self, scenario_def, test):
225220
"""Run a CMAP spec test."""

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/unified_format.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,12 +1008,8 @@ async def __set_fail_point(self, client, command_args):
10081008
if not async_client_context.test_commands_enabled:
10091009
self.skipTest("Test commands must be enabled")
10101010

1011-
cmd_on = SON([("configureFailPoint", "failCommand")])
1012-
cmd_on.update(command_args)
1013-
await client.admin.command(cmd_on)
1014-
self.addAsyncCleanup(
1015-
client.admin.command, "configureFailPoint", cmd_on["configureFailPoint"], mode="off"
1016-
)
1011+
await self.configure_fail_point(client, command_args)
1012+
self.addAsyncCleanup(self.configure_fail_point, client, command_args, off=True)
10171013

10181014
async def _testOperation_failPoint(self, spec):
10191015
await self.__set_fail_point(

test/asynchronous/utils_spec_runner.py

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

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

277272
async def targeted_fail_point(self, session, fail_point):
278273
"""Run the targetedFailPoint test operation.
@@ -281,7 +276,7 @@ async def targeted_fail_point(self, session, fail_point):
281276
"""
282277
clients = {c.address: c for c in self.mongos_clients}
283278
client = clients[session._pinned_address]
284-
await self._set_fail_point(client, fail_point)
279+
await self.configure_fail_point(client, fail_point)
285280
self.addAsyncCleanup(self.set_fail_point, {"mode": "off"})
286281

287282
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
@@ -211,15 +211,10 @@ def check_error(self, actual, expected):
211211
self.check_object(actual, expected)
212212
self.assertIn(message, str(actual))
213213

214-
def _set_fail_point(self, client, command_args):
215-
cmd = SON([("configureFailPoint", "failCommand")])
216-
cmd.update(command_args)
217-
client.admin.command(cmd)
218-
219214
def set_fail_point(self, command_args):
220215
if not client_context.supports_failCommand_fail_point:
221216
self.skipTest("failCommand fail point must be supported")
222-
self._set_fail_point(self.client, command_args)
217+
self.configure_fail_point(self.client, command_args)
223218

224219
def run_scenario(self, scenario_def, test):
225220
"""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/unified_format.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -999,12 +999,8 @@ def __set_fail_point(self, client, command_args):
999999
if not client_context.test_commands_enabled:
10001000
self.skipTest("Test commands must be enabled")
10011001

1002-
cmd_on = SON([("configureFailPoint", "failCommand")])
1003-
cmd_on.update(command_args)
1004-
client.admin.command(cmd_on)
1005-
self.addCleanup(
1006-
client.admin.command, "configureFailPoint", cmd_on["configureFailPoint"], mode="off"
1007-
)
1002+
self.configure_fail_point(client, command_args)
1003+
self.addCleanup(self.configure_fail_point, client, command_args, off=True)
10081004

10091005
def _testOperation_failPoint(self, spec):
10101006
self.__set_fail_point(

test/utils_spec_runner.py

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

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

277272
def targeted_fail_point(self, session, fail_point):
278273
"""Run the targetedFailPoint test operation.
@@ -281,7 +276,7 @@ def targeted_fail_point(self, session, fail_point):
281276
"""
282277
clients = {c.address: c for c in self.mongos_clients}
283278
client = clients[session._pinned_address]
284-
self._set_fail_point(client, fail_point)
279+
self.configure_fail_point(client, fail_point)
285280
self.addCleanup(self.set_fail_point, {"mode": "off"})
286281

287282
def assert_session_pinned(self, session):

0 commit comments

Comments
 (0)