def test_parallel_one_state_exception(capsys): class ExceptionAfter1SecState(State): def execute(self, board): time.sleep(1) return StateStatus.EXCEPTION ws = WaitState("ws1", 5) fs = ExceptionAfter1SecState("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 0.5 second to see it is still running assert not exe.wait(0.5) assert exe.check_status(StateStatus.RUNNING) assert exe._curr_state.check_name('pm') # at this point, it should throw or raise the exception # wait another 1.5 seconds assert exe.wait(1.5) assert exe.check_status(StateStatus.EXCEPTION) assert not pm._run_thread.is_alive()
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.check_status(StateStatus.RUNNING) assert exe._curr_state.check_name('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()