forked from fgmacedo/python-statemachine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_threading.py
More file actions
188 lines (145 loc) · 5.62 KB
/
Copy pathtest_threading.py
File metadata and controls
188 lines (145 loc) · 5.62 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import threading
import time
from statemachine.state import State
from statemachine.statemachine import StateMachine
def test_machine_should_allow_multi_thread_event_changes():
"""
Test for https://github.com/fgmacedo/python-statemachine/issues/443
"""
class CampaignMachine(StateMachine):
"A workflow machine"
draft = State(initial=True)
producing = State()
closed = State(final=True)
add_job = draft.to(producing) | producing.to(closed)
machine = CampaignMachine()
def off_thread_change_state():
time.sleep(0.01)
machine.add_job()
thread = threading.Thread(target=off_thread_change_state)
thread.start()
thread.join()
assert machine.current_state.id == "producing"
def test_regression_443():
"""
Test for https://github.com/fgmacedo/python-statemachine/issues/443
"""
time_collecting = 0.2
time_to_send = 0.125
time_sampling_current_state = 0.05
class TrafficLightMachine(StateMachine):
"A traffic light machine"
green = State(initial=True)
yellow = State()
red = State()
cycle = green.to(yellow) | yellow.to(red) | red.to(green)
class Controller:
def __init__(self):
self.statuses_history = []
self.fsm = TrafficLightMachine()
# set up thread
t = threading.Thread(target=self.recv_cmds)
t.start()
def recv_cmds(self):
"""Pretend we receive a command triggering a state change after Xs."""
waiting_time = 0
sent = False
while waiting_time < time_collecting:
if waiting_time >= time_to_send and not sent:
self.fsm.cycle()
sent = True
waiting_time += time_sampling_current_state
self.statuses_history.append(self.fsm.current_state.id)
time.sleep(time_sampling_current_state)
c1 = Controller()
c2 = Controller()
time.sleep(time_collecting + 0.01)
assert c1.statuses_history == ["green", "green", "green", "yellow"]
assert c2.statuses_history == ["green", "green", "green", "yellow"]
def test_regression_443_with_modifications():
"""
Test for https://github.com/fgmacedo/python-statemachine/issues/443
"""
time_collecting = 0.2
time_to_send = 0.125
time_sampling_current_state = 0.05
class TrafficLightMachine(StateMachine):
"A traffic light machine"
green = State(initial=True)
yellow = State()
red = State()
cycle = green.to(yellow) | yellow.to(red) | red.to(green)
def __init__(self, name):
self.name = name
self.statuses_history = []
super().__init__()
def beat(self):
waiting_time = 0
sent = False
while waiting_time < time_collecting:
if waiting_time >= time_to_send and not sent:
self.cycle()
sent = True
self.statuses_history.append(f"{self.name}.{self.current_state.id}")
time.sleep(time_sampling_current_state)
waiting_time += time_sampling_current_state
class Controller:
def __init__(self, name):
self.fsm = TrafficLightMachine(name)
# set up thread
t = threading.Thread(target=self.fsm.beat)
t.start()
c1 = Controller("c1")
c2 = Controller("c2")
c3 = Controller("c3")
time.sleep(time_collecting + 0.01)
assert c1.fsm.statuses_history == ["c1.green", "c1.green", "c1.green", "c1.yellow"]
assert c2.fsm.statuses_history == ["c2.green", "c2.green", "c2.green", "c2.yellow"]
assert c3.fsm.statuses_history == ["c3.green", "c3.green", "c3.green", "c3.yellow"]
async def test_regression_443_with_modifications_for_async_engine(): # noqa: C901
"""
Test for https://github.com/fgmacedo/python-statemachine/issues/443
"""
time_collecting = 0.2
time_to_send = 0.125
time_sampling_current_state = 0.05
class TrafficLightMachine(StateMachine):
"A traffic light machine"
green = State(initial=True)
yellow = State()
red = State()
cycle = green.to(yellow) | yellow.to(red) | red.to(green)
async def on_cycle(self):
return "caution"
def __init__(self, name):
self.name = name
self.statuses_history = []
super().__init__()
def beat(self):
waiting_time = 0
sent = False
while waiting_time < time_collecting:
if waiting_time >= time_to_send and not sent:
self.cycle()
sent = True
self.statuses_history.append(f"{self.name}.{self.current_state.id}")
time.sleep(time_sampling_current_state)
waiting_time += time_sampling_current_state
class Controller:
def __init__(self, name):
self.fsm = TrafficLightMachine(name)
async def start(self):
# set up thread
await self.fsm.activate_initial_state()
t = threading.Thread(target=self.fsm.beat)
t.start()
c1 = Controller("c1")
c2 = Controller("c2")
c3 = Controller("c3")
await c1.start()
await c2.start()
await c3.start()
time.sleep(time_collecting + 0.01)
assert c1.fsm.statuses_history == ["c1.green", "c1.green", "c1.green", "c1.yellow"]
assert c2.fsm.statuses_history == ["c2.green", "c2.green", "c2.green", "c2.yellow"]
assert c3.fsm.statuses_history == ["c3.green", "c3.green", "c3.green", "c3.yellow"]