Skip to content

Commit cad638b

Browse files
author
Jon Wayne Parrott
authored
Add DEFAULT sentinel for gapic_v1.method (#4079)
1 parent f96afd0 commit cad638b

3 files changed

Lines changed: 67 additions & 10 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2017 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.api.core.gapic_v1 import config
16+
from google.api.core.gapic_v1 import method
17+
18+
__all__ = [
19+
'config',
20+
'method',
21+
]

packages/google-cloud-core/google/api/core/gapic_v1/method.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
_API_CORE_VERSION = pkg_resources.get_distribution('google-cloud-core').version
3434
METRICS_METADATA_KEY = 'x-goog-api-client'
3535
USE_DEFAULT_METADATA = object()
36+
DEFAULT = object()
37+
"""Sentinel value indicating that a retry or timeout argument was unspecified,
38+
so the default should be used."""
3639

3740

3841
def _is_not_none_or_false(value):
@@ -82,18 +85,23 @@ def _determine_timeout(default_timeout, specified_timeout, retry):
8285
default_timeout (Optional[Timeout]): The default timeout specified
8386
at method creation time.
8487
specified_timeout (Optional[Timeout]): The timeout specified at
85-
invocation time.
88+
invocation time. If :attr:`DEFAULT`, this will be set to
89+
the ``default_timeout``.
8690
retry (Optional[Retry]): The retry specified at invocation time.
8791
8892
Returns:
8993
Optional[Timeout]: The timeout to apply to the method or ``None``.
9094
"""
95+
if specified_timeout is DEFAULT:
96+
specified_timeout = default_timeout
97+
9198
if specified_timeout is default_timeout:
9299
# If timeout is the default and the default timeout is exponential and
93100
# a non-default retry is specified, make sure the timeout's deadline
94101
# matches the retry's. This handles the case where the user leaves
95102
# the timeout default but specifies a lower deadline via the retry.
96-
if retry and isinstance(default_timeout, timeout.ExponentialTimeout):
103+
if (retry and retry is not DEFAULT
104+
and isinstance(default_timeout, timeout.ExponentialTimeout)):
97105
return default_timeout.with_deadline(retry._deadline)
98106
else:
99107
return default_timeout
@@ -141,6 +149,9 @@ def __call__(self, *args, **kwargs):
141149

142150
retry = kwargs.pop('retry', self._retry)
143151

152+
if retry is DEFAULT:
153+
retry = self._retry
154+
144155
# Apply all applicable decorators.
145156
wrapped_func = _apply_decorators(self._target, [retry, timeout_])
146157

packages/google-cloud-core/tests/unit/api_core/gapic/test_method.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ def test_wrap_method_with_merged_metadata():
8181

8282
@mock.patch('time.sleep')
8383
def test_wrap_method_with_default_retry_and_timeout(unusued_sleep):
84-
method = mock.Mock(spec=['__call__'], side_effect=[
85-
exceptions.InternalServerError(None),
86-
42])
84+
method = mock.Mock(
85+
spec=['__call__'],
86+
side_effect=[exceptions.InternalServerError(None), 42]
87+
)
8788
default_retry = retry.Retry()
8889
default_timeout = timeout.ConstantTimeout(60)
8990
wrapped_method = google.api.core.gapic_v1.method.wrap_method(
@@ -96,11 +97,33 @@ def test_wrap_method_with_default_retry_and_timeout(unusued_sleep):
9697
method.assert_called_with(timeout=60, metadata=mock.ANY)
9798

9899

100+
@mock.patch('time.sleep')
101+
def test_wrap_method_with_default_retry_and_timeout_using_sentinel(
102+
unusued_sleep):
103+
method = mock.Mock(
104+
spec=['__call__'],
105+
side_effect=[exceptions.InternalServerError(None), 42]
106+
)
107+
default_retry = retry.Retry()
108+
default_timeout = timeout.ConstantTimeout(60)
109+
wrapped_method = google.api.core.gapic_v1.method.wrap_method(
110+
method, default_retry, default_timeout)
111+
112+
result = wrapped_method(
113+
retry=google.api.core.gapic_v1.method.DEFAULT,
114+
timeout=google.api.core.gapic_v1.method.DEFAULT)
115+
116+
assert result == 42
117+
assert method.call_count == 2
118+
method.assert_called_with(timeout=60, metadata=mock.ANY)
119+
120+
99121
@mock.patch('time.sleep')
100122
def test_wrap_method_with_overriding_retry_and_timeout(unusued_sleep):
101-
method = mock.Mock(spec=['__call__'], side_effect=[
102-
exceptions.NotFound(None),
103-
42])
123+
method = mock.Mock(
124+
spec=['__call__'],
125+
side_effect=[exceptions.NotFound(None), 42]
126+
)
104127
default_retry = retry.Retry()
105128
default_timeout = timeout.ConstantTimeout(60)
106129
wrapped_method = google.api.core.gapic_v1.method.wrap_method(
@@ -117,8 +140,10 @@ def test_wrap_method_with_overriding_retry_and_timeout(unusued_sleep):
117140

118141
@mock.patch('time.sleep')
119142
def test_wrap_method_with_overriding_retry_deadline(unusued_sleep):
120-
method = mock.Mock(spec=['__call__'], side_effect=([
121-
exceptions.InternalServerError(None)] * 3) + [42])
143+
method = mock.Mock(
144+
spec=['__call__'],
145+
side_effect=([exceptions.InternalServerError(None)] * 3) + [42]
146+
)
122147
default_retry = retry.Retry()
123148
default_timeout = timeout.ExponentialTimeout(deadline=60)
124149
wrapped_method = google.api.core.gapic_v1.method.wrap_method(

0 commit comments

Comments
 (0)