@@ -161,20 +161,25 @@ def test_constructor_defaults(self):
161
161
assert retry_ ._maximum == 60
162
162
assert retry_ ._multiplier == 2
163
163
assert retry_ ._deadline == 120
164
+ assert retry_ ._on_error is None
164
165
165
166
def test_constructor_options (self ):
167
+ _some_function = mock .Mock ()
168
+
166
169
retry_ = retry .Retry (
167
170
predicate = mock .sentinel .predicate ,
168
171
initial = 1 ,
169
172
maximum = 2 ,
170
173
multiplier = 3 ,
171
174
deadline = 4 ,
175
+ on_error = _some_function ,
172
176
)
173
177
assert retry_ ._predicate == mock .sentinel .predicate
174
178
assert retry_ ._initial == 1
175
179
assert retry_ ._maximum == 2
176
180
assert retry_ ._multiplier == 3
177
181
assert retry_ ._deadline == 4
182
+ assert retry_ ._on_error is _some_function
178
183
179
184
def test_with_deadline (self ):
180
185
retry_ = retry .Retry ()
@@ -209,7 +214,8 @@ def test___str__(self):
209
214
assert re .match (
210
215
(
211
216
r"<Retry predicate=<function.*?if_exception_type.*?>, "
212
- r"initial=1.0, maximum=60.0, multiplier=2.0, deadline=120.0>"
217
+ r"initial=1.0, maximum=60.0, multiplier=2.0, deadline=120.0, "
218
+ r"on_error=None>"
213
219
),
214
220
str (retry_ ),
215
221
)
@@ -230,8 +236,7 @@ def test___call___and_execute_success(self, sleep):
230
236
target .assert_called_once_with ("meep" )
231
237
sleep .assert_not_called ()
232
238
233
- # Make uniform return half of its maximum, which will be the calculated
234
- # sleep time.
239
+ # Make uniform return half of its maximum, which is the calculated sleep time.
235
240
@mock .patch ("random.uniform" , autospec = True , side_effect = lambda m , n : n / 2.0 )
236
241
@mock .patch ("time.sleep" , autospec = True )
237
242
def test___call___and_execute_retry (self , sleep , uniform ):
@@ -253,3 +258,55 @@ def test___call___and_execute_retry(self, sleep, uniform):
253
258
target .assert_has_calls ([mock .call ("meep" ), mock .call ("meep" )])
254
259
sleep .assert_called_once_with (retry_ ._initial )
255
260
assert on_error .call_count == 1
261
+
262
+ @mock .patch ("time.sleep" , autospec = True )
263
+ def test___init___without_retry_executed (self , sleep ):
264
+ _some_function = mock .Mock ()
265
+
266
+ retry_ = retry .Retry (
267
+ predicate = retry .if_exception_type (ValueError ), on_error = _some_function
268
+ )
269
+ # check the proper creation of the class
270
+ assert retry_ ._on_error is _some_function
271
+
272
+ target = mock .Mock (spec = ["__call__" ], side_effect = [42 ])
273
+ # __name__ is needed by functools.partial.
274
+ target .__name__ = "target"
275
+
276
+ wrapped = retry_ (target )
277
+
278
+ result = wrapped ("meep" )
279
+
280
+ assert result == 42
281
+ target .assert_called_once_with ("meep" )
282
+ sleep .assert_not_called ()
283
+ _some_function .assert_not_called ()
284
+
285
+ # Make uniform return half of its maximum, which is the calculated sleep time.
286
+ @mock .patch ("random.uniform" , autospec = True , side_effect = lambda m , n : n / 2.0 )
287
+ @mock .patch ("time.sleep" , autospec = True )
288
+ def test___init___when_retry_is_executed (self , sleep , uniform ):
289
+ _some_function = mock .Mock ()
290
+
291
+ retry_ = retry .Retry (
292
+ predicate = retry .if_exception_type (ValueError ), on_error = _some_function
293
+ )
294
+ # check the proper creation of the class
295
+ assert retry_ ._on_error is _some_function
296
+
297
+ target = mock .Mock (
298
+ spec = ["__call__" ], side_effect = [ValueError (), ValueError (), 42 ]
299
+ )
300
+ # __name__ is needed by functools.partial.
301
+ target .__name__ = "target"
302
+
303
+ wrapped = retry_ (target )
304
+ target .assert_not_called ()
305
+
306
+ result = wrapped ("meep" )
307
+
308
+ assert result == 42
309
+ assert target .call_count == 3
310
+ assert _some_function .call_count == 2
311
+ target .assert_has_calls ([mock .call ("meep" ), mock .call ("meep" )])
312
+ sleep .assert_any_call (retry_ ._initial )
0 commit comments