Skip to content

Commit ee1541c

Browse files
authored
PYTHON-1772 Fix flakey mock timeout tests for with_transaction (mongodb#415)
1 parent 82131c4 commit ee1541c

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

test/test_transactions.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -498,28 +498,18 @@ def test_unpin_for_non_transaction_operation(self):
498498
self.assertGreater(len(addresses), 1)
499499

500500

501-
class PatchMockTimer(object):
502-
"""Patches the client_session module's monotonic time to test timeouts."""
503-
def __init__(self):
504-
self.monotonic_time = None
505-
self.counter = 0
506-
507-
def time(self):
508-
time = self.monotonic_time()
509-
if self.counter > 0:
510-
time += client_session._WITH_TRANSACTION_RETRY_TIME_LIMIT
511-
self.counter += 1
512-
return time
501+
class PatchSessionTimeout(object):
502+
"""Patches the client_session's with_transaction timeout for testing."""
503+
def __init__(self, mock_timeout):
504+
self.real_timeout = client_session._WITH_TRANSACTION_RETRY_TIME_LIMIT
505+
self.mock_timeout = mock_timeout
513506

514507
def __enter__(self):
515-
self.monotonic_time = client_session.monotonic.time
516-
self.counter = 0
517-
client_session.monotonic.time = self.time
508+
client_session._WITH_TRANSACTION_RETRY_TIME_LIMIT = self.mock_timeout
518509
return self
519510

520511
def __exit__(self, exc_type, exc_val, exc_tb):
521-
if self.monotonic_time:
522-
client_session.monotonic.time = self.monotonic_time
512+
client_session._WITH_TRANSACTION_RETRY_TIME_LIMIT = self.real_timeout
523513

524514

525515
class TestTransactionsConvenientAPI(TransactionsBase):
@@ -572,7 +562,7 @@ def callback(session):
572562
coll.insert_one({})
573563
listener.results.clear()
574564
with client.start_session() as s:
575-
with PatchMockTimer():
565+
with PatchSessionTimeout(0):
576566
with self.assertRaises(OperationFailure):
577567
s.with_transaction(callback)
578568

@@ -601,7 +591,7 @@ def callback(session):
601591
listener.results.clear()
602592

603593
with client.start_session() as s:
604-
with PatchMockTimer():
594+
with PatchSessionTimeout(0):
605595
with self.assertRaises(OperationFailure):
606596
s.with_transaction(callback)
607597

@@ -629,7 +619,7 @@ def callback(session):
629619
listener.results.clear()
630620

631621
with client.start_session() as s:
632-
with PatchMockTimer():
622+
with PatchSessionTimeout(0):
633623
with self.assertRaises(ConnectionFailure):
634624
s.with_transaction(callback)
635625

0 commit comments

Comments
 (0)