Skip to content

Commit 14f1f34

Browse files
Gurov Ilyabusunkim96
Gurov Ilya
authored andcommitted
feat(api_core): add retry param into PollingFuture() and it's inheritors (#9923)
* feat(api_core): add retry param into PollingFuture() and it's inheritors Towards #6197
1 parent 18375fb commit 14f1f34

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

google/api_core/future/polling.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,12 @@ def __init__(self, retry=DEFAULT_RETRY):
6666
self._done_callbacks = []
6767

6868
@abc.abstractmethod
69-
def done(self):
69+
def done(self, retry=DEFAULT_RETRY):
7070
"""Checks to see if the operation is complete.
7171
72+
Args:
73+
retry (google.api_core.retry.Retry): (Optional) How to retry the RPC.
74+
7275
Returns:
7376
bool: True if the operation is complete, False otherwise.
7477
"""

google/api_core/operation.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,28 @@ def _set_result_from_operation(self):
145145
)
146146
self.set_exception(exception)
147147

148-
def _refresh_and_update(self):
149-
"""Refresh the operation and update the result if needed."""
148+
def _refresh_and_update(self, retry=polling.DEFAULT_RETRY):
149+
"""Refresh the operation and update the result if needed.
150+
151+
Args:
152+
retry (google.api_core.retry.Retry): (Optional) How to retry the RPC.
153+
"""
150154
# If the currently cached operation is done, no need to make another
151155
# RPC as it will not change once done.
152156
if not self._operation.done:
153-
self._operation = self._refresh()
157+
self._operation = self._refresh(retry=retry)
154158
self._set_result_from_operation()
155159

156-
def done(self):
160+
def done(self, retry=polling.DEFAULT_RETRY):
157161
"""Checks to see if the operation is complete.
158162
163+
Args:
164+
retry (google.api_core.retry.Retry): (Optional) How to retry the RPC.
165+
159166
Returns:
160167
bool: True if the operation is complete, False otherwise.
161168
"""
162-
self._refresh_and_update()
169+
self._refresh_and_update(retry)
163170
return self._operation.done
164171

165172
def cancel(self):

tests/unit/test_operation.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515

1616
import mock
1717

18+
from google.api_core import exceptions
1819
from google.api_core import operation
1920
from google.api_core import operations_v1
21+
from google.api_core import retry
2022
from google.longrunning import operations_pb2
2123
from google.protobuf import struct_pb2
2224
from google.rpc import code_pb2
@@ -113,6 +115,23 @@ def test_result():
113115
assert future.done()
114116

115117

118+
def test_done_w_retry():
119+
RETRY_PREDICATE = retry.if_exception_type(exceptions.TooManyRequests)
120+
test_retry = retry.Retry(predicate=RETRY_PREDICATE)
121+
122+
expected_result = struct_pb2.Struct()
123+
responses = [
124+
make_operation_proto(),
125+
# Second operation response includes the result.
126+
make_operation_proto(done=True, response=expected_result),
127+
]
128+
future, _, _ = make_operation_future(responses)
129+
future._refresh = mock.Mock()
130+
131+
future.done(retry=test_retry)
132+
future._refresh.assert_called_once_with(retry=test_retry)
133+
134+
116135
def test_exception():
117136
expected_exception = status_pb2.Status(message="meep")
118137
responses = [

0 commit comments

Comments
 (0)