@@ -498,28 +498,18 @@ def test_unpin_for_non_transaction_operation(self):
498
498
self .assertGreater (len (addresses ), 1 )
499
499
500
500
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
513
506
514
507
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
518
509
return self
519
510
520
511
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
523
513
524
514
525
515
class TestTransactionsConvenientAPI (TransactionsBase ):
@@ -572,7 +562,7 @@ def callback(session):
572
562
coll .insert_one ({})
573
563
listener .results .clear ()
574
564
with client .start_session () as s :
575
- with PatchMockTimer ( ):
565
+ with PatchSessionTimeout ( 0 ):
576
566
with self .assertRaises (OperationFailure ):
577
567
s .with_transaction (callback )
578
568
@@ -601,7 +591,7 @@ def callback(session):
601
591
listener .results .clear ()
602
592
603
593
with client .start_session () as s :
604
- with PatchMockTimer ( ):
594
+ with PatchSessionTimeout ( 0 ):
605
595
with self .assertRaises (OperationFailure ):
606
596
s .with_transaction (callback )
607
597
@@ -629,7 +619,7 @@ def callback(session):
629
619
listener .results .clear ()
630
620
631
621
with client .start_session () as s :
632
- with PatchMockTimer ( ):
622
+ with PatchSessionTimeout ( 0 ):
633
623
with self .assertRaises (ConnectionFailure ):
634
624
s .with_transaction (callback )
635
625
0 commit comments