Add 'apps/arv-web/' from commit 'f9732ad8460d013c2f28363655d0d1b91894dca5'
[arvados.git] / sdk / python / tests / test_retry.py
1 #!/usr/bin/env python
2
3 import itertools
4 import unittest
5
6 import arvados.errors as arv_error
7 import arvados.retry as arv_retry
8 import mock
9
10 from arvados_testutil import fake_requests_response
11
12 class RetryLoopTestMixin(object):
13     @staticmethod
14     def loop_success(result):
15         # During the tests, we use integers that look like HTTP status
16         # codes as loop results.  Then we define simplified HTTP
17         # heuristics here to decide whether the result is success (True),
18         # permanent failure (False), or temporary failure (None).
19         if result < 400:
20             return True
21         elif result < 500:
22             return False
23         else:
24             return None
25
26     def run_loop(self, num_retries, *results, **kwargs):
27         responses = itertools.chain(results, itertools.repeat(None))
28         retrier = arv_retry.RetryLoop(num_retries, self.loop_success,
29                                       **kwargs)
30         for tries_left, response in itertools.izip(retrier, responses):
31             retrier.save_result(response)
32         return retrier
33
34     def check_result(self, retrier, expect_success, last_code):
35         self.assertIs(retrier.success(), expect_success,
36                       "loop success flag is incorrect")
37         self.assertEqual(last_code, retrier.last_result())
38
39
40 class RetryLoopTestCase(unittest.TestCase, RetryLoopTestMixin):
41     def test_zero_retries_and_success(self):
42         retrier = self.run_loop(0, 200)
43         self.check_result(retrier, True, 200)
44
45     def test_zero_retries_and_tempfail(self):
46         retrier = self.run_loop(0, 500, 501)
47         self.check_result(retrier, None, 500)
48
49     def test_zero_retries_and_permfail(self):
50         retrier = self.run_loop(0, 400, 201)
51         self.check_result(retrier, False, 400)
52
53     def test_one_retry_with_immediate_success(self):
54         retrier = self.run_loop(1, 200, 201)
55         self.check_result(retrier, True, 200)
56
57     def test_one_retry_with_delayed_success(self):
58         retrier = self.run_loop(1, 500, 201)
59         self.check_result(retrier, True, 201)
60
61     def test_one_retry_with_no_success(self):
62         retrier = self.run_loop(1, 500, 501, 502)
63         self.check_result(retrier, None, 501)
64
65     def test_one_retry_but_permfail(self):
66         retrier = self.run_loop(1, 400, 201)
67         self.check_result(retrier, False, 400)
68
69     def test_two_retries_with_immediate_success(self):
70         retrier = self.run_loop(2, 200, 201, 202)
71         self.check_result(retrier, True, 200)
72
73     def test_two_retries_with_success_after_one(self):
74         retrier = self.run_loop(2, 500, 201, 502)
75         self.check_result(retrier, True, 201)
76
77     def test_two_retries_with_success_after_two(self):
78         retrier = self.run_loop(2, 500, 501, 202, 503)
79         self.check_result(retrier, True, 202)
80
81     def test_two_retries_with_no_success(self):
82         retrier = self.run_loop(2, 500, 501, 502, 503)
83         self.check_result(retrier, None, 502)
84
85     def test_two_retries_with_permfail(self):
86         retrier = self.run_loop(2, 500, 401, 202)
87         self.check_result(retrier, False, 401)
88
89     def test_save_result_before_start_is_error(self):
90         retrier = arv_retry.RetryLoop(0)
91         self.assertRaises(arv_error.AssertionError, retrier.save_result, 1)
92
93     def test_save_result_after_end_is_error(self):
94         retrier = arv_retry.RetryLoop(0)
95         for count in retrier:
96             pass
97         self.assertRaises(arv_error.AssertionError, retrier.save_result, 1)
98
99
100 @mock.patch('time.time', side_effect=itertools.count())
101 @mock.patch('time.sleep')
102 class RetryLoopBackoffTestCase(unittest.TestCase, RetryLoopTestMixin):
103     def run_loop(self, num_retries, *results, **kwargs):
104         kwargs.setdefault('backoff_start', 8)
105         return super(RetryLoopBackoffTestCase, self).run_loop(
106             num_retries, *results, **kwargs)
107
108     def check_backoff(self, sleep_mock, sleep_count, multiplier=1):
109         # Figure out how much time we actually spent sleeping.
110         sleep_times = [arglist[0][0] for arglist in sleep_mock.call_args_list
111                        if arglist[0][0] > 0]
112         self.assertEqual(sleep_count, len(sleep_times),
113                          "loop did not back off correctly")
114         last_wait = 0
115         for this_wait in sleep_times:
116             self.assertGreater(this_wait, last_wait * multiplier,
117                                "loop did not grow backoff times correctly")
118             last_wait = this_wait
119
120     def test_no_backoff_with_no_retries(self, sleep_mock, time_mock):
121         self.run_loop(0, 500, 201)
122         self.check_backoff(sleep_mock, 0)
123
124     def test_no_backoff_after_success(self, sleep_mock, time_mock):
125         self.run_loop(1, 200, 501)
126         self.check_backoff(sleep_mock, 0)
127
128     def test_no_backoff_after_permfail(self, sleep_mock, time_mock):
129         self.run_loop(1, 400, 201)
130         self.check_backoff(sleep_mock, 0)
131
132     def test_backoff_before_success(self, sleep_mock, time_mock):
133         self.run_loop(5, 500, 501, 502, 203, 504)
134         self.check_backoff(sleep_mock, 3)
135
136     def test_backoff_before_permfail(self, sleep_mock, time_mock):
137         self.run_loop(5, 500, 501, 502, 403, 504)
138         self.check_backoff(sleep_mock, 3)
139
140     def test_backoff_all_tempfail(self, sleep_mock, time_mock):
141         self.run_loop(3, 500, 501, 502, 503, 504)
142         self.check_backoff(sleep_mock, 3)
143
144     def test_backoff_multiplier(self, sleep_mock, time_mock):
145         self.run_loop(5, 500, 501, 502, 503, 504, 505,
146                       backoff_start=5, backoff_growth=10)
147         self.check_backoff(sleep_mock, 5, 9)
148
149
150 class CheckHTTPResponseSuccessTestCase(unittest.TestCase):
151     def results_map(self, *codes):
152         for code in codes:
153             response = fake_requests_response(code, None)
154             yield code, arv_retry.check_http_response_success(response)
155
156     def check(assert_name):
157         def check_method(self, expected, *codes):
158             assert_func = getattr(self, assert_name)
159             for code, actual in self.results_map(*codes):
160                 assert_func(expected, actual,
161                             "{} status flagged {}".format(code, actual))
162                 if assert_name != 'assertIs':
163                     self.assertTrue(
164                         actual is True or actual is False or actual is None,
165                         "{} status returned {}".format(code, actual))
166         return check_method
167
168     check_is = check('assertIs')
169     check_is_not = check('assertIsNot')
170
171     def test_obvious_successes(self):
172         self.check_is(True, *range(200, 207))
173
174     def test_obvious_stops(self):
175         self.check_is(False, 424, 426, 428, 431,
176                       *range(400, 408) + range(410, 420))
177
178     def test_obvious_retries(self):
179         self.check_is(None, 500, 502, 503, 504)
180
181     def test_4xx_retries(self):
182         self.check_is(None, 408, 409, 422, 423)
183
184     def test_5xx_failures(self):
185         self.check_is(False, 501, *range(505, 512))
186
187     def test_1xx_not_retried(self):
188         self.check_is_not(None, 100, 101)
189
190     def test_redirects_not_retried(self):
191         self.check_is_not(None, *range(300, 309))
192
193     def test_wacky_code_retries(self):
194         self.check_is(None, 0, 99, 600, -200)
195
196
197 class RetryMethodTestCase(unittest.TestCase):
198     class Tester(object):
199         def __init__(self):
200             self.num_retries = 1
201
202         @arv_retry.retry_method
203         def check(self, a, num_retries=None, z=0):
204             return (a, num_retries, z)
205
206
207     def test_positional_arg_passed(self):
208         self.assertEqual((3, 2, 0), self.Tester().check(3, 2))
209
210     def test_keyword_arg_passed(self):
211         self.assertEqual((4, 3, 0), self.Tester().check(num_retries=3, a=4))
212
213     def test_not_specified(self):
214         self.assertEqual((0, 1, 0), self.Tester().check(0))
215
216     def test_not_specified_with_other_kwargs(self):
217         self.assertEqual((1, 1, 1), self.Tester().check(1, z=1))
218
219     def test_bad_call(self):
220         with self.assertRaises(TypeError):
221             self.Tester().check(num_retries=2)
222
223
224 if __name__ == '__main__':
225     unittest.main()