|
1
|
|
|
from contextvars import ContextVar |
|
2
|
|
|
|
|
3
|
|
|
from curio import run |
|
4
|
|
|
|
|
5
|
|
|
from libellule.control_flow.common import * |
|
6
|
|
|
from libellule.control_flow.control import * |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
async def a_func(): |
|
10
|
|
|
return "a" |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
async def b_func(): |
|
14
|
|
|
return "b" |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
async def failure_func(): |
|
18
|
|
|
return FAILURE |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
async def success_func(): |
|
22
|
|
|
return SUCCESS |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
async def exception_func(): |
|
26
|
|
|
raise RuntimeError() |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
def test_sequence(): |
|
30
|
|
|
assert not run( |
|
31
|
|
|
sequence(children=[a_func, failure_func, success_func]) |
|
|
|
|
|
|
32
|
|
|
), "default behaviour fail of one failed" |
|
33
|
|
|
|
|
34
|
|
|
assert run( |
|
35
|
|
|
sequence(children=[a_func, failure_func, success_func], succes_threshold=2) |
|
36
|
|
|
) |
|
37
|
|
|
assert run( |
|
38
|
|
|
sequence(children=[a_func, success_func, failure_func], succes_threshold=2) |
|
39
|
|
|
) |
|
40
|
|
|
assert run( |
|
41
|
|
|
sequence(children=[failure_func, a_func, success_func], succes_threshold=2) |
|
42
|
|
|
) |
|
43
|
|
|
|
|
44
|
|
|
assert run( |
|
45
|
|
|
sequence(children=[exception_func, failure_func, a_func], succes_threshold=1) |
|
46
|
|
|
) |
|
47
|
|
|
assert not run( |
|
48
|
|
|
sequence(children=[exception_func, failure_func], succes_threshold=1) |
|
49
|
|
|
) |
|
50
|
|
|
|
|
51
|
|
|
assert not run(sequence(children=[])) |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
def test_fallback(): |
|
55
|
|
|
assert run(fallback(children=[exception_func, failure_func, a_func])) |
|
|
|
|
|
|
56
|
|
|
assert not run(fallback(children=[exception_func, failure_func])) |
|
57
|
|
|
assert not run(fallback(children=[])) |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
def test_selector(): |
|
61
|
|
|
assert run(selector(children=[exception_func, failure_func, a_func])) |
|
|
|
|
|
|
62
|
|
|
assert not run(selector(children=[exception_func, failure_func])) |
|
63
|
|
|
assert selector(children=[]).__node_metadata.name == "selector" |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
def test_decision(): |
|
67
|
|
|
assert run(decision(condition=success_func, success_tree=a_func)) == "a" |
|
|
|
|
|
|
68
|
|
|
assert not run(decision(condition=failure_func, success_tree=a_func)) |
|
69
|
|
|
assert ( |
|
70
|
|
|
run(decision(condition=failure_func, success_tree=a_func, failure_tree=b_func)) |
|
71
|
|
|
== "b" |
|
72
|
|
|
) |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
def test_repeat_until(): |
|
76
|
|
|
|
|
77
|
|
|
counter = ContextVar("counter", default=5) |
|
78
|
|
|
|
|
79
|
|
|
async def tick(): |
|
80
|
|
|
value = counter.get() |
|
81
|
|
|
counter.set(value - 1) |
|
82
|
|
|
if value <= 0: |
|
83
|
|
|
return FAILURE |
|
|
|
|
|
|
84
|
|
|
if value == 3: |
|
85
|
|
|
raise RuntimeError("3") |
|
86
|
|
|
return SUCCESS |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
assert run(repeat_until(condition=tick, child=a_func)) == "a" |
|
|
|
|
|
|
89
|
|
|
|