/
test_flow.py
179 lines (125 loc) · 3.75 KB
/
test_flow.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
from time import time
import pytest
from funcy.flow import *
def test_silent():
assert silent(int)(1) == 1
assert silent(int)('1') == 1
assert silent(int)('hello') is None
assert silent(str.upper)('hello') == 'HELLO'
class MyError(Exception):
pass
def test_ignore():
assert ignore(Exception)(raiser(Exception))() is None
assert ignore(Exception)(raiser(MyError))() is None
assert ignore((TypeError, MyError))(raiser(MyError))() is None
with pytest.raises(TypeError):
ignore(MyError)(raiser(TypeError))()
assert ignore(MyError, default=42)(raiser(MyError))() == 42
def test_raiser():
with pytest.raises(Exception) as e: raiser()()
assert e.type is Exception
with pytest.raises(MyError): raiser(MyError)()
with pytest.raises(MyError) as e: raiser(MyError, 'some message')()
assert e.value.args == ('some message',)
with pytest.raises(MyError): raiser(MyError('some message'))()
with pytest.raises(MyError): raiser(MyError)('junk', keyword='junk')
def test_suppress():
with suppress(Exception):
raise Exception
with suppress(Exception):
raise MyError
with pytest.raises(TypeError):
with suppress(MyError):
raise TypeError
with suppress(TypeError, MyError):
raise MyError
def test_retry():
calls = []
def failing(n=1):
if len(calls) < n:
calls.append(1)
raise MyError
return 1
with pytest.raises(MyError): failing()
calls = []
assert retry(2, MyError)(failing)() == 1
calls = []
with pytest.raises(MyError): retry(2, MyError)(failing)(2)
def test_retry_timeout():
def failing():
raise MyError
# sleep only between tries, so retry is 11, but sleep summary is ~0.1 sec
start_time = time()
with pytest.raises(MyError): retry(11, MyError, timeout=0.01)(failing)()
assert 0.1 < time() - start_time < 0.11
# exponential timeout
start_time = time()
with pytest.raises(MyError): retry(4, MyError, timeout=lambda a: 0.01 * 2 ** a)(failing)()
d = time() - start_time
assert 0.07 < d < 0.08
def test_retry_many_errors():
calls = []
def failing(n=1):
if len(calls) < n:
calls.append(1)
raise MyError
return 1
assert retry(2, (MyError, RuntimeError))(failing)() == 1
calls = []
assert retry(2, [MyError, RuntimeError])(failing)() == 1
def test_fallback():
assert fallback(raiser(), lambda: 1) == 1
with pytest.raises(Exception): fallback((raiser(), MyError), lambda: 1)
assert fallback((raiser(MyError), MyError), lambda: 1) == 1
def test_limit_error_rate():
calls = []
@limit_error_rate(2, 60, MyError)
def limited(x):
calls.append(x)
raise TypeError
with pytest.raises(TypeError): limited(1)
with pytest.raises(TypeError): limited(2)
with pytest.raises(MyError): limited(3)
assert calls == [1, 2]
def test_post_processing():
@post_processing(max)
def my_max(l):
return l
assert my_max([1, 3, 2]) == 3
def test_collecting():
@collecting
def doubles(l):
for i in l:
yield i * 2
assert doubles([1, 2]) == [2, 4]
def test_once():
calls = []
@once
def call(n):
calls.append(n)
return n
call(1)
call(2)
assert calls == [1]
def test_once_per():
calls = []
@once_per('n')
def call(n, x=None):
calls.append(n)
return n
call(1)
call(2)
call(1, 42)
assert calls == [1, 2]
def test_once_per_args():
calls = []
@once_per_args
def call(n, x=None):
calls.append(n)
return n
call(1)
call(2)
call(1, 42)
assert calls == [1, 2, 1]
call(1)
assert calls == [1, 2, 1]