def test_parallel_one_state_fails(capsys): class FailAfter1SecState(State): def execute(self, board): time.sleep(1) return StateStatus.FAILED ws = WaitState("ws1", 5) fs = FailAfter1SecState("fs") es = IdleState("es") fes = IdleState("fs-terminal") pm = ParallelState('pm', [ws, fs]) pm.add_transition_on_success(es) pm.add_transition_on_failed(fes) exe = Machine("main_machine", pm, end_state_ids=['es', 'fs-terminal'], rate=10) # run machine and see how it reacts exe.start(None) # wait for one second assert not exe.wait(0.5) assert exe.checkStatus(StateStatus.RUNNING) assert exe._curr_state.checkName('pm') # at this point ws should be done but ws2 is still going # wait another one seconds assert exe.wait(2) assert exe._curr_state == fes assert not pm._run_thread.is_alive()
def test_parallel_state_in_machine(capsys): ws = WaitState("ws1", 1) ws2 = WaitState("ws2", 2) es = IdleState("es") pm = ParallelState('pm', [ws, ws2]) pm.add_transition_on_success(es) exe = Machine("main_machine", pm, end_state_ids=['es'], rate=10) # run machine and see how it reacts exe.start(None) # wait for one second assert not exe.wait(1.1) assert ws.checkStatus(StateStatus.SUCCESS) assert ws2.checkStatus(StateStatus.RUNNING) assert exe.checkStatus(StateStatus.RUNNING) # at this point ws should be done but ws2 is still going # wait another one seconds assert exe.wait(2) assert exe.checkStatus(StateStatus.SUCCESS) assert not pm._run_thread.is_alive()
def test_machine_with_exception(capsys): ps1 = PrintState("ps1", "p1") re1 = RaiseExceptionState('re1') ps2 = PrintState("ps2", "p2") ps1.add_transition_on_success(re1) re1.add_transition_on_success(ps2) mac = Machine("mac", ps1, ["ps2"]) mac.run() assert capsys.readouterr().out == 'p1\n' assert mac.checkStatus(StateStatus.EXCEPTIION) assert str(mac._internal_exception) == "raiseException" assert mac._exception_raised_state_name == "mac.re1"
def test_exception_in_parallel_state(capsys): error_text = "IndexErrorInTestEXCEPTION" class RaiseExceptionState(State): def execute(self, board): raise IndexError(error_text) ws = WaitState("ws1", 10) re = RaiseExceptionState("re") pm = ParallelState('pm', [ws, re]) es = IdleState("es") pm.add_transition_on_success(es) exe = Machine("xe", pm, end_state_ids=['es', 'onException'], rate=10) # run machine and see how it reacted exe.start(None) exe.wait(0.5) assert exe._curr_state == pm assert exe.checkStatus(StateStatus.EXCEPTIION) assert str(exe._internal_exception) == error_text assert exe._exception_raised_state_name == "xe.pm.re" assert not pm._run_thread.is_alive()