Skip to content

Commit 44e7f59

Browse files
committed
change test
1 parent 8b7daff commit 44e7f59

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

pymongo/asynchronous/client_session.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,11 @@ def _max_time_expired_error(exc: PyMongoError) -> bool:
477477
_BACKOFF_INITIAL = 0.001 # 1ms initial backoff
478478

479479

480+
def _set_backoff_initial(seconds: float) -> None:
481+
global _BACKOFF_INITIAL # noqa: PLW0603
482+
_BACKOFF_INITIAL = seconds
483+
484+
480485
def _within_time_limit(start_time: float) -> bool:
481486
"""Are we within the with_transaction retry limit?"""
482487
return time.monotonic() - start_time < _WITH_TRANSACTION_RETRY_TIME_LIMIT
@@ -518,7 +523,6 @@ def __init__(
518523
# Is this an implicitly created session?
519524
self._implicit = implicit
520525
self._transaction = _Transaction(None, client)
521-
self._transaction_retry_backoffs: list[float] = []
522526

523527
async def end_session(self) -> None:
524528
"""Finish this session. If a transaction has started, abort it.
@@ -706,12 +710,10 @@ async def callback(session, custom_arg, custom_kwarg=None):
706710
"""
707711
start_time = time.monotonic()
708712
retry = 0
709-
self._transaction_retry_backoffs = []
710713
while True:
711714
if retry: # Implement exponential backoff on retry.
712715
jitter = random.random() # noqa: S311
713716
backoff = jitter * min(_BACKOFF_INITIAL * (1.25**retry), _BACKOFF_MAX)
714-
self._transaction_retry_backoffs.append(backoff)
715717
await asyncio.sleep(backoff)
716718
retry += 1
717719
await self.start_transaction(

pymongo/synchronous/client_session.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,11 @@ def _max_time_expired_error(exc: PyMongoError) -> bool:
475475
_BACKOFF_INITIAL = 0.001 # 1ms initial backoff
476476

477477

478+
def _set_backoff_initial(seconds: float) -> None:
479+
global _BACKOFF_INITIAL # noqa: PLW0603
480+
_BACKOFF_INITIAL = seconds
481+
482+
478483
def _within_time_limit(start_time: float) -> bool:
479484
"""Are we within the with_transaction retry limit?"""
480485
return time.monotonic() - start_time < _WITH_TRANSACTION_RETRY_TIME_LIMIT
@@ -516,7 +521,6 @@ def __init__(
516521
# Is this an implicitly created session?
517522
self._implicit = implicit
518523
self._transaction = _Transaction(None, client)
519-
self._transaction_retry_backoffs: list[float] = []
520524

521525
def end_session(self) -> None:
522526
"""Finish this session. If a transaction has started, abort it.
@@ -704,12 +708,10 @@ def callback(session, custom_arg, custom_kwarg=None):
704708
"""
705709
start_time = time.monotonic()
706710
retry = 0
707-
self._transaction_retry_backoffs = []
708711
while True:
709712
if retry: # Implement exponential backoff on retry.
710713
jitter = random.random() # noqa: S311
711714
backoff = jitter * min(_BACKOFF_INITIAL * (1.25**retry), _BACKOFF_MAX)
712-
self._transaction_retry_backoffs.append(backoff)
713715
time.sleep(backoff)
714716
retry += 1
715717
self.start_transaction(read_concern, write_concern, read_preference, max_commit_time_ms)

test/asynchronous/test_transactions.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -612,11 +612,15 @@ async def callback(session):
612612
async def test_transaction_backoff(self):
613613
client = async_client_context.client
614614
coll = client[self.db.name].test
615+
# optionally set _backoff_initial to a higher value
616+
_set_backoff_initial(client_session._BACKOFF_MAX)
615617
# set fail point to trigger transaction failure and trigger backoff
616618
await self.set_fail_point(
617619
{
618620
"configureFailPoint": "failCommand",
619-
"mode": {"times": 3},
621+
"mode": {
622+
"times": 30
623+
}, # sufficiently high enough such that the time effect of backoff is noticeable
620624
"data": {
621625
"failCommands": ["commitTransaction"],
622626
"errorCode": 24,
@@ -632,16 +636,11 @@ async def test_transaction_backoff(self):
632636
async def callback(session):
633637
await coll.insert_one({}, session=session)
634638

635-
total_backoff = 0
636639
async with self.client.start_session() as s:
637640
await s.with_transaction(callback)
638-
self.assertEqual(len(s._transaction_retry_backoffs), 3)
639-
for backoff in s._transaction_retry_backoffs:
640-
self.assertGreater(backoff, 0)
641-
total_backoff += backoff
642641

643642
end = time.monotonic()
644-
self.assertGreaterEqual(end - start, total_backoff)
643+
self.assertGreaterEqual(end - start, 1.25) # 1 second
645644

646645

647646
class TestOptionsInsideTransactionProse(AsyncTransactionsBase):

test/test_transactions.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -600,11 +600,15 @@ def callback(session):
600600
def test_transaction_backoff(self):
601601
client = client_context.client
602602
coll = client[self.db.name].test
603+
# optionally set _backoff_initial to a higher value
604+
_set_backoff_initial(client_session._BACKOFF_MAX)
603605
# set fail point to trigger transaction failure and trigger backoff
604606
self.set_fail_point(
605607
{
606608
"configureFailPoint": "failCommand",
607-
"mode": {"times": 3},
609+
"mode": {
610+
"times": 30
611+
}, # sufficiently high enough such that the time effect of backoff is noticeable
608612
"data": {
609613
"failCommands": ["commitTransaction"],
610614
"errorCode": 24,
@@ -618,16 +622,11 @@ def test_transaction_backoff(self):
618622
def callback(session):
619623
coll.insert_one({}, session=session)
620624

621-
total_backoff = 0
622625
with self.client.start_session() as s:
623626
s.with_transaction(callback)
624-
self.assertEqual(len(s._transaction_retry_backoffs), 3)
625-
for backoff in s._transaction_retry_backoffs:
626-
self.assertGreater(backoff, 0)
627-
total_backoff += backoff
628627

629628
end = time.monotonic()
630-
self.assertGreaterEqual(end - start, total_backoff)
629+
self.assertGreaterEqual(end - start, 1.25) # 1 second
631630

632631

633632
class TestOptionsInsideTransactionProse(TransactionsBase):

0 commit comments

Comments
 (0)