19
19
import mock
20
20
import pytest
21
21
22
- from google .api_core import exceptions
22
+ from google .api_core import exceptions , retry
23
23
from google .api_core .future import polling
24
24
25
25
@@ -43,6 +43,8 @@ def test_polling_future_constructor():
43
43
assert not future .cancelled ()
44
44
assert future .running ()
45
45
assert future .cancel ()
46
+ with mock .patch .object (future , "done" , return_value = True ):
47
+ future .result ()
46
48
47
49
48
50
def test_set_result ():
@@ -87,7 +89,7 @@ def __init__(self):
87
89
self .poll_count = 0
88
90
self .event = threading .Event ()
89
91
90
- def done (self ):
92
+ def done (self , retry = polling . DEFAULT_RETRY ):
91
93
self .poll_count += 1
92
94
self .event .wait ()
93
95
self .set_result (42 )
@@ -108,7 +110,7 @@ def test_result_with_polling():
108
110
109
111
110
112
class PollingFutureImplTimeout (PollingFutureImplWithPoll ):
111
- def done (self ):
113
+ def done (self , retry = polling . DEFAULT_RETRY ):
112
114
time .sleep (1 )
113
115
return False
114
116
@@ -130,7 +132,7 @@ def __init__(self, errors):
130
132
super (PollingFutureImplTransient , self ).__init__ ()
131
133
self ._errors = errors
132
134
133
- def done (self ):
135
+ def done (self , retry = polling . DEFAULT_RETRY ):
134
136
if self ._errors :
135
137
error , self ._errors = self ._errors [0 ], self ._errors [1 :]
136
138
raise error ("testing" )
@@ -192,3 +194,49 @@ def test_double_callback_background_thread():
192
194
assert future .poll_count == 1
193
195
callback .assert_called_once_with (future )
194
196
callback2 .assert_called_once_with (future )
197
+
198
+
199
+ class PollingFutureImplWithoutRetry (PollingFutureImpl ):
200
+ def done (self ):
201
+ return True
202
+
203
+ def result (self ):
204
+ return super (PollingFutureImplWithoutRetry , self ).result ()
205
+
206
+ def _blocking_poll (self , timeout ):
207
+ return super (PollingFutureImplWithoutRetry , self )._blocking_poll (
208
+ timeout = timeout
209
+ )
210
+
211
+
212
+ class PollingFutureImplWith_done_or_raise (PollingFutureImpl ):
213
+ def done (self ):
214
+ return True
215
+
216
+ def _done_or_raise (self ):
217
+ return super (PollingFutureImplWith_done_or_raise , self )._done_or_raise ()
218
+
219
+
220
+ def test_polling_future_without_retry ():
221
+ custom_retry = retry .Retry (
222
+ predicate = retry .if_exception_type (exceptions .TooManyRequests )
223
+ )
224
+ future = PollingFutureImplWithoutRetry ()
225
+ assert future .done ()
226
+ assert future .running ()
227
+ assert future .result () is None
228
+
229
+ with mock .patch .object (future , "done" ) as done_mock :
230
+ future ._done_or_raise ()
231
+ done_mock .assert_called_once_with ()
232
+
233
+ with mock .patch .object (future , "done" ) as done_mock :
234
+ future ._done_or_raise (retry = custom_retry )
235
+ done_mock .assert_called_once_with (retry = custom_retry )
236
+
237
+
238
+ def test_polling_future_with__done_or_raise ():
239
+ future = PollingFutureImplWith_done_or_raise ()
240
+ assert future .done ()
241
+ assert future .running ()
242
+ assert future .result () is None
0 commit comments