def test_object_set_get(capsys): class SetState(State): def execute(self, board: Board): obj = {'hello': [1, 2, 3], 'name': {'first': 'test'}} board.set('obj', obj) obj['name'] = {} return StateStatus.SUCCESS class GetState(State): def execute(self, board): obj = board.get('obj') assert obj['hello'] == [1, 2, 3] assert obj['name']['first'] == 'test' return StateStatus.SUCCESS s = SetState('s') g = GetState('g') w = WaitState('w', 1) s.add_transition_on_success(w) w.add_transition_on_success(g) exe = Machine('xe', s, end_state_ids=['g']) exe.run() assert exe.is_end() assert exe._curr_state._status == StateStatus.SUCCESS
def test_interrupt_machine(capsys): s1 = WaitState('s1', 1.1) s2 = DummyState('s2') s1.add_transition_on_success(s2) mac = Machine("mac", s1, ["s2"], debug=True, rate=1) mac.start(None) assert mac.interrupt()
def test_wait_state(): s1 = WaitState("s1", 2) s2 = IdleState("s2") s1.add_transition_on_success(s2) exe = Machine("test", s1, end_state_ids=['s2'], rate=10) start_time = time.time() exe.run() duration = time.time() - start_time # Because the waut these are executed, its hard to know the margin assert duration == pytest.approx(2, rel=0.1)
def test_repeat_sequential_state(): w1 = WaitState('w1', 0.3) w2 = WaitState('w2', 0.3) seqs = SequentialState('seqs', [w1, w2]) seqs.start(None) seqs.wait(1) info = seqs.get_debug_info() assert info['children'][0]['status'] == StateStatus.SUCCESS assert info['children'][1]['status'] == StateStatus.SUCCESS seqs.start(None) seqs.wait(0.1) info = seqs.get_debug_info() assert info['children'][0]['status'] == StateStatus.RUNNING assert info['children'][1]['status'] == StateStatus.NOT_RUNNING
def test_debugging_machine(capsys): from behavior_machine import logging logging.add_fs('capsys', sys.stdout) s1 = WaitState('s1', 1.1) s2 = DummyState('s2') s1.add_transition_on_success(s2) mac = Machine("mac", s1, ["s2"], debug=True, rate=1) mac.run() assert mac.is_end() assert capsys.readouterr().out == ("[Base] mac(Machine) -- RUNNING\n" " -> s1(WaitState) -- RUNNING\n" "[Base] mac(Machine) -- RUNNING\n" " -> s2(DummyState) -- SUCCESS\n")
def test_parallel_debug_info(): w1 = WaitState('w1', 0.3) w2 = WaitState('w2', 0.3) pm = ParallelState("pm", [w1, w2]) pm.start(None) pm.wait(0.1) info = pm.get_debug_info() assert info['name'] == 'pm' assert len(info['children']) == 2 assert info['children'][0]['name'] == 'w1' assert info['children'][0]['status'] == StateStatus.RUNNING assert info['children'][1]['name'] == 'w2' assert info['children'][1]['status'] == StateStatus.RUNNING pm.interrupt()
def test_interrupt_in_parallel_state(capsys): ws = WaitState("ws1", 1) ws2 = WaitState("ws2", 2) es = IdleState("es") pm = ParallelState('pm', [ws, ws2]) pm.add_transition_on_success(es) pm.add_transition_after_elapsed(es, 0.1) exe = Machine("main_machine", pm, end_state_ids=['es'], rate=10) # run machine exe.start(None) # because of the elapsed transition, the machine will immediate transition to the end state in 0.1 seconds assert exe.wait(0.2) assert ws._status == StateStatus.INTERRUPTED assert ws2._status == StateStatus.INTERRUPTED assert not pm._run_thread.is_alive()
def test_machine_with_exception_in_transition_with_zombie_states(capsys): ws1 = WaitState('ws1', 10) is2 = DummyState('d2') ws1.add_transition(lambda s, b: s.unknown(), is2) mac = Machine("mac", ws1, ["is2"]) mac.run() assert mac._status == StateStatus.EXCEPTION # this is an interrupted, because exception happen at higher level assert ws1._status == StateStatus.INTERRUPTED assert not mac._run_thread.is_alive() assert not ws1._run_thread.is_alive() assert is2._run_thread is None # Never reach it
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()
def test_sequential_debug_info(): w1 = WaitState('w1', 1) w2 = WaitState('w2', 1) seqs = SequentialState('seqs', [w1, w2]) seqs.start(None) seqs.wait(0.1) info = seqs.get_debug_info() assert info['name'] == 'seqs' assert len(info['children']) == 2 assert info['children'][0]['name'] == 'w1' assert info['children'][0]['status'] == StateStatus.RUNNING assert info['children'][1]['name'] == 'w2' assert info['children'][1]['status'] == StateStatus.UNKNOWN seqs.wait(1) info = seqs.get_debug_info() assert info['children'][0]['status'] == StateStatus.SUCCESS assert info['children'][1]['status'] == StateStatus.RUNNING seqs.wait()
def test_wait_state_with_interrupt(): s1 = WaitState("s1", 10) start_time = time.time() s1.start(None) s1.interrupt() s1.wait() duration = time.time() - start_time # Because the waut these are executed, its hard to know the margin # should be really close to zero because we interrupt immediately after it started. assert duration == pytest.approx(0.0, abs=1e-3)
def test_debugging_machine(caplog): import logging logging.basicConfig(level=logging.DEBUG) caplog.set_level(logging.DEBUG) logger = logging.getLogger(__name__) s1 = WaitState('s1', 1.1) s2 = DummyState('s2') s1.add_transition_on_success(s2) mac = Machine("mac", s1, ["s2"], debug=True, rate=1, logger=logger) mac.run() assert mac.is_end() assert len(caplog.records) == 3 assert caplog.records[ 0].message == "[Base] mac(Machine) -- RUNNING\n -> s1(WaitState) -- RUNNING" # This is at t=0 assert caplog.records[ 1].message == "[Base] mac(Machine) -- RUNNING\n -> s1(WaitState) -- RUNNING" # This is at t=1 assert caplog.records[ 2].message == "[Base] mac(Machine) -- RUNNING\n -> s2(DummyState) -- SUCCESS" # At the end
def test_machine_rate_fast(): w1 = WaitState("w1", 0.05) # execute at second 0 w2 = WaitState("w2", 0.05) # execute at second 0.1s es = DummyState("endState") # execute at second 0.2 w1.add_transition_on_success(w2) w2.add_transition_on_success(es) exe = Machine("xe", w1, end_state_ids=["endState"], rate=10) start_time = time.time() exe.run() duration = time.time() - start_time assert pytest.approx(duration, abs=1e-2) == 0.2 assert w1._status == StateStatus.SUCCESS assert w2._status == StateStatus.SUCCESS
def test_parallel_state_individual(capsys): ws = WaitState("ws1", 1) ws2 = WaitState("ws2", 2) pm = ParallelState('pm', [ws, ws2]) pm.start(None) # wait for one second outside time.sleep(1) pm.tick(None) # ticks to force event flags if any assert not pm.wait(0.1) assert pm.check_status(StateStatus.RUNNING) # at this point ws should be done but ws2 is still going time.sleep(1.1) pm.tick(None) # ticks to force event flags if any # wait assert pm.wait(0.1) assert not pm.check_status(StateStatus.RUNNING) assert pm.check_status(StateStatus.SUCCESS) assert not pm._run_thread.is_alive()
def test_interruption_in_machines_with_sequential_state(capsys): ws1 = WaitState("ws1", 0.2) ws2 = WaitState("ws2", 0.2) ps1 = PrintState("ps1", "Print1") es = IdleState("es") iss = IdleState("iss") sm = SequentialState("sm", children=[ws1, ws2, ps1]) sm.add_transition_on_success(es) sm.add_transition(lambda s, b: s._curr_child.checkName('ws2'), iss) exe = Machine("exe", sm, ["es", "iss"], rate=100) exe.run() assert exe._exception_raised_state_name == "" assert exe._internal_exception is None assert exe._status == StateStatus.SUCCESS assert not exe._run_thread.is_alive() assert exe._curr_state._name == 'iss' assert exe.is_end() assert capsys.readouterr().out == "" assert not sm._run_thread.is_alive() assert not ws1._run_thread.is_alive() assert not ws2._run_thread.is_alive() assert sm._status == StateStatus.INTERRUPTED assert ws2._status == StateStatus.INTERRUPTED assert ws1._status == StateStatus.SUCCESS assert ps1._status == StateStatus.UNKNOWN assert ws2.checkStatus(StateStatus.INTERRUPTED) assert ws1.checkStatus(StateStatus.SUCCESS)
def test_object_get_in_transition(capsys): class SetState(State): def execute(self, board: Board): obj = {'hello': [1, 2, 3], 'name': {'first': 'test'}} board.set('obj', obj) obj = {} return StateStatus.SUCCESS s = SetState('s') w = WaitState('w', 1) i = IdleState('i') end = IdleState('end') s.add_transition_on_success(w) w.add_transition_on_success(i) i.add_transition( lambda state, board: board.get('obj')['name']['first'] == 'test', end) exe = Machine('xe', s, end_state_ids=['end']) exe.run() assert exe.is_end() # Idle state returns RUNNING instead of SUCCESS assert exe._curr_state._status == StateStatus.RUNNING
def test_parallel_state_performance(capsys): wait_state_list = [] for i in range(0, 1000): wait_state_list.append(WaitState(f"wait{i}", 1)) pm = ParallelState('pm', wait_state_list) exe = Machine("xe", pm, rate=10) start_time = time.time() exe.start(None) pm.wait() exe.interrupt() duration = time.time() - start_time assert duration < 2 # as long as its not too slow, we are fine. assert not pm._run_thread.is_alive()
def test_atleastone_state(capsys): ps1 = PrintState('p1', "ps1") ws1 = WaitState("w1", 0.5) ps2 = PrintState('p2', "ps2") one = AtLeastOneState("one", children=[ ps2, SequentialState("seq", children=[ ws1, ps1 ]) ]) es = IdleState("endState") one.add_transition_on_success(es) exe = Machine("xe", one, end_state_ids=["endState"], rate=10) exe.run() assert capsys.readouterr().out == "ps2\n"
def test_interrupt_in_parallel_state(capsys): ws = WaitState("ws1", 1) ws2 = WaitState("ws2", 2) es = IdleState("es") pm = ParallelState('pm', [ws, ws2]) pm.add_transition_on_success(es) pm.add_transition(lambda x, y: True, es) exe = Machine("main_machine", pm, end_state_ids=['es'], rate=10) # run machine exe.start(None) # because of the always transition, it should happen in about 10 assert exe.wait(0.2) assert ws.checkStatus(StateStatus.INTERRUPTED) assert ws2.checkStatus(StateStatus.INTERRUPTED) assert not pm._run_thread.is_alive()
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.check_status(StateStatus.EXCEPTION) assert str(exe._internal_exception) == error_text assert exe._exception_raised_state_name == "xe.pm.re" assert not pm._run_thread.is_alive()
def test_sequential_state_flow(capsys): flow_in_text = "test_sequential_state_flow" first_time = True class PreState(State): def execute(self, board: Board) -> StateStatus: self.flow_out = flow_in_text class ReceiveState(State): def execute(self, board): nonlocal first_time if first_time: assert self.flow_in == flow_in_text first_time = False print("one") return StateStatus.SUCCESS else: assert self.flow_in == None print("two") return StateStatus.FAILED ps = PreState("pre") ws = WaitState("ws1", 0.1) rs = ReceiveState("rs") es = IdleState("es") seqs = SequentialState('seqs', [rs, ws]) ps.add_transition_on_complete(seqs) seqs.add_transition_on_success(seqs) seqs.add_transition_on_failed(es) me = Machine("me", ps, end_state_ids=['es']) me.start(None) me.wait() assert capsys.readouterr().out == "one\ntwo\n"
def test_interruption_in_sequential_state(capsys): ws1 = WaitState("ws1", 0.1) ws2 = WaitState("ws2", 0.1) ps1 = PrintState("ps1", "Print1") sm = SequentialState("sm", children=[ws1, ws2, ps1]) sm.start(None) sm.wait(0.15) sm.interrupt() assert capsys.readouterr().out == "" assert sm.checkStatus(StateStatus.INTERRUPTED) assert ws2.checkStatus(StateStatus.INTERRUPTED) assert ws1.checkStatus(StateStatus.SUCCESS) assert ps1.checkStatus(StateStatus.UNKNOWN) assert not sm._run_thread.is_alive() assert not ws1._run_thread.is_alive() assert not ws2._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.check_status(StateStatus.SUCCESS) assert ws2.check_status(StateStatus.RUNNING) assert exe.check_status(StateStatus.RUNNING) # at this point ws should be done but ws2 is still going # wait another one seconds assert exe.wait(2) assert exe.check_status(StateStatus.SUCCESS) assert not pm._run_thread.is_alive()
def test_sequential_state_interrupt_before_start(): seq = SequentialState("seq", children=[ WaitState("w1", 1), WaitState("w2", 2) ]) seq.interrupt()