-
Notifications
You must be signed in to change notification settings - Fork 13
/
test_automations.py
146 lines (120 loc) · 3.93 KB
/
test_automations.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
import pytest
import rosys
from rosys.automation import Automator
from rosys.driving import Driver
from rosys.geometry import Pose, Spline
from rosys.hardware import Robot
from rosys.testing import assert_pose, forward
async def test_pause_and_resume_spline(driver: Driver, automator: Automator, robot: Robot):
spline = Spline.from_poses(Pose(x=0, y=0, yaw=0), Pose(x=2, y=0, yaw=0))
automator.start(driver.drive_spline(spline))
await forward(x=1)
assert_pose(1, 0, deg=0)
automator.pause(because='test')
await forward(seconds=1.0)
assert_pose(1, 0, deg=0)
automator.resume()
await forward(x=2)
assert_pose(2, 0, deg=0)
@pytest.mark.parametrize('dx', [2, 10])
async def test_driving_a_spline(driver: Driver, automator: Automator, robot: Robot, dx: float):
assert_pose(0, 0, deg=0)
spline = Spline.from_poses(Pose(x=0, y=0, yaw=0), Pose(x=dx, y=1, yaw=0))
automator.start(driver.drive_spline(spline))
await forward(x=dx)
assert_pose(dx, 1, deg=0, deg_tolerance=5)
async def test_aborting_a_drive(driver: Driver, automator: Automator, robot: Robot):
assert_pose(0, 0, deg=0)
automator.start(driver.drive_spline(Spline.from_poses(Pose(x=0), Pose(x=2))))
cause: list[str] = []
automator.AUTOMATION_STOPPED.register(cause.append)
await forward(x=1)
assert_pose(1, 0, deg=0)
driver.abort()
await forward(seconds=1)
assert_pose(1, 0, deg=0)
assert cause == ['an exception occurred in an automation']
async def test_finally_block(automator: Automator):
events: list[str] = []
async def run() -> None:
try:
while True:
events.append('tick')
await rosys.sleep(3)
finally:
events.append('tock')
automator.start(run())
await forward(seconds=10)
automator.stop(because='test')
await forward(seconds=1)
assert events == ['tick', 'tick', 'tick', 'tick', 'tock']
async def test_parallelize(automator: Automator):
events: list[str] = []
async def slow():
try:
for i in range(5):
events.append(f'slow {i}')
await rosys.sleep(0.5)
finally:
events.append('slow done')
async def fast():
try:
for i in range(5):
events.append(f'fast {i}')
await rosys.sleep(0.2)
finally:
events.append('fast done')
async def run(*, return_when_first_completed: bool):
await rosys.automation.parallelize(slow(), fast(), return_when_first_completed=return_when_first_completed)
events.clear()
automator.start(run(return_when_first_completed=True))
await forward(seconds=10)
assert events == [
'slow 0',
'fast 0',
'fast 1',
'fast 2',
'slow 1',
'fast 3',
'fast 4',
'slow 2',
'fast done',
'slow done',
]
assert automator.is_stopped
events.clear()
automator.start(run(return_when_first_completed=False))
await forward(seconds=10)
assert events == [
'slow 0',
'fast 0',
'fast 1',
'fast 2',
'slow 1',
'fast 3',
'fast 4',
'slow 2',
'fast done',
'slow 3',
'slow 4',
'slow done',
]
assert automator.is_stopped
async def test_parallelize_exception(automator: Automator):
failures: list[str] = []
automator.AUTOMATION_FAILED.register(failures.append)
async def slow():
for i in range(5):
print(f'slow {i}')
if i == 3:
raise ValueError('i is 3')
await rosys.sleep(0.5)
async def fast():
for i in range(5):
print(f'fast {i}')
await rosys.sleep(0.2)
async def run():
await rosys.automation.parallelize(slow(), fast())
automator.start(run())
await forward(seconds=10)
assert failures == ['automation aborted because of i is 3']