forked from litl/backoff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackoff_tests.py
117 lines (88 loc) · 2.73 KB
/
backoff_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
# coding:utf-8
import backoff
import mock
import pytest
def test_expo():
gen = backoff.expo()
for i in range(9):
assert 2 ** i == next(gen)
def test_expo_base3():
gen = backoff.expo(base=3)
for i in range(9):
assert 3 ** i == next(gen)
def test_expo_max_value():
gen = backoff.expo(max_value=2 ** 4)
expected = [1, 2, 4, 8, 16, 16, 16]
for expect in expected:
assert expect == next(gen)
def test_fibo():
gen = backoff.fibo()
expected = [1, 1, 2, 3, 5, 8, 13]
for expect in expected:
assert expect == next(gen)
def test_fibo_max_value():
gen = backoff.fibo(max_value=8)
expected = [1, 1, 2, 3, 5, 8, 8, 8]
for expect in expected:
assert expect == next(gen)
def test_constant():
gen = backoff.constant(interval=3)
for i in range(9):
assert 3 == next(gen)
@mock.patch('time.sleep', lambda x: None)
def test_on_predicate():
@backoff.on_predicate(backoff.expo)
def return_true(log, n):
val = (len(log) == n - 1)
log.append(val)
return val
log = []
ret = return_true(log, 3)
assert True == ret
assert 3 == len(log)
@mock.patch('time.sleep', lambda x: None)
def test_on_predicate_max_tries():
@backoff.on_predicate(backoff.expo, max_tries=3)
def return_true(log, n):
val = (len(log) == n)
log.append(val)
return val
log = []
ret = return_true(log, 10)
assert False == ret
assert 3 == len(log)
@mock.patch('time.sleep', lambda x: None)
def test_on_exception():
@backoff.on_exception(backoff.expo, KeyError)
def keyerror_then_true(log, n):
if len(log) == n:
return True
e = KeyError()
log.append(e)
raise e
log = []
assert True == keyerror_then_true(log, 3)
assert 3 == len(log)
@mock.patch('time.sleep', lambda x: None)
def test_on_exception_max_tries():
@backoff.on_exception(backoff.expo, KeyError, max_tries=3)
def keyerror_then_true(log, n, foo=None):
if len(log) == n:
return True
e = KeyError()
log.append(e)
raise e
log = []
with pytest.raises(KeyError):
keyerror_then_true(log, 10, foo="bar")
assert 3 == len(log)
def test_invoc_repr():
def func(a, b, c=None):
pass
assert "func(a, b, c=c)" == backoff._invoc_repr(func,
["a", "b"],
{"c": "c"})
assert "func(c=c)" == backoff._invoc_repr(func, [], {"c": "c"})
assert "func(a, b)" == backoff._invoc_repr(func, ["a", "b"], {})
assert u"func(ユニコーン, ア=あ)" == \
backoff._invoc_repr(func, [u"ユニコーン"], {u"ア": u"あ"})