1 from builtins import zip
2 from builtins import range
3 from builtins import object
7 import arvados.errors as arv_error
8 import arvados.retry as arv_retry
11 class RetryLoopTestMixin(object):
13 def loop_success(result):
14 # During the tests, we use integers that look like HTTP status
15 # codes as loop results. Then we define simplified HTTP
16 # heuristics here to decide whether the result is success (True),
17 # permanent failure (False), or temporary failure (None).
25 def run_loop(self, num_retries, *results, **kwargs):
26 responses = itertools.chain(results, itertools.repeat(None))
27 retrier = arv_retry.RetryLoop(num_retries, self.loop_success,
29 for tries_left, response in zip(retrier, responses):
30 retrier.save_result(response)
33 def check_result(self, retrier, expect_success, last_code):
34 self.assertIs(retrier.success(), expect_success,
35 "loop success flag is incorrect")
36 self.assertEqual(last_code, retrier.last_result())
39 class RetryLoopTestCase(unittest.TestCase, RetryLoopTestMixin):
40 def test_zero_retries_and_success(self):
41 retrier = self.run_loop(0, 200)
42 self.check_result(retrier, True, 200)
44 def test_zero_retries_and_tempfail(self):
45 retrier = self.run_loop(0, 500, 501)
46 self.check_result(retrier, None, 500)
48 def test_zero_retries_and_permfail(self):
49 retrier = self.run_loop(0, 400, 201)
50 self.check_result(retrier, False, 400)
52 def test_one_retry_with_immediate_success(self):
53 retrier = self.run_loop(1, 200, 201)
54 self.check_result(retrier, True, 200)
56 def test_one_retry_with_delayed_success(self):
57 retrier = self.run_loop(1, 500, 201)
58 self.check_result(retrier, True, 201)
60 def test_one_retry_with_no_success(self):
61 retrier = self.run_loop(1, 500, 501, 502)
62 self.check_result(retrier, None, 501)
64 def test_one_retry_but_permfail(self):
65 retrier = self.run_loop(1, 400, 201)
66 self.check_result(retrier, False, 400)
68 def test_two_retries_with_immediate_success(self):
69 retrier = self.run_loop(2, 200, 201, 202)
70 self.check_result(retrier, True, 200)
72 def test_two_retries_with_success_after_one(self):
73 retrier = self.run_loop(2, 500, 201, 502)
74 self.check_result(retrier, True, 201)
76 def test_two_retries_with_success_after_two(self):
77 retrier = self.run_loop(2, 500, 501, 202, 503)
78 self.check_result(retrier, True, 202)
80 def test_two_retries_with_no_success(self):
81 retrier = self.run_loop(2, 500, 501, 502, 503)
82 self.check_result(retrier, None, 502)
84 def test_two_retries_with_permfail(self):
85 retrier = self.run_loop(2, 500, 401, 202)
86 self.check_result(retrier, False, 401)
88 def test_save_result_before_start_is_error(self):
89 retrier = arv_retry.RetryLoop(0)
90 self.assertRaises(arv_error.AssertionError, retrier.save_result, 1)
92 def test_save_result_after_end_is_error(self):
93 retrier = arv_retry.RetryLoop(0)
96 self.assertRaises(arv_error.AssertionError, retrier.save_result, 1)
99 @mock.patch('time.time', side_effect=itertools.count())
100 @mock.patch('time.sleep')
101 class RetryLoopBackoffTestCase(unittest.TestCase, RetryLoopTestMixin):
102 def run_loop(self, num_retries, *results, **kwargs):
103 kwargs.setdefault('backoff_start', 8)
104 return super(RetryLoopBackoffTestCase, self).run_loop(
105 num_retries, *results, **kwargs)
107 def check_backoff(self, sleep_mock, sleep_count, multiplier=1):
108 # Figure out how much time we actually spent sleeping.
109 sleep_times = [arglist[0][0] for arglist in sleep_mock.call_args_list
110 if arglist[0][0] > 0]
111 self.assertEqual(sleep_count, len(sleep_times),
112 "loop did not back off correctly")
114 for this_wait in sleep_times:
115 self.assertGreater(this_wait, last_wait * multiplier,
116 "loop did not grow backoff times correctly")
117 last_wait = this_wait
119 def test_no_backoff_with_no_retries(self, sleep_mock, time_mock):
120 self.run_loop(0, 500, 201)
121 self.check_backoff(sleep_mock, 0)
123 def test_no_backoff_after_success(self, sleep_mock, time_mock):
124 self.run_loop(1, 200, 501)
125 self.check_backoff(sleep_mock, 0)
127 def test_no_backoff_after_permfail(self, sleep_mock, time_mock):
128 self.run_loop(1, 400, 201)
129 self.check_backoff(sleep_mock, 0)
131 def test_backoff_before_success(self, sleep_mock, time_mock):
132 self.run_loop(5, 500, 501, 502, 203, 504)
133 self.check_backoff(sleep_mock, 3)
135 def test_backoff_before_permfail(self, sleep_mock, time_mock):
136 self.run_loop(5, 500, 501, 502, 403, 504)
137 self.check_backoff(sleep_mock, 3)
139 def test_backoff_all_tempfail(self, sleep_mock, time_mock):
140 self.run_loop(3, 500, 501, 502, 503, 504)
141 self.check_backoff(sleep_mock, 3)
143 def test_backoff_multiplier(self, sleep_mock, time_mock):
144 self.run_loop(5, 500, 501, 502, 503, 504, 505,
145 backoff_start=5, backoff_growth=10, max_wait=1000000000)
146 self.check_backoff(sleep_mock, 5, 9)
149 class CheckHTTPResponseSuccessTestCase(unittest.TestCase):
150 def results_map(self, *codes):
152 yield code, arv_retry.check_http_response_success(code)
154 def check(assert_name):
155 def check_method(self, expected, *codes):
156 assert_func = getattr(self, assert_name)
157 for code, actual in self.results_map(*codes):
158 assert_func(expected, actual,
159 "{} status flagged {}".format(code, actual))
160 if assert_name != 'assertIs':
162 actual is True or actual is False or actual is None,
163 "{} status returned {}".format(code, actual))
166 check_is = check('assertIs')
167 check_is_not = check('assertIsNot')
169 def test_obvious_successes(self):
170 self.check_is(True, *list(range(200, 207)))
172 def test_obvious_stops(self):
173 self.check_is(False, 424, 426, 428, 431,
174 *list(range(400, 408)) + list(range(410, 420)))
176 def test_obvious_retries(self):
177 self.check_is(None, 500, 502, 503, 504)
179 def test_4xx_retries(self):
180 self.check_is(None, 408, 409, 422, 423)
182 def test_5xx_failures(self):
183 self.check_is(False, 501, *list(range(505, 512)))
185 def test_1xx_not_retried(self):
186 self.check_is_not(None, 100, 101)
188 def test_redirects_not_retried(self):
189 self.check_is_not(None, *list(range(300, 309)))
191 def test_wacky_code_retries(self):
192 self.check_is(None, 0, 99, 600, -200)
195 class RetryMethodTestCase(unittest.TestCase):
196 class Tester(object):
200 @arv_retry.retry_method
201 def check(self, a, num_retries=None, z=0):
202 return (a, num_retries, z)
205 def test_positional_arg_raises(self):
206 # unsupported use -- make sure we raise rather than ignore
207 with self.assertRaises(TypeError):
208 self.assertEqual((3, 2, 0), self.Tester().check(3, 2))
210 def test_keyword_arg_passed(self):
211 self.assertEqual((4, 3, 0), self.Tester().check(num_retries=3, a=4))
213 def test_not_specified(self):
214 self.assertEqual((0, 1, 0), self.Tester().check(0))
216 def test_not_specified_with_other_kwargs(self):
217 self.assertEqual((1, 1, 1), self.Tester().check(1, z=1))
219 def test_bad_call(self):
220 with self.assertRaises(TypeError):
221 self.Tester().check(num_retries=2)
224 if __name__ == '__main__':