Skip to content

Commit fe21d09

Browse files
committed
change test
1 parent 8341b71 commit fe21d09

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

pymongo/asynchronous/client_session.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,10 +708,12 @@ async def callback(session, custom_arg, custom_kwarg=None):
708708
"""
709709
start_time = time.monotonic()
710710
retry = 0
711+
self._transaction_retry_backoffs = []
711712
while True:
712713
if retry: # Implement exponential backoff on retry.
713714
jitter = random.random() # noqa: S311
714715
backoff = jitter * min(_BACKOFF_INITIAL * (1.25**retry), _BACKOFF_MAX)
716+
self._transaction_retry_backoffs.append(backoff)
715717
await asyncio.sleep(backoff)
716718
retry += 1
717719
await self.start_transaction(

test/asynchronous/test_transactions.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -623,11 +623,15 @@ async def callback(session):
623623
async def test_transaction_backoff(self):
624624
client = async_client_context.client
625625
coll = client[self.db.name].test
626+
# optionally set _backoff_initial to a higher value
627+
_set_backoff_initial(client_session._BACKOFF_MAX)
626628
# set fail point to trigger transaction failure and trigger backoff
627629
await self.set_fail_point(
628630
{
629631
"configureFailPoint": "failCommand",
630-
"mode": {"times": 3},
632+
"mode": {
633+
"times": 30
634+
}, # sufficiently high enough such that the time effect of backoff is noticeable
631635
"data": {
632636
"failCommands": ["commitTransaction"],
633637
"errorCode": 24,
@@ -643,16 +647,11 @@ async def test_transaction_backoff(self):
643647
async def callback(session):
644648
await coll.insert_one({}, session=session)
645649

646-
total_backoff = 0
647650
async with self.client.start_session() as s:
648651
await s.with_transaction(callback)
649-
self.assertEqual(len(s._transaction_retry_backoffs), 3)
650-
for backoff in s._transaction_retry_backoffs:
651-
self.assertGreater(backoff, 0)
652-
total_backoff += backoff
653652

654653
end = time.monotonic()
655-
self.assertGreaterEqual(end - start, total_backoff)
654+
self.assertGreaterEqual(end - start, 1.25) # 1 second
656655

657656

658657
class TestOptionsInsideTransactionProse(AsyncTransactionsBase):

test/test_transactions.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -611,11 +611,15 @@ def callback(session):
611611
def test_transaction_backoff(self):
612612
client = client_context.client
613613
coll = client[self.db.name].test
614+
# optionally set _backoff_initial to a higher value
615+
_set_backoff_initial(client_session._BACKOFF_MAX)
614616
# set fail point to trigger transaction failure and trigger backoff
615617
self.set_fail_point(
616618
{
617619
"configureFailPoint": "failCommand",
618-
"mode": {"times": 3},
620+
"mode": {
621+
"times": 30
622+
}, # sufficiently high enough such that the time effect of backoff is noticeable
619623
"data": {
620624
"failCommands": ["commitTransaction"],
621625
"errorCode": 24,
@@ -629,16 +633,11 @@ def test_transaction_backoff(self):
629633
def callback(session):
630634
coll.insert_one({}, session=session)
631635

632-
total_backoff = 0
633636
with self.client.start_session() as s:
634637
s.with_transaction(callback)
635-
self.assertEqual(len(s._transaction_retry_backoffs), 3)
636-
for backoff in s._transaction_retry_backoffs:
637-
self.assertGreater(backoff, 0)
638-
total_backoff += backoff
639638

640639
end = time.monotonic()
641-
self.assertGreaterEqual(end - start, total_backoff)
640+
self.assertGreaterEqual(end - start, 1.25) # 1 second
642641

643642

644643
class TestOptionsInsideTransactionProse(TransactionsBase):

0 commit comments

Comments
 (0)