Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: _determine_timeout problem handling float type timeout #64

Merged
merged 1 commit into from Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -29,6 +29,7 @@ pip-log.txt
.nox
.cache
.pytest_cache
pytype_output


# Mac
Expand Down Expand Up @@ -57,4 +58,4 @@ system_tests/local_test_setup

# Make sure a generated file isn't accidentally committed.
pylintrc
pylintrc.test
pylintrc.test
14 changes: 8 additions & 6 deletions google/api_core/gapic_v1/method.py
Expand Up @@ -61,6 +61,13 @@ def _determine_timeout(default_timeout, specified_timeout, retry):
Returns:
Optional[Timeout]: The timeout to apply to the method or ``None``.
"""
# If timeout is specified as a number instead of a Timeout instance,
# convert it to a ConstantTimeout.
if isinstance(specified_timeout, (int, float)):
specified_timeout = timeout.ConstantTimeout(specified_timeout)
if isinstance(default_timeout, (int, float)):
default_timeout = timeout.ConstantTimeout(default_timeout)

if specified_timeout is DEFAULT:
specified_timeout = default_timeout

Expand All @@ -78,12 +85,7 @@ def _determine_timeout(default_timeout, specified_timeout, retry):
else:
return default_timeout

# If timeout is specified as a number instead of a Timeout instance,
# convert it to a ConstantTimeout.
if isinstance(specified_timeout, (int, float)):
return timeout.ConstantTimeout(specified_timeout)
else:
return specified_timeout
return specified_timeout


class _GapicCallable(object):
Expand Down
24 changes: 23 additions & 1 deletion tests/unit/gapic/test_method.py
Expand Up @@ -32,6 +32,27 @@ def _utcnow_monotonic():
curr_value += delta


def test__determine_timeout():
# Check _determine_timeout always returns a Timeout object.
timeout_type_timeout = timeout.ConstantTimeout(600.0)
returned_timeout = google.api_core.gapic_v1.method._determine_timeout(
600.0, 600.0, None
)
assert isinstance(returned_timeout, timeout.ConstantTimeout)
returned_timeout = google.api_core.gapic_v1.method._determine_timeout(
600.0, timeout_type_timeout, None
)
assert isinstance(returned_timeout, timeout.ConstantTimeout)
returned_timeout = google.api_core.gapic_v1.method._determine_timeout(
timeout_type_timeout, 600.0, None
)
assert isinstance(returned_timeout, timeout.ConstantTimeout)
returned_timeout = google.api_core.gapic_v1.method._determine_timeout(
timeout_type_timeout, timeout_type_timeout, None
)
assert isinstance(returned_timeout, timeout.ConstantTimeout)


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

Expand Down Expand Up @@ -154,7 +175,8 @@ def test_wrap_method_with_default_retry_and_timeout_using_sentinel(unusued_sleep

@mock.patch("time.sleep")
def test_wrap_method_with_overriding_retry_and_timeout(unusued_sleep):
method = mock.Mock(spec=["__call__"], side_effect=[exceptions.NotFound(None), 42])
method = mock.Mock(spec=["__call__"], side_effect=[
exceptions.NotFound(None), 42])
default_retry = retry.Retry()
default_timeout = timeout.ConstantTimeout(60)
wrapped_method = google.api_core.gapic_v1.method.wrap_method(
Expand Down