-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_async.py
329 lines (241 loc) · 7.09 KB
/
test_async.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# SPDX-FileCopyrightText: 2022 Hynek Schlawack <[email protected]>
#
# SPDX-License-Identifier: MIT
import datetime as dt
import pytest
import stamina
pytestmark = pytest.mark.anyio
@pytest.mark.parametrize("attempts", [None, 1])
@pytest.mark.parametrize("timeout", [None, 1, dt.timedelta(days=1)])
@pytest.mark.parametrize("duration", [1, dt.timedelta(days=1)])
async def test_ok(attempts, timeout, duration):
"""
No error, no problem.
"""
class C:
@stamina.retry(
on=Exception,
attempts=attempts,
timeout=timeout,
wait_initial=duration,
wait_max=duration,
wait_jitter=duration,
)
async def f(self):
return 42
@stamina.retry(
on=Exception,
attempts=attempts,
timeout=timeout,
wait_initial=duration,
wait_max=duration,
wait_jitter=duration,
)
async def f():
return 42
assert 42 == await f()
assert 42 == await C().f()
@pytest.mark.parametrize("timeout", [None, 1, dt.timedelta(days=1)])
@pytest.mark.parametrize("duration", [0, dt.timedelta(days=0)])
async def test_retries(duration, timeout, on):
"""
Retries if the specific error is raised.
"""
i = 0
@stamina.retry(
on=on,
timeout=timeout,
wait_max=duration,
wait_initial=duration,
wait_jitter=duration,
)
async def f():
nonlocal i
if i == 0:
i += 1
raise ValueError
return 42
assert 42 == await f()
assert 1 == i
async def test_retries_method(on):
"""
Retries if the specific error is raised.
"""
i = 0
class C:
@stamina.retry(on=on, wait_max=0)
async def f(self):
nonlocal i
if i == 0:
i += 1
raise ValueError
return 42
assert 42 == await C().f()
assert 1 == i
async def test_wrong_exception(on):
"""
Exceptions that are not passed as `on` are left through.
"""
@stamina.retry(on=on)
async def f():
raise TypeError("passed")
with pytest.raises(TypeError, match="passed"):
await f()
async def test_retry_inactive():
"""
If inactive, don't retry.
"""
num_called = 0
@stamina.retry(on=Exception)
async def f():
nonlocal num_called
num_called += 1
raise Exception("passed")
stamina.set_active(False)
with pytest.raises(Exception, match="passed"):
await f()
assert 1 == num_called
async def test_retry_inactive_block():
"""
If inactive, don't retry.
"""
num_called = 0
stamina.set_active(False)
with pytest.raises(Exception, match="passed"): # noqa: PT012
async for attempt in stamina.retry_context(on=ValueError):
with attempt:
num_called += 1
raise Exception("passed")
assert 1 == num_called
async def test_retry_inactive_ok():
"""
If inactive, the happy path still works.
"""
num_called = 0
@stamina.retry(on=Exception)
async def f():
nonlocal num_called
num_called += 1
stamina.set_active(False)
await f()
assert 1 == num_called
async def test_retry_inactive_block_ok():
"""
If inactive, the happy path still works.
"""
num_called = 0
stamina.set_active(False)
async for attempt in stamina.retry_context(on=ValueError):
with attempt:
num_called += 1
assert 1 == num_called
async def test_retry_block(on):
"""
Async retry_context blocks are retried.
"""
num_called = 0
async for attempt in stamina.retry_context(on=on, wait_max=0):
with attempt:
num_called += 1
assert num_called == attempt.num
if num_called < 2:
raise ValueError
assert 2 == num_called
async def test_next_wait():
"""
The next_wait property is updated.
"""
async for attempt in stamina.retry_context(on=ValueError, wait_max=0.0001):
with attempt:
assert pytest.approx(0.0001) == attempt.next_wait
if attempt.num == 1:
raise ValueError
async def test_testing_mode():
"""
Testing mode can be set and reset.
"""
stamina.set_testing(True, attempts=3)
assert stamina.is_testing()
with pytest.raises(ValueError): # noqa: PT012
async for attempt in stamina.retry_context(on=ValueError):
assert 0.0 == attempt.next_wait
with attempt:
raise ValueError
assert 3 == attempt.num
stamina.set_testing(False)
assert not stamina.is_testing()
async for attempt in stamina.retry_context(on=ValueError):
assert 0.0 != attempt.next_wait
break
async def test_retry_blocks_can_be_disabled():
"""
Async context retries respect the config.
"""
stamina.set_active(False)
num_called = 0
with pytest.raises(Exception, match="passed"): # noqa: PT012
async for attempt in stamina.retry_context(on=Exception, attempts=2):
with attempt:
num_called += 1
raise Exception("passed")
assert 1 == num_called
class TestAsyncRetryingCaller:
async def test_ok(self):
"""
No error, no problem.
"""
arc = stamina.AsyncRetryingCaller().on(BaseException)
async def f():
return 42
assert 42 == await arc(f)
async def test_retries(self, on):
"""
Retries if the specific error is raised. Arguments are passed through.
"""
i = 0
async def f(*args, **kw):
nonlocal i
if i < 1:
i += 1
raise ValueError
return args, kw
arc = stamina.AsyncRetryingCaller(wait_max=0).on(on)
args, kw = await arc(f, 42, foo="bar")
assert 1 == i
assert (42,) == args
assert {"foo": "bar"} == kw
def test_repr(self):
"""
repr() is useful
"""
arc = stamina.AsyncRetryingCaller(
attempts=42,
timeout=13.0,
wait_initial=23,
wait_max=123,
wait_jitter=0.42,
wait_exp_base=666,
)
r = repr(arc)
assert (
"<AsyncRetryingCaller(attempts=42, timeout=13.0, "
"wait_exp_base=666, wait_initial=23, wait_jitter=0.42, "
"wait_max=123)>"
) == r
assert f"<BoundAsyncRetryingCaller(ValueError, {r})>" == repr(
arc.on(ValueError)
)
async def test_testing_mode_context():
"""
Testing mode context manager works with async code.
"""
assert not stamina.is_testing()
with stamina.set_testing(True, attempts=3):
assert stamina.is_testing()
with pytest.raises(ValueError): # noqa: PT012
async for attempt in stamina.retry_context(on=ValueError):
assert 0.0 == attempt.next_wait
with attempt:
raise ValueError
assert 3 == attempt.num
assert not stamina.is_testing()