Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Making test_wrap_method_with_overriding_retry_deadline deterministic.
  • Loading branch information
dhermes committed Oct 6, 2017
commit 776ae3cc5915a717f6c35920ba340fff4acbfc16
24 changes: 21 additions & 3 deletions core/tests/unit/api_core/gapic/test_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import datetime

import mock

from google.api.core import exceptions
Expand All @@ -21,6 +23,14 @@
import google.api.core.page_iterator


def _utcnow_monotonic():
curr_value = datetime.datetime.min
delta = datetime.timedelta(seconds=0.5)
while True:
yield curr_value
curr_value += delta


def test_wrap_method_basic():
method = mock.Mock(spec=['__call__'], return_value=42)

Expand Down Expand Up @@ -139,7 +149,11 @@ def test_wrap_method_with_overriding_retry_and_timeout(unusued_sleep):


@mock.patch('time.sleep')
def test_wrap_method_with_overriding_retry_deadline(unusued_sleep):
@mock.patch(
'google.api.core.helpers.datetime_helpers.utcnow',
side_effect=_utcnow_monotonic(),
autospec=True)
def test_wrap_method_with_overriding_retry_deadline(utcnow, unused_sleep):
method = mock.Mock(
spec=['__call__'],
side_effect=([exceptions.InternalServerError(None)] * 4) + [42]
Expand All @@ -156,8 +170,12 @@ def test_wrap_method_with_overriding_retry_deadline(unusued_sleep):

assert result == 42
timeout_args = [call[1]['timeout'] for call in method.call_args_list]
# Timeout should never exceed 30s.
assert max(timeout_args) <= 30
assert timeout_args == [5.0, 10.0, 20.0, 26.0, 25.0]
assert utcnow.call_count == (
1 + # First to set the deadline.
5 + # One for each min(timeout, maximum, (DEADLINE - NOW).seconds)
5

This comment was marked as spam.

)


def test_wrap_method_with_overriding_timeout_as_a_number():
Expand Down