forked from mozilla-releng/redo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_retry.py
213 lines (167 loc) · 7.03 KB
/
test_retry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# ***** BEGIN LICENSE BLOCK *****
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
# ***** END LICENSE BLOCK *****
import mock
import unittest
from redo import retry, retriable, retrying, retrier
ATTEMPT_N = 1
def _succeedOnSecondAttempt(foo=None, exception=Exception):
global ATTEMPT_N
if ATTEMPT_N == 2:
ATTEMPT_N += 1
return
ATTEMPT_N += 1
raise exception("Fail")
def _alwaysPass():
global ATTEMPT_N
ATTEMPT_N += 1
return True
def _mirrorArgs(*args, **kwargs):
return args, kwargs
def _alwaysFail():
raise Exception("Fail")
class NewError(Exception):
pass
class OtherError(Exception):
pass
def _raiseCustomException():
return _succeedOnSecondAttempt(exception=NewError)
class TestRetry(unittest.TestCase):
def setUp(self):
global ATTEMPT_N
ATTEMPT_N = 1
self.sleep_patcher = mock.patch('time.sleep')
self.sleep_patcher.start()
def tearDown(self):
self.sleep_patcher.stop()
def testRetrySucceed(self):
# Will raise if anything goes wrong
retry(_succeedOnSecondAttempt, attempts=2, sleeptime=0)
def testRetriableSucceed(self):
func = retriable(attempts=2, sleeptime=0)(_succeedOnSecondAttempt)
func()
def testRetryFailWithoutCatching(self):
self.assertRaises(Exception, retry, _alwaysFail, sleeptime=0,
exceptions=())
def testRetriableFailWithoutCatching(self):
func = retriable(sleeptime=0)(_alwaysFail)
self.assertRaises(Exception, func, retry_exceptions=())
def testRetryFailEnsureRaisesLastException(self):
self.assertRaises(Exception, retry, _alwaysFail, sleeptime=0)
def testRetriableFailEnsureRaisesLastException(self):
func = retriable(sleeptime=0)(_alwaysFail)
self.assertRaises(Exception, func)
def testRetrySelectiveExceptionSucceed(self):
retry(_raiseCustomException, attempts=2, sleeptime=0,
retry_exceptions=(NewError,))
def testRetriableSelectiveExceptionSucceed(self):
func = retriable(attempts=2, sleeptime=0,
retry_exceptions=(NewError,))(_raiseCustomException)
func()
def testRetrySelectiveExceptionFail(self):
self.assertRaises(NewError, retry, _raiseCustomException, attempts=2,
sleeptime=0, retry_exceptions=(OtherError,))
def testRetriableSelectiveExceptionFail(self):
func = retriable(attempts=2, sleeptime=0,
retry_exceptions=(OtherError,))(_raiseCustomException)
self.assertRaises(NewError, func)
def testRetryWithSleep(self):
retry(_succeedOnSecondAttempt, attempts=2, sleeptime=1)
def testRetriableWithSleep(self):
func = retriable(attempts=2, sleeptime=1)(_succeedOnSecondAttempt)
func()
def testRetryOnlyRunOnce(self):
"""Tests that retry() doesn't call the action again after success"""
global ATTEMPT_N
retry(_alwaysPass, attempts=3, sleeptime=0)
# ATTEMPT_N gets increased regardless of pass/fail
self.assertEquals(2, ATTEMPT_N)
def testRetriableOnlyRunOnce(self):
global ATTEMPT_N
func = retriable(attempts=3, sleeptime=0)(_alwaysPass)
func()
# ATTEMPT_N gets increased regardless of pass/fail
self.assertEquals(2, ATTEMPT_N)
def testRetryReturns(self):
ret = retry(_alwaysPass, sleeptime=0)
self.assertEquals(ret, True)
def testRetriableReturns(self):
func = retriable(sleeptime=0)(_alwaysPass)
ret = func()
self.assertEquals(ret, True)
def testRetryCleanupIsCalled(self):
cleanup = mock.Mock()
retry(_succeedOnSecondAttempt, cleanup=cleanup, sleeptime=0)
self.assertEquals(cleanup.call_count, 1)
def testRetriableCleanupIsCalled(self):
cleanup = mock.Mock()
func = retriable(cleanup=cleanup, sleeptime=0)(_succeedOnSecondAttempt)
func()
self.assertEquals(cleanup.call_count, 1)
def testRetryArgsPassed(self):
args = (1, 'two', 3)
kwargs = dict(foo='a', bar=7)
ret = retry(_mirrorArgs, args=args, kwargs=kwargs.copy(), sleeptime=0)
self.assertEqual(ret[0], args)
self.assertEqual(ret[1], kwargs)
def testRetriableArgsPassed(self):
args = (1, 'two', 3)
kwargs = dict(foo='a', bar=7)
func = retriable(sleeptime=0)(_mirrorArgs)
ret = func(*args, **kwargs)
self.assertEqual(ret[0], args)
self.assertEqual(ret[1], kwargs)
def test_retrying_id(self):
"""Make sure that the context manager doesn't change the original
callable"""
def wrapped():
pass
before = id(wrapped)
with retrying(wrapped) as w:
within = id(w)
after = id(wrapped)
self.assertEqual(before, after)
self.assertNotEqual(before, within)
def test_retrying_call_retry(self):
"""Make sure to distribute retry and function args/kwargs properly"""
def wrapped(*args, **kwargs):
pass
with mock.patch("redo.retry") as mocked_retry:
with retrying(wrapped, 1, x="y") as w:
w("a", b=1, c="a")
mocked_retry.assert_called_once_with(
wrapped, 1, x='y', args=('a',), kwargs={'c': 'a', 'b': 1})
def test_retrier(self):
"""Make sure retrier behaves properly"""
n = 0
for _ in retrier(attempts=5, sleeptime=0, jitter=0):
n += 1
self.assertEquals(n, 5)
def test_retrier_sleep(self):
"""Make sure retrier sleep is behaving"""
with mock.patch("time.sleep") as sleep:
# Test that normal sleep scaling works
for _ in retrier(attempts=5, sleeptime=10, max_sleeptime=300, sleepscale=2, jitter=0):
pass
expected = [mock.call(x) for x in (10, 20, 40, 80)]
self.assertEquals(sleep.call_args_list, expected)
def test_retrier_maxsleep(self):
with mock.patch("time.sleep") as sleep:
# Test that max sleep time works
for _ in retrier(attempts=5, sleeptime=10, max_sleeptime=30, sleepscale=2, jitter=0):
pass
expected = [mock.call(x) for x in (10, 20, 30, 30)]
self.assertEquals(sleep.call_args_list, expected)
def test_retrier_jitter(self):
with mock.patch("time.sleep") as sleep:
# Test that jitter works
with mock.patch("random.randint") as randint:
randint.return_value = 3
for _ in retrier(attempts=5, sleeptime=10, max_sleeptime=300,
sleepscale=2, jitter=3):
randint.return_value *= -1
expected = [mock.call(x) for x in (7, 17, 31, 65)]
self.assertEquals(sleep.call_args_list, expected)
self.assertEquals(randint.call_args, mock.call(-3, 3))