@@ -161,20 +161,25 @@ def test_constructor_defaults(self):
161161 assert retry_ ._maximum == 60
162162 assert retry_ ._multiplier == 2
163163 assert retry_ ._deadline == 120
164+ assert retry_ ._on_error is None
164165
165166 def test_constructor_options (self ):
167+ _some_function = mock .Mock ()
168+
166169 retry_ = retry .Retry (
167170 predicate = mock .sentinel .predicate ,
168171 initial = 1 ,
169172 maximum = 2 ,
170173 multiplier = 3 ,
171174 deadline = 4 ,
175+ on_error = _some_function ,
172176 )
173177 assert retry_ ._predicate == mock .sentinel .predicate
174178 assert retry_ ._initial == 1
175179 assert retry_ ._maximum == 2
176180 assert retry_ ._multiplier == 3
177181 assert retry_ ._deadline == 4
182+ assert retry_ ._on_error is _some_function
178183
179184 def test_with_deadline (self ):
180185 retry_ = retry .Retry ()
@@ -209,7 +214,8 @@ def test___str__(self):
209214 assert re .match (
210215 (
211216 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>"
213219 ),
214220 str (retry_ ),
215221 )
@@ -230,8 +236,7 @@ def test___call___and_execute_success(self, sleep):
230236 target .assert_called_once_with ("meep" )
231237 sleep .assert_not_called ()
232238
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.
235240 @mock .patch ("random.uniform" , autospec = True , side_effect = lambda m , n : n / 2.0 )
236241 @mock .patch ("time.sleep" , autospec = True )
237242 def test___call___and_execute_retry (self , sleep , uniform ):
@@ -253,3 +258,55 @@ def test___call___and_execute_retry(self, sleep, uniform):
253258 target .assert_has_calls ([mock .call ("meep" ), mock .call ("meep" )])
254259 sleep .assert_called_once_with (retry_ ._initial )
255260 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