-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_contrib.py
132 lines (103 loc) · 4.64 KB
/
test_contrib.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
from logging import getLogger
import pytest
from aspectlib import contrib
from aspectlib.contrib import retry
from aspectlib.test import LogCapture
def flaky_func(arg):
if arg:
arg.pop()
raise OSError('Tough luck!')
def test_done_suceess():
calls = []
@retry
def ok_func():
calls.append(1)
ok_func()
assert calls == [1]
def test_defaults():
calls = []
retry(sleep=calls.append)(flaky_func)([None] * 5)
assert calls == [0, 0, 0, 0, 0]
def test_raises():
calls = []
pytest.raises(OSError, retry(sleep=calls.append)(flaky_func), [None] * 6) # noqa: PT011
assert calls == [0, 0, 0, 0, 0]
calls = []
pytest.raises(OSError, retry(sleep=calls.append, retries=1)(flaky_func), [None, None]) # noqa: PT011
assert calls == [0]
def test_backoff():
calls = []
retry(sleep=calls.append, backoff=1.5)(flaky_func)([None] * 5)
assert calls == [1.5, 1.5, 1.5, 1.5, 1.5]
def test_backoff_exponential():
calls = []
retry(sleep=calls.append, retries=10, backoff=retry.exponential_backoff)(flaky_func)([None] * 10)
print(calls)
assert calls == [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
def test_backoff_straight():
calls = []
retry(sleep=calls.append, retries=10, backoff=retry.straight_backoff)(flaky_func)([None] * 10)
print(calls)
assert calls == [1, 2, 5, 10, 15, 20, 25, 30, 35, 40]
def test_backoff_flat():
calls = []
retry(sleep=calls.append, retries=10, backoff=retry.flat_backoff)(flaky_func)([None] * 10)
print(calls)
assert calls == [1, 2, 5, 10, 15, 30, 60, 60, 60, 60]
def test_with_class():
logger = getLogger(__name__)
class Connection:
count = 0
@retry
def __init__(self, address):
self.address = address
self.__connect()
def __connect(self, *_, **__):
self.count += 1
if self.count % 3:
raise OSError('Failed')
else:
logger.info('connected!')
@retry(cleanup=__connect)
def action(self, arg1, arg2):
self.count += 1
if self.count % 3 == 0:
raise OSError('Failed')
else:
logger.info('action!')
def __repr__(self):
return f'Connection@{self.count}'
with LogCapture([logger, contrib.logger]) as logcap:
try:
conn = Connection('to-something')
for i in range(5):
conn.action(i, i)
finally:
for i in logcap.messages:
print(i)
assert logcap.messages == [
('ERROR', "__init__((Connection@1, 'to-something'), {}) raised exception Failed. 5 retries left. Sleeping 0 secs."),
('ERROR', "__init__((Connection@1, 'to-something'), {}) raised exception Failed. 5 retries left. Sleeping 0 secs."),
('ERROR', "__init__((Connection@2, 'to-something'), {}) raised exception Failed. 4 retries left. Sleeping 0 secs."),
('ERROR', "__init__((Connection@2, 'to-something'), {}) raised exception Failed. 4 retries left. Sleeping 0 secs."),
('INFO', 'connected!'),
('INFO', 'action!'),
('INFO', 'action!'),
('ERROR', 'action((Connection@6, 2, 2), {}) raised exception Failed. 5 retries left. Sleeping 0 secs.'),
('ERROR', 'action((Connection@6, 2, 2), {}) raised exception Failed. 5 retries left. Sleeping 0 secs.'),
('ERROR', 'action((Connection@7, 2, 2), {}) raised exception Failed. 4 retries left. Sleeping 0 secs.'),
('ERROR', 'action((Connection@7, 2, 2), {}) raised exception Failed. 4 retries left. Sleeping 0 secs.'),
('ERROR', 'action((Connection@8, 2, 2), {}) raised exception Failed. 3 retries left. Sleeping 0 secs.'),
('ERROR', 'action((Connection@8, 2, 2), {}) raised exception Failed. 3 retries left. Sleeping 0 secs.'),
('INFO', 'connected!'),
('INFO', 'action!'),
('INFO', 'action!'),
('ERROR', 'action((Connection@12, 4, 4), {}) raised exception Failed. 5 retries left. Sleeping 0 secs.'),
('ERROR', 'action((Connection@12, 4, 4), {}) raised exception Failed. 5 retries left. Sleeping 0 secs.'),
('ERROR', 'action((Connection@13, 4, 4), {}) raised exception Failed. 4 retries left. Sleeping 0 secs.'),
('ERROR', 'action((Connection@13, 4, 4), {}) raised exception Failed. 4 retries left. Sleeping 0 secs.'),
('ERROR', 'action((Connection@14, 4, 4), {}) raised exception Failed. 3 retries left. Sleeping 0 secs.'),
('ERROR', 'action((Connection@14, 4, 4), {}) raised exception Failed. 3 retries left. Sleeping 0 secs.'),
('INFO', 'connected!'),
('INFO', 'action!'),
]