1
|
|
|
from contextvars import ContextVar |
2
|
|
|
|
3
|
|
|
import pytest |
4
|
|
|
|
5
|
|
|
from async_btree import ( |
6
|
|
|
FAILURE, |
7
|
|
|
SUCCESS, |
8
|
|
|
ExceptionDecorator, |
9
|
|
|
alias, |
10
|
|
|
always_failure, |
11
|
|
|
always_success, |
12
|
|
|
decorate, |
13
|
|
|
inverter, |
14
|
|
|
is_failure, |
15
|
|
|
is_success, |
16
|
|
|
retry, |
17
|
|
|
retry_until_failed, |
18
|
|
|
retry_until_success, |
19
|
|
|
) |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
async def a_func(): |
23
|
|
|
return 'a' |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
async def failure_func(): |
27
|
|
|
return FAILURE |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
async def success_func(): |
31
|
|
|
return SUCCESS |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
async def exception_func(): |
35
|
|
|
raise RuntimeError() |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
async def empty_func(): |
39
|
|
|
return [] |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
def test_root_name(kernel): |
43
|
|
|
rooted = alias(child=a_func, name='a_func') |
44
|
|
|
assert rooted.__node_metadata.name == 'a_func' |
45
|
|
|
assert kernel.run(rooted) == 'a' |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
def test_decorate(kernel): |
49
|
|
|
async def b_decorator(child_value, other=''): |
50
|
|
|
return f'b{child_value}{other}' |
51
|
|
|
|
52
|
|
|
assert kernel.run(decorate(a_func, b_decorator)) == 'ba' |
53
|
|
|
|
54
|
|
|
assert kernel.run(decorate(a_func, b_decorator, other='c')) == 'bac' |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
def test_always_success(kernel): |
58
|
|
|
assert kernel.run(always_success(success_func)) == SUCCESS |
59
|
|
|
assert kernel.run(always_success(failure_func)) == SUCCESS |
60
|
|
|
assert kernel.run(always_success(exception_func)) == SUCCESS |
61
|
|
|
assert kernel.run(always_success(a_func)) == 'a' |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
def test_always_failure(kernel): |
65
|
|
|
assert kernel.run(always_failure(success_func)) == FAILURE |
66
|
|
|
assert kernel.run(always_failure(failure_func)) == FAILURE |
67
|
|
|
assert not kernel.run(always_failure(exception_func)) |
68
|
|
|
assert isinstance(kernel.run(always_failure(exception_func)), ExceptionDecorator) |
69
|
|
|
assert kernel.run(always_failure(empty_func)) == [] |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
def test_is_success(kernel): |
73
|
|
|
assert kernel.run(is_success(success_func)) |
74
|
|
|
assert not kernel.run(is_success(failure_func)) |
75
|
|
|
assert not kernel.run(is_success(exception_func)) |
76
|
|
|
assert kernel.run(is_success(a_func)) |
77
|
|
|
assert not kernel.run(is_success(empty_func)) |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
def test_is_failure(kernel): |
81
|
|
|
assert not kernel.run(is_failure(success_func)) |
82
|
|
|
assert kernel.run(is_failure(failure_func)) |
83
|
|
|
assert kernel.run(is_failure(exception_func)) |
84
|
|
|
assert not kernel.run(is_failure(a_func)) |
85
|
|
|
assert kernel.run(is_failure(empty_func)) |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
def test_inverter(kernel): |
89
|
|
|
assert not kernel.run(inverter(success_func)) |
90
|
|
|
assert kernel.run(inverter(failure_func)) |
91
|
|
|
with pytest.raises(RuntimeError): |
92
|
|
|
kernel.run(inverter(exception_func)) |
93
|
|
|
assert not kernel.run(inverter(a_func)) |
94
|
|
|
assert kernel.run(inverter(empty_func)) |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
def test_retry(kernel): |
98
|
|
|
|
99
|
|
|
counter = ContextVar('counter_test_retry', default=5) |
100
|
|
|
|
101
|
|
|
async def tick(): |
102
|
|
|
value = counter.get() |
103
|
|
|
counter.set(value - 1) |
104
|
|
|
if value <= 0: |
105
|
|
|
return SUCCESS |
106
|
|
|
if value == 3: |
107
|
|
|
raise RuntimeError('3') |
108
|
|
|
return FAILURE |
109
|
|
|
|
110
|
|
|
result = kernel.run(retry(tick)) |
111
|
|
|
assert not result |
112
|
|
|
assert isinstance(result, ExceptionDecorator) |
113
|
|
|
assert kernel.run(retry(tick)) |
114
|
|
|
|
115
|
|
|
counter.set(10) |
116
|
|
|
assert kernel.run(retry(tick, max_retry=11)) |
117
|
|
|
|
118
|
|
|
counter.set(100) |
119
|
|
|
assert kernel.run(retry(tick, max_retry=-1)) |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
def test_retry_until_success(kernel): |
123
|
|
|
counter = ContextVar('counter_test_retry_until_success', default=5) |
124
|
|
|
|
125
|
|
|
async def tick(): |
126
|
|
|
value = counter.get() |
127
|
|
|
counter.set(value - 1) |
128
|
|
|
if value <= 0: |
129
|
|
|
return SUCCESS |
130
|
|
|
if value == 3: |
131
|
|
|
raise RuntimeError('3') |
132
|
|
|
return FAILURE |
133
|
|
|
|
134
|
|
|
counter.set(100) |
135
|
|
|
assert kernel.run(retry_until_success(tick)) |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
def test_retry_until_failed(kernel): |
139
|
|
|
counter = ContextVar('counter_test_retry_until_failed', default=5) |
140
|
|
|
|
141
|
|
|
async def tick(): |
142
|
|
|
value = counter.get() |
143
|
|
|
counter.set(value - 1) |
144
|
|
|
if value <= 0: |
145
|
|
|
return SUCCESS |
146
|
|
|
if value == 3: |
147
|
|
|
raise RuntimeError('3') |
148
|
|
|
return FAILURE |
149
|
|
|
|
150
|
|
|
counter.set(100) |
151
|
|
|
assert kernel.run(retry_until_failed(tick)) |
152
|
|
|
|