Skip to content

Commit d2557ef

Browse files
authored
Retry transient errors in 'PollingFuture.result'. (#6305)
Closes #6301.
1 parent 9dfc335 commit d2557ef

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

google/api_core/future/polling.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ class _OperationNotComplete(Exception):
2828
pass
2929

3030

31-
RETRY_PREDICATE = retry.if_exception_type(_OperationNotComplete)
31+
RETRY_PREDICATE = retry.if_exception_type(
32+
_OperationNotComplete,
33+
exceptions.TooManyRequests,
34+
exceptions.InternalServerError,
35+
exceptions.BadGateway,
36+
)
3237
DEFAULT_RETRY = retry.Retry(predicate=RETRY_PREDICATE)
3338

3439

tests/unit/future/test_polling.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import mock
2020
import pytest
2121

22+
from google.api_core import exceptions
2223
from google.api_core.future import polling
2324

2425

@@ -118,6 +119,34 @@ def test_result_timeout():
118119
future.result(timeout=1)
119120

120121

122+
class PollingFutureImplTransient(PollingFutureImplWithPoll):
123+
def __init__(self, errors):
124+
super(PollingFutureImplTransient, self).__init__()
125+
self._errors = errors
126+
127+
def done(self):
128+
if self._errors:
129+
error, self._errors = self._errors[0], self._errors[1:]
130+
raise error('testing')
131+
self.poll_count += 1
132+
self.set_result(42)
133+
return True
134+
135+
136+
def test_result_transient_error():
137+
future = PollingFutureImplTransient((
138+
exceptions.TooManyRequests,
139+
exceptions.InternalServerError,
140+
exceptions.BadGateway,
141+
))
142+
result = future.result()
143+
assert result == 42
144+
assert future.poll_count == 1
145+
# Repeated calls should not cause additional polling
146+
assert future.result() == result
147+
assert future.poll_count == 1
148+
149+
121150
def test_callback_background_thread():
122151
future = PollingFutureImplWithPoll()
123152
callback = mock.Mock()

0 commit comments

Comments
 (0)