|
1
|
|
|
from contextvars import ContextVar |
|
2
|
|
|
|
|
3
|
|
|
from async_btree import ( |
|
4
|
|
|
FAILURE, |
|
5
|
|
|
SUCCESS, |
|
6
|
|
|
ExceptionDecorator, |
|
7
|
|
|
decision, |
|
8
|
|
|
fallback, |
|
9
|
|
|
repeat_until, |
|
10
|
|
|
selector, |
|
11
|
|
|
sequence, |
|
12
|
|
|
) |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
async def a_func(): |
|
16
|
|
|
return 'a' |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
async def b_func(): |
|
20
|
|
|
return 'b' |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
async def failure_func(): |
|
24
|
|
|
return FAILURE |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
async def success_func(): |
|
28
|
|
|
return SUCCESS |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
async def exception_func(): |
|
32
|
|
|
raise RuntimeError("ops") |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
def test_sequence(kernel): |
|
36
|
|
|
assert not kernel.run( |
|
37
|
|
|
sequence(children=[a_func, failure_func, success_func]) |
|
38
|
|
|
), "default behaviour fail of one failed" |
|
39
|
|
|
|
|
40
|
|
|
assert kernel.run( |
|
41
|
|
|
sequence(children=[a_func, failure_func, success_func], succes_threshold=2) |
|
42
|
|
|
) |
|
43
|
|
|
assert kernel.run( |
|
44
|
|
|
sequence(children=[a_func, success_func, failure_func], succes_threshold=2) |
|
45
|
|
|
) |
|
46
|
|
|
assert kernel.run( |
|
47
|
|
|
sequence(children=[failure_func, a_func, success_func], succes_threshold=2) |
|
48
|
|
|
), 'must continue after first failure' |
|
49
|
|
|
|
|
50
|
|
|
assert kernel.run( |
|
51
|
|
|
sequence(children=[exception_func, failure_func, a_func], succes_threshold=1) |
|
52
|
|
|
) |
|
53
|
|
|
assert not kernel.run( |
|
54
|
|
|
sequence(children=[exception_func, failure_func], succes_threshold=1) |
|
55
|
|
|
) |
|
56
|
|
|
|
|
57
|
|
|
assert not kernel.run(sequence(children=[])) |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
def test_fallback(kernel): |
|
61
|
|
|
assert kernel.run(fallback(children=[exception_func, failure_func, a_func])) |
|
62
|
|
|
assert not kernel.run(fallback(children=[exception_func, failure_func])) |
|
63
|
|
|
assert not kernel.run(fallback(children=[])) |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
def test_selector(kernel): |
|
67
|
|
|
assert kernel.run(selector(children=[exception_func, failure_func, a_func])) |
|
68
|
|
|
assert not kernel.run(selector(children=[exception_func, failure_func])) |
|
69
|
|
|
assert selector(children=[]).__node_metadata.name == 'selector' |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
def test_decision(kernel): |
|
73
|
|
|
assert kernel.run(decision(condition=success_func, success_tree=a_func)) == 'a' |
|
74
|
|
|
assert not kernel.run(decision(condition=failure_func, success_tree=a_func)) |
|
75
|
|
|
|
|
76
|
|
|
result = kernel.run( |
|
77
|
|
|
decision(condition=failure_func, success_tree=a_func, failure_tree=b_func) |
|
78
|
|
|
) |
|
79
|
|
|
assert result == 'b', 'failure tree must be called' |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
def test_repeat_until_falsy_condition(kernel): |
|
83
|
|
|
|
|
84
|
|
|
counter = ContextVar('counter', default=5) |
|
85
|
|
|
|
|
86
|
|
|
async def tick(): |
|
87
|
|
|
value = counter.get() |
|
88
|
|
|
counter.set(value - 1) |
|
89
|
|
|
if value <= 0: |
|
90
|
|
|
return FAILURE |
|
91
|
|
|
if value == 3: |
|
92
|
|
|
raise RuntimeError('3') |
|
93
|
|
|
return SUCCESS |
|
94
|
|
|
|
|
95
|
|
|
assert ( |
|
96
|
|
|
kernel.run(repeat_until(condition=tick, child=a_func)) == 'a' |
|
97
|
|
|
), 'return last sucess result' |
|
98
|
|
|
assert counter.get() == 2 |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
def test_repeat_until_return_last_result(kernel): |
|
102
|
|
|
|
|
103
|
|
|
counter = ContextVar('tick_test_repeat_until_return_last_result', default=5) |
|
104
|
|
|
|
|
105
|
|
|
async def tick(): |
|
106
|
|
|
value = counter.get() |
|
107
|
|
|
counter.set(value - 1) |
|
108
|
|
|
if value <= 0: |
|
109
|
|
|
return FAILURE |
|
110
|
|
|
return SUCCESS |
|
111
|
|
|
|
|
112
|
|
|
result = kernel.run(repeat_until(condition=tick, child=exception_func)) |
|
113
|
|
|
assert counter.get() == -1 |
|
114
|
|
|
assert isinstance(result, ExceptionDecorator) |
|
115
|
|
|
|