Skip to content

Commit 3123de6

Browse files
Jon Wayne Parrottdhermes
authored andcommitted
Fix test assertion in test_wrap_method_with_overriding_retry_deadline (googleapis#4131)
* Fix test assertion in test_wrap_method_with_overriding_retry_deadline * Making `test_wrap_method_with_overriding_retry_deadline` deterministic.
1 parent 3a95612 commit 3123de6

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

core/tests/unit/api_core/gapic/test_method.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import datetime
16+
1517
import mock
1618

1719
from google.api.core import exceptions
@@ -21,6 +23,14 @@
2123
import 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+
2434
def 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

162181
def test_wrap_method_with_overriding_timeout_as_a_number():

0 commit comments

Comments
 (0)