@@ -749,6 +749,8 @@ def unit_of_work(txn, *args, **kw):
749749 self .assertEqual (kw , {'some_arg' : 'def' })
750750
751751 def test_run_in_transaction_w_timeout (self ):
752+ from google .cloud .spanner import session as MUT
753+ from google .cloud ._testing import _Monkey
752754 from google .gax .errors import GaxError
753755 from google .gax .grpc import exc_to_code
754756 from google .cloud .proto .spanner .v1 .transaction_pb2 import (
@@ -779,12 +781,17 @@ def unit_of_work(txn, *args, **kw):
779781 called_with .append ((txn , args , kw ))
780782 txn .insert (TABLE_NAME , COLUMNS , VALUES )
781783
782- with self .assertRaises (GaxError ) as exc :
783- session .run_in_transaction (unit_of_work , timeout_secs = 0.01 )
784+ time_module = _FauxTimeModule ()
785+ time_module ._times = [1 , 1.5 , 2.5 ] # retry once w/ timeout_secs=1
786+
787+ with _Monkey (MUT , time = time_module ):
788+ with self .assertRaises (GaxError ) as exc :
789+ session .run_in_transaction (unit_of_work , timeout_secs = 1 )
784790
785791 self .assertEqual (exc_to_code (exc .exception .cause ), StatusCode .ABORTED )
786792
787- self .assertGreater (len (called_with ), 1 )
793+ self .assertEqual (time_module ._slept , None )
794+ self .assertEqual (len (called_with ), 2 )
788795 for txn , args , kw in called_with :
789796 self .assertIsInstance (txn , Transaction )
790797 self .assertIsNone (txn .committed )
@@ -881,9 +888,14 @@ def __init__(self, name):
881888class _FauxTimeModule (object ):
882889
883890 _slept = None
891+ _times = ()
884892
885893 def time (self ):
886894 import time
895+
896+ if len (self ._times ) > 0 :
897+ return self ._times .pop (0 )
898+
887899 return time .time ()
888900
889901 def sleep (self , seconds ):
0 commit comments