Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions core/google/api/core/gapic_v1/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from google.api.core.gapic_v1 import config
from google.api.core.gapic_v1 import method

__all__ = [
'config',
'method',
]
15 changes: 13 additions & 2 deletions core/google/api/core/gapic_v1/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
_API_CORE_VERSION = pkg_resources.get_distribution('google-cloud-core').version
METRICS_METADATA_KEY = 'x-goog-api-client'
USE_DEFAULT_METADATA = object()
DEFAULT = object()

This comment was marked as spam.

This comment was marked as spam.

"""Sentinel value indicating that a retry or timeout argument was unspecified,
so the default should be used."""


def _is_not_none_or_false(value):
Expand Down Expand Up @@ -82,18 +85,23 @@ def _determine_timeout(default_timeout, specified_timeout, retry):
default_timeout (Optional[Timeout]): The default timeout specified
at method creation time.
specified_timeout (Optional[Timeout]): The timeout specified at
invocation time.
invocation time. If :attr:`DEFAULT`, this will be set to
the ``default_timeout``.
retry (Optional[Retry]): The retry specified at invocation time.

Returns:
Optional[Timeout]: The timeout to apply to the method or ``None``.
"""
if specified_timeout is DEFAULT:
specified_timeout = default_timeout

This comment was marked as spam.

This comment was marked as spam.


if specified_timeout is default_timeout:
# If timeout is the default and the default timeout is exponential and
# a non-default retry is specified, make sure the timeout's deadline
# matches the retry's. This handles the case where the user leaves
# the timeout default but specifies a lower deadline via the retry.
if retry and isinstance(default_timeout, timeout.ExponentialTimeout):
if (retry and retry is not DEFAULT
and isinstance(default_timeout, timeout.ExponentialTimeout)):
return default_timeout.with_deadline(retry._deadline)
else:
return default_timeout
Expand Down Expand Up @@ -141,6 +149,9 @@ def __call__(self, *args, **kwargs):

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

if retry is DEFAULT:
retry = self._retry

# Apply all applicable decorators.
wrapped_func = _apply_decorators(self._target, [retry, timeout_])

Expand Down
41 changes: 33 additions & 8 deletions core/tests/unit/api_core/gapic/test_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ def test_wrap_method_with_merged_metadata():

@mock.patch('time.sleep')
def test_wrap_method_with_default_retry_and_timeout(unusued_sleep):
method = mock.Mock(spec=['__call__'], side_effect=[
exceptions.InternalServerError(None),
42])
method = mock.Mock(
spec=['__call__'],
side_effect=[exceptions.InternalServerError(None), 42]
)
default_retry = retry.Retry()
default_timeout = timeout.ConstantTimeout(60)
wrapped_method = google.api.core.gapic_v1.method.wrap_method(
Expand All @@ -96,11 +97,33 @@ def test_wrap_method_with_default_retry_and_timeout(unusued_sleep):
method.assert_called_with(timeout=60, metadata=mock.ANY)


@mock.patch('time.sleep')
def test_wrap_method_with_default_retry_and_timeout_using_sentinel(
unusued_sleep):
method = mock.Mock(
spec=['__call__'],
side_effect=[exceptions.InternalServerError(None), 42]
)
default_retry = retry.Retry()
default_timeout = timeout.ConstantTimeout(60)
wrapped_method = google.api.core.gapic_v1.method.wrap_method(
method, default_retry, default_timeout)

result = wrapped_method(
retry=google.api.core.gapic_v1.method.DEFAULT,
timeout=google.api.core.gapic_v1.method.DEFAULT)

assert result == 42
assert method.call_count == 2
method.assert_called_with(timeout=60, metadata=mock.ANY)


@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 All @@ -117,8 +140,10 @@ 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):
method = mock.Mock(spec=['__call__'], side_effect=([
exceptions.InternalServerError(None)] * 3) + [42])
method = mock.Mock(
spec=['__call__'],
side_effect=([exceptions.InternalServerError(None)] * 3) + [42]
)
default_retry = retry.Retry()
default_timeout = timeout.ExponentialTimeout(deadline=60)
wrapped_method = google.api.core.gapic_v1.method.wrap_method(
Expand Down