1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import datetime
16+
1517import mock
1618
1719from google .api .core import exceptions
2123import google .api .core .page_iterator
2224
2325
26+ def _utcnow_monotonic ():
27+ curr_value = datetime .datetime .min
28+ delta = datetime .timedelta (seconds = 0.5 )
29+ while True :
30+ yield curr_value
31+ curr_value += delta
32+
33+
2434def test_wrap_method_basic ():
2535 method = mock .Mock (spec = ['__call__' ], return_value = 42 )
2636
@@ -139,10 +149,14 @@ def test_wrap_method_with_overriding_retry_and_timeout(unusued_sleep):
139149
140150
141151@mock .patch ('time.sleep' )
142- def test_wrap_method_with_overriding_retry_deadline (unusued_sleep ):
152+ @mock .patch (
153+ 'google.api.core.helpers.datetime_helpers.utcnow' ,
154+ side_effect = _utcnow_monotonic (),
155+ autospec = True )
156+ def test_wrap_method_with_overriding_retry_deadline (utcnow , unused_sleep ):
143157 method = mock .Mock (
144158 spec = ['__call__' ],
145- side_effect = ([exceptions .InternalServerError (None )] * 3 ) + [42 ]
159+ side_effect = ([exceptions .InternalServerError (None )] * 4 ) + [42 ]
146160 )
147161 default_retry = retry .Retry ()
148162 default_timeout = timeout .ExponentialTimeout (deadline = 60 )
@@ -156,7 +170,12 @@ def test_wrap_method_with_overriding_retry_deadline(unusued_sleep):
156170
157171 assert result == 42
158172 timeout_args = [call [1 ]['timeout' ] for call in method .call_args_list ]
159- assert timeout_args == [5 , 10 , 20 , 29 ]
173+ assert timeout_args == [5.0 , 10.0 , 20.0 , 26.0 , 25.0 ]
174+ assert utcnow .call_count == (
175+ 1 + # First to set the deadline.
176+ 5 + # One for each min(timeout, maximum, (DEADLINE - NOW).seconds)
177+ 5
178+ )
160179
161180
162181def test_wrap_method_with_overriding_timeout_as_a_number ():
0 commit comments