|
| 1 | +import threading |
| 2 | +import time |
| 3 | + |
| 4 | +from statemachine.state import State |
| 5 | +from statemachine.statemachine import StateMachine |
| 6 | + |
| 7 | + |
| 8 | +def test_machine_should_allow_multi_thread_event_changes(): |
| 9 | + """ |
| 10 | + Test for https://github.com/fgmacedo/python-statemachine/issues/443 |
| 11 | + """ |
| 12 | + |
| 13 | + class CampaignMachine(StateMachine): |
| 14 | + "A workflow machine" |
| 15 | + |
| 16 | + draft = State(initial=True) |
| 17 | + producing = State() |
| 18 | + closed = State() |
| 19 | + add_job = draft.to(producing) | producing.to(closed) |
| 20 | + |
| 21 | + machine = CampaignMachine() |
| 22 | + |
| 23 | + def off_thread_change_state(): |
| 24 | + time.sleep(0.01) |
| 25 | + machine.add_job() |
| 26 | + |
| 27 | + thread = threading.Thread(target=off_thread_change_state) |
| 28 | + thread.start() |
| 29 | + thread.join() |
| 30 | + assert machine.current_state.id == "producing" |
| 31 | + |
| 32 | + |
| 33 | +def test_regression_443(): |
| 34 | + """ |
| 35 | + Test for https://github.com/fgmacedo/python-statemachine/issues/443 |
| 36 | + """ |
| 37 | + time_collecting = 0.2 |
| 38 | + time_to_send = 0.125 |
| 39 | + time_sampling_current_state = 0.05 |
| 40 | + |
| 41 | + class TrafficLightMachine(StateMachine): |
| 42 | + "A traffic light machine" |
| 43 | + |
| 44 | + green = State(initial=True) |
| 45 | + yellow = State() |
| 46 | + red = State() |
| 47 | + |
| 48 | + cycle = green.to(yellow) | yellow.to(red) | red.to(green) |
| 49 | + |
| 50 | + class Controller: |
| 51 | + def __init__(self): |
| 52 | + self.statuses_history = [] |
| 53 | + self.fsm = TrafficLightMachine() |
| 54 | + # set up thread |
| 55 | + t = threading.Thread(target=self.recv_cmds) |
| 56 | + t.start() |
| 57 | + |
| 58 | + def recv_cmds(self): |
| 59 | + """Pretend we receive a command triggering a state change after Xs.""" |
| 60 | + waiting_time = 0 |
| 61 | + sent = False |
| 62 | + while waiting_time < time_collecting: |
| 63 | + if waiting_time >= time_to_send and not sent: |
| 64 | + self.fsm.cycle() |
| 65 | + sent = True |
| 66 | + |
| 67 | + waiting_time += time_sampling_current_state |
| 68 | + self.statuses_history.append(self.fsm.current_state.id) |
| 69 | + time.sleep(time_sampling_current_state) |
| 70 | + |
| 71 | + c1 = Controller() |
| 72 | + c2 = Controller() |
| 73 | + time.sleep(time_collecting + 0.01) |
| 74 | + assert c1.statuses_history == ["green", "green", "green", "yellow"] |
| 75 | + assert c2.statuses_history == ["green", "green", "green", "yellow"] |
| 76 | + |
| 77 | + |
| 78 | +def test_regression_443_with_modifications(): |
| 79 | + """ |
| 80 | + Test for https://github.com/fgmacedo/python-statemachine/issues/443 |
| 81 | + """ |
| 82 | + time_collecting = 0.2 |
| 83 | + time_to_send = 0.125 |
| 84 | + time_sampling_current_state = 0.05 |
| 85 | + |
| 86 | + class TrafficLightMachine(StateMachine): |
| 87 | + "A traffic light machine" |
| 88 | + |
| 89 | + green = State(initial=True) |
| 90 | + yellow = State() |
| 91 | + red = State() |
| 92 | + |
| 93 | + cycle = green.to(yellow) | yellow.to(red) | red.to(green) |
| 94 | + |
| 95 | + def __init__(self, name): |
| 96 | + self.name = name |
| 97 | + self.statuses_history = [] |
| 98 | + super().__init__() |
| 99 | + |
| 100 | + def beat(self): |
| 101 | + waiting_time = 0 |
| 102 | + sent = False |
| 103 | + while waiting_time < time_collecting: |
| 104 | + if waiting_time >= time_to_send and not sent: |
| 105 | + self.cycle() |
| 106 | + sent = True |
| 107 | + |
| 108 | + self.statuses_history.append(f"{self.name}.{self.current_state.id}") |
| 109 | + |
| 110 | + time.sleep(time_sampling_current_state) |
| 111 | + waiting_time += time_sampling_current_state |
| 112 | + |
| 113 | + class Controller: |
| 114 | + def __init__(self, name): |
| 115 | + self.fsm = TrafficLightMachine(name) |
| 116 | + # set up thread |
| 117 | + t = threading.Thread(target=self.fsm.beat) |
| 118 | + t.start() |
| 119 | + |
| 120 | + c1 = Controller("c1") |
| 121 | + c2 = Controller("c2") |
| 122 | + c3 = Controller("c3") |
| 123 | + time.sleep(time_collecting + 0.01) |
| 124 | + |
| 125 | + assert c1.fsm.statuses_history == ["c1.green", "c1.green", "c1.green", "c1.yellow"] |
| 126 | + assert c2.fsm.statuses_history == ["c2.green", "c2.green", "c2.green", "c2.yellow"] |
| 127 | + assert c3.fsm.statuses_history == ["c3.green", "c3.green", "c3.green", "c3.yellow"] |
0 commit comments