Skip to content

Commit c18b2df

Browse files
committed
fix tests
1 parent fe0626f commit c18b2df

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

retry_decorator/retry_decorator.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import logging
77
import time
88
import random
9-
import types
109

1110

1211
def _isiter(i):
@@ -28,16 +27,16 @@ def _deco_retry(f, exc=Exception, tries=10, timeout_secs=1.0, logger=None, callb
2827
:param callback_by_exception:
2928
:return:
3029
"""
30+
3131
def f_retry(*args, **kwargs):
3232
mtries, mdelay = tries, timeout_secs
3333
run_one_last_time = True
34+
3435
while mtries > 1:
3536
try:
3637
return f(*args, **kwargs)
3738
except exc as e:
3839
# check if this exception is something the caller wants special handling for
39-
if isinstance(callback_by_exception, (types.FunctionType, list, tuple)):
40-
callback_by_exception = {Exception: callback_by_exception}
4140
callback_errors = callback_by_exception or {}
4241

4342
for error_type in callback_errors:
@@ -97,6 +96,9 @@ def __init__(
9796
elif tries < 1:
9897
raise ValueError("[tries] arg needs to be an int >= 1")
9998

99+
if callable(callback_by_exception) or isinstance(callback_by_exception, (list, tuple)):
100+
callback_by_exception = {Exception: callback_by_exception}
101+
100102
self.exc = exc
101103
self.tries = tries
102104
self.timeout_secs = timeout_secs

tests/test_callback.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ class ExampleTestError(Exception):
1414
pass
1515

1616

17-
class_for_testing = None
17+
class_for_testing = ClassForTesting()
1818

1919

2020
class 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,19 +95,30 @@ 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()
102-
raise Exception('Expected ValueError to be thrown')
113+
retry_decorator.retry(tries=0, callback_by_exception=partial(callback_logic, class_for_testing, 'hello', 'foo'))
114+
raise AssertionError('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()
109-
raise Exception('Expected TypeError to be thrown')
120+
retry_decorator.retry(tries='not int', callback_by_exception=partial(callback_logic, class_for_testing, 'hello', 'foo'))
121+
raise AssertionError('Expected TypeError to be thrown')
110122
except TypeError:
111123
pass
112124

@@ -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')})
126134
def 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
})
178186
def 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'))
184192
def 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)))
188198
def 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

196203
if __name__ == '__main__':

0 commit comments

Comments
 (0)