Skip to content

Commit

Permalink
Retry transient errors in 'PollingFuture.result'. (#6305)
Browse files Browse the repository at this point in the history
Closes #6301.
  • Loading branch information
tseaver authored Oct 31, 2018
1 parent 43b7232 commit f576d14
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
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

0 comments on commit f576d14

Please sign in to comment.