Skip to content

Commit 2010373

Browse files
fix: _determine_timeout problem handling float type timeout (googleapis#64)
1 parent e2d9a7b commit 2010373

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pip-log.txt
2929
.nox
3030
.cache
3131
.pytest_cache
32+
pytype_output
3233

3334

3435
# Mac
@@ -57,4 +58,4 @@ system_tests/local_test_setup
5758

5859
# Make sure a generated file isn't accidentally committed.
5960
pylintrc
60-
pylintrc.test
61+
pylintrc.test

google/api_core/gapic_v1/method.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ def _determine_timeout(default_timeout, specified_timeout, retry):
6161
Returns:
6262
Optional[Timeout]: The timeout to apply to the method or ``None``.
6363
"""
64+
# If timeout is specified as a number instead of a Timeout instance,
65+
# convert it to a ConstantTimeout.
66+
if isinstance(specified_timeout, (int, float)):
67+
specified_timeout = timeout.ConstantTimeout(specified_timeout)
68+
if isinstance(default_timeout, (int, float)):
69+
default_timeout = timeout.ConstantTimeout(default_timeout)
70+
6471
if specified_timeout is DEFAULT:
6572
specified_timeout = default_timeout
6673

@@ -78,12 +85,7 @@ def _determine_timeout(default_timeout, specified_timeout, retry):
7885
else:
7986
return default_timeout
8087

81-
# If timeout is specified as a number instead of a Timeout instance,
82-
# convert it to a ConstantTimeout.
83-
if isinstance(specified_timeout, (int, float)):
84-
return timeout.ConstantTimeout(specified_timeout)
85-
else:
86-
return specified_timeout
88+
return specified_timeout
8789

8890

8991
class _GapicCallable(object):

tests/unit/gapic/test_method.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ def _utcnow_monotonic():
3232
curr_value += delta
3333

3434

35+
def test__determine_timeout():
36+
# Check _determine_timeout always returns a Timeout object.
37+
timeout_type_timeout = timeout.ConstantTimeout(600.0)
38+
returned_timeout = google.api_core.gapic_v1.method._determine_timeout(
39+
600.0, 600.0, None
40+
)
41+
assert isinstance(returned_timeout, timeout.ConstantTimeout)
42+
returned_timeout = google.api_core.gapic_v1.method._determine_timeout(
43+
600.0, timeout_type_timeout, None
44+
)
45+
assert isinstance(returned_timeout, timeout.ConstantTimeout)
46+
returned_timeout = google.api_core.gapic_v1.method._determine_timeout(
47+
timeout_type_timeout, 600.0, None
48+
)
49+
assert isinstance(returned_timeout, timeout.ConstantTimeout)
50+
returned_timeout = google.api_core.gapic_v1.method._determine_timeout(
51+
timeout_type_timeout, timeout_type_timeout, None
52+
)
53+
assert isinstance(returned_timeout, timeout.ConstantTimeout)
54+
55+
3556
def test_wrap_method_basic():
3657
method = mock.Mock(spec=["__call__"], return_value=42)
3758

@@ -154,7 +175,8 @@ def test_wrap_method_with_default_retry_and_timeout_using_sentinel(unusued_sleep
154175

155176
@mock.patch("time.sleep")
156177
def test_wrap_method_with_overriding_retry_and_timeout(unusued_sleep):
157-
method = mock.Mock(spec=["__call__"], side_effect=[exceptions.NotFound(None), 42])
178+
method = mock.Mock(spec=["__call__"], side_effect=[
179+
exceptions.NotFound(None), 42])
158180
default_retry = retry.Retry()
159181
default_timeout = timeout.ConstantTimeout(60)
160182
wrapped_method = google.api_core.gapic_v1.method.wrap_method(

0 commit comments

Comments
 (0)