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

api_core: retry transient errors in 'PollingFuture.result'. #6305

Merged
merged 1 commit into from
Oct 31, 2018
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
7 changes: 6 additions & 1 deletion api_core/google/api_core/future/polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ class _OperationNotComplete(Exception):
pass


RETRY_PREDICATE = retry.if_exception_type(_OperationNotComplete)
RETRY_PREDICATE = retry.if_exception_type(
_OperationNotComplete,
exceptions.TooManyRequests,
exceptions.InternalServerError,
exceptions.BadGateway,
)
DEFAULT_RETRY = retry.Retry(predicate=RETRY_PREDICATE)


Expand Down
29 changes: 29 additions & 0 deletions api_core/tests/unit/future/test_polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import mock
import pytest

from google.api_core import exceptions
from google.api_core.future import polling


Expand Down Expand Up @@ -118,6 +119,34 @@ def test_result_timeout():
future.result(timeout=1)


class PollingFutureImplTransient(PollingFutureImplWithPoll):
def __init__(self, errors):
super(PollingFutureImplTransient, self).__init__()
self._errors = errors

def done(self):
if self._errors:
error, self._errors = self._errors[0], self._errors[1:]
raise error('testing')
self.poll_count += 1
self.set_result(42)
return True


def test_result_transient_error():
future = PollingFutureImplTransient((
exceptions.TooManyRequests,
exceptions.InternalServerError,
exceptions.BadGateway,
))
result = future.result()
assert result == 42
assert future.poll_count == 1
# Repeated calls should not cause additional polling
assert future.result() == result
assert future.poll_count == 1


def test_callback_background_thread():
future = PollingFutureImplWithPoll()
callback = mock.Mock()
Expand Down