@@ -14,14 +14,15 @@ class ExampleTestError(Exception):
1414 pass
1515
1616
17- class_for_testing = None
17+ class_for_testing = ClassForTesting ()
1818
1919
2020class MyTestCase (unittest .TestCase ):
2121
22- def setUp ():
23- global class_for_testing
24- class_for_testing = ClassForTesting ()
22+ def setUp (self ):
23+ class_for_testing .hello = None
24+ class_for_testing .cb_counter = 0
25+ class_for_testing .exe_counter = 0
2526
2627 def test_callback_invoked_on_configured_exception_type (self ):
2728 try :
@@ -77,7 +78,7 @@ def test_verify_breakout_true_works(self):
7778 except Exception :
7879 pass
7980 self .assertEqual (class_for_testing .hello , 'baz' )
80- self .assertEqual (class_for_testing .cb_counter , 6 )
81+ self .assertEqual (class_for_testing .cb_counter , 6 ) # we had 2 handlers, but because of breakout=True only first of them was ever ran
8182 self .assertEqual (class_for_testing .exe_counter , 7 )
8283
8384 def test_verify_run_last_time_false_works (self ):
@@ -94,18 +95,29 @@ def test_verify_tries_1_is_ok(self):
9495 my_test_func_9 ()
9596 except Exception :
9697 pass
98+ self .assertEqual (class_for_testing .hello , None )
99+ self .assertEqual (class_for_testing .cb_counter , 0 )
100+ self .assertEqual (class_for_testing .exe_counter , 1 )
101+
102+ def test_verify_run_last_time_false_with_2_tries (self ):
103+ try :
104+ my_test_func_10 ()
105+ except Exception :
106+ pass
97107 self .assertEqual (class_for_testing .hello , 'foo' )
108+ self .assertEqual (class_for_testing .cb_counter , 1 )
109+ self .assertEqual (class_for_testing .exe_counter , 1 )
98110
99111 def test_verify_tries_0_errors_out (self ):
100112 try :
101- my_test_func_10 ( )
113+ retry_decorator . retry ( tries = 0 , callback_by_exception = partial ( callback_logic , class_for_testing , 'hello' , 'foo' ) )
102114 raise Exception ('Expected ValueError to be thrown' )
103115 except ValueError :
104116 pass
105117
106118 def test_verify_tries_not_int_is_error (self ):
107119 try :
108- my_test_func_11 ( )
120+ retry_decorator . retry ( tries = 'not int' , callback_by_exception = partial ( callback_logic , class_for_testing , 'hello' , 'foo' ) )
109121 raise Exception ('Expected TypeError to be thrown' )
110122 except TypeError :
111123 pass
@@ -117,10 +129,6 @@ def callback_logic(instance, attr_to_set, value_to_set):
117129 instance .cb_counter += 1
118130
119131
120- def get_callback_tuple_breakout (attr_value , breakout_value = False ):
121- return (partial (callback_logic , class_for_testing , 'hello' , attr_value ), breakout_value )
122-
123-
124132@retry_decorator .retry (exc = ExampleTestError , tries = 2 , callback_by_exception = {
125133 ExampleTestError : partial (callback_logic , class_for_testing , 'hello' , 'world' )})
126134def my_test_func ():
@@ -173,7 +181,7 @@ def my_test_func_7():
173181
174182@retry_decorator .retry (tries = 8 , callback_by_exception = {
175183 TypeError : partial (callback_logic , class_for_testing , 'hello' , 'foo' ),
176- Exception : (partial (callback_logic , class_for_testing , 'hello' , 'bar' ), False , False )
184+ Exception : (partial (callback_logic , class_for_testing , 'hello' , 'bar' ), ( False , False ) )
177185 })
178186def my_test_func_8 ():
179187 class_for_testing .exe_counter += 1
@@ -182,15 +190,14 @@ def my_test_func_8():
182190
183191@retry_decorator .retry (tries = 1 , callback_by_exception = partial (callback_logic , class_for_testing , 'hello' , 'foo' ))
184192def my_test_func_9 ():
193+ class_for_testing .exe_counter += 1
185194 raise TypeError ('type oh noes.' )
186195
187196
197+ @retry_decorator .retry (tries = 2 , callback_by_exception = (partial (callback_logic , class_for_testing , 'hello' , 'foo' ), (False , False )))
188198def my_test_func_10 ():
189- retry_decorator .retry (tries = 0 , callback_by_exception = partial (callback_logic , class_for_testing , 'hello' , 'foo' ))
190-
191-
192- def my_test_func_11 ():
193- retry_decorator .retry (tries = 'not int' , callback_by_exception = partial (callback_logic , class_for_testing , 'hello' , 'foo' ))
199+ class_for_testing .exe_counter += 1
200+ raise TypeError ('type oh noes.' )
194201
195202
196203if __name__ == '__main__' :
0 commit comments