Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6623b31

Browse files
author
Ilya Gurov
authored
feat(api-core): pass retry from result() to done() (#9)
Towards:googleapis/python-bigquery#24
1 parentede6dc6 commit6623b31

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

‎google/api_core/future/polling.py‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,18 @@ def done(self, retry=DEFAULT_RETRY):
7878
# pylint: disable=redundant-returns-doc, missing-raises-doc
7979
raiseNotImplementedError()
8080

81-
def_done_or_raise(self):
81+
def_done_or_raise(self,retry=DEFAULT_RETRY):
8282
"""Check if the future is done and raise if it's not."""
83-
ifnotself.done():
83+
kwargs= {}ifretryisDEFAULT_RETRYelse {"retry":retry}
84+
85+
ifnotself.done(**kwargs):
8486
raise_OperationNotComplete()
8587

8688
defrunning(self):
8789
"""True if the operation is currently running."""
8890
returnnotself.done()
8991

90-
def_blocking_poll(self,timeout=None):
92+
def_blocking_poll(self,timeout=None,retry=DEFAULT_RETRY):
9193
"""Poll and wait for the Future to be resolved.
9294
9395
Args:
@@ -101,13 +103,14 @@ def _blocking_poll(self, timeout=None):
101103
retry_=self._retry.with_deadline(timeout)
102104

103105
try:
104-
retry_(self._done_or_raise)()
106+
kwargs= {}ifretryisDEFAULT_RETRYelse {"retry":retry}
107+
retry_(self._done_or_raise)(**kwargs)
105108
exceptexceptions.RetryError:
106109
raiseconcurrent.futures.TimeoutError(
107110
"Operation did not complete within the designated ""timeout."
108111
)
109112

110-
defresult(self,timeout=None):
113+
defresult(self,timeout=None,retry=DEFAULT_RETRY):
111114
"""Get the result of the operation, blocking if necessary.
112115
113116
Args:
@@ -122,7 +125,8 @@ def result(self, timeout=None):
122125
google.api_core.GoogleAPICallError: If the operation errors or if
123126
the timeout is reached before the operation completes.
124127
"""
125-
self._blocking_poll(timeout=timeout)
128+
kwargs= {}ifretryisDEFAULT_RETRYelse {"retry":retry}
129+
self._blocking_poll(timeout=timeout,**kwargs)
126130

127131
ifself._exceptionisnotNone:
128132
# pylint: disable=raising-bad-type

‎tests/unit/future/test_polling.py‎

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
importmock
2020
importpytest
2121

22-
fromgoogle.api_coreimportexceptions
22+
fromgoogle.api_coreimportexceptions,retry
2323
fromgoogle.api_core.futureimportpolling
2424

2525

@@ -43,6 +43,8 @@ def test_polling_future_constructor():
4343
assertnotfuture.cancelled()
4444
assertfuture.running()
4545
assertfuture.cancel()
46+
withmock.patch.object(future,"done",return_value=True):
47+
future.result()
4648

4749

4850
deftest_set_result():
@@ -87,7 +89,7 @@ def __init__(self):
8789
self.poll_count=0
8890
self.event=threading.Event()
8991

90-
defdone(self):
92+
defdone(self,retry=polling.DEFAULT_RETRY):
9193
self.poll_count+=1
9294
self.event.wait()
9395
self.set_result(42)
@@ -108,7 +110,7 @@ def test_result_with_polling():
108110

109111

110112
classPollingFutureImplTimeout(PollingFutureImplWithPoll):
111-
defdone(self):
113+
defdone(self,retry=polling.DEFAULT_RETRY):
112114
time.sleep(1)
113115
returnFalse
114116

@@ -130,7 +132,7 @@ def __init__(self, errors):
130132
super(PollingFutureImplTransient,self).__init__()
131133
self._errors=errors
132134

133-
defdone(self):
135+
defdone(self,retry=polling.DEFAULT_RETRY):
134136
ifself._errors:
135137
error,self._errors=self._errors[0],self._errors[1:]
136138
raiseerror("testing")
@@ -192,3 +194,49 @@ def test_double_callback_background_thread():
192194
assertfuture.poll_count==1
193195
callback.assert_called_once_with(future)
194196
callback2.assert_called_once_with(future)
197+
198+
199+
classPollingFutureImplWithoutRetry(PollingFutureImpl):
200+
defdone(self):
201+
returnTrue
202+
203+
defresult(self):
204+
returnsuper(PollingFutureImplWithoutRetry,self).result()
205+
206+
def_blocking_poll(self,timeout):
207+
returnsuper(PollingFutureImplWithoutRetry,self)._blocking_poll(
208+
timeout=timeout
209+
)
210+
211+
212+
classPollingFutureImplWith_done_or_raise(PollingFutureImpl):
213+
defdone(self):
214+
returnTrue
215+
216+
def_done_or_raise(self):
217+
returnsuper(PollingFutureImplWith_done_or_raise,self)._done_or_raise()
218+
219+
220+
deftest_polling_future_without_retry():
221+
custom_retry=retry.Retry(
222+
predicate=retry.if_exception_type(exceptions.TooManyRequests)
223+
)
224+
future=PollingFutureImplWithoutRetry()
225+
assertfuture.done()
226+
assertfuture.running()
227+
assertfuture.result()isNone
228+
229+
withmock.patch.object(future,"done")asdone_mock:
230+
future._done_or_raise()
231+
done_mock.assert_called_once_with()
232+
233+
withmock.patch.object(future,"done")asdone_mock:
234+
future._done_or_raise(retry=custom_retry)
235+
done_mock.assert_called_once_with(retry=custom_retry)
236+
237+
238+
deftest_polling_future_with__done_or_raise():
239+
future=PollingFutureImplWith_done_or_raise()
240+
assertfuture.done()
241+
assertfuture.running()
242+
assertfuture.result()isNone

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp