-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
165 lines (118 loc) · 3.39 KB
/
tests.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
from pytest import raises
from robust.tools import retry, timeout, breaker
from robust.exception import (ContinuousFailureException,
TimeoutException,
ConnectionCutException)
class TestRetryCase(object):
def test_happy_case(self):
@retry(5)
def dummy():
return 42
assert 42 == dummy()
def test_fail_case(self):
@retry(5)
def fail():
raise RuntimeError("Don't know what to do")
with raises(ContinuousFailureException):
fail()
def test_callback(self):
passed = False
def do_pass():
nonlocal passed
passed = True
@retry(5, do_pass)
def fail():
raise RuntimeError("Don't know what to do")
do_pass()
assert passed
class TestTimeoutCase(object):
def test_happy_case(self):
@timeout(1)
def dummy():
return 42
assert 42 == dummy()
def test_failure_case(self):
@timeout(1)
def fail():
while True:
pass
with raises(TimeoutException):
fail()
def test_callback(self):
passed = False
def do_pass():
nonlocal passed
passed = True
@timeout(1, do_pass)
def fail():
while True:
pass
do_pass()
assert passed
class TestBreakerPattern(object):
def test_cut_after_5_failures(self):
@breaker(limit=5, revive=30)
def fail():
raise RuntimeError("Just failing for no good reason")
with raises(ConnectionCutException):
counter = 0
while True:
try:
fail()
except RuntimeError:
pass
counter += 1
assert counter == 5
def test_revive_after_1_second(self):
import time
@breaker(limit=2, revive=1)
def fail():
raise RuntimeError("Failing and failing")
while True:
try:
fail()
except RuntimeError:
pass
except ConnectionCutException:
break
with raises(ConnectionCutException):
fail()
time.sleep(1)
with raises(RuntimeError):
fail()
with raises(ConnectionCutException):
fail()
def test_revive_with_timeout(self):
import time
@breaker(limit=1, revive=2)
@timeout(1)
def fail():
time.sleep(2)
with raises(TimeoutException):
fail()
with raises(ConnectionCutException):
fail()
time.sleep(2)
with raises(TimeoutException):
fail()
class TestTimers(object):
def test_signal_timer(self):
import time
from robust.alarm import _signal_timer
reached = False
def callback():
nonlocal reached
reached = True
_signal_timer(1, callback)
time.sleep(2)
assert reached
def test_threading_timer(self):
import time
from robust.alarm import _threading_timer
reached = False
def callback():
nonlocal reached
reached = True
_threading_timer(1, callback)
time.sleep(2)
assert reached