def test_repeat_node_in_machine(): counter = 0 class CounterState(State): def execute(self, board: Board) -> StateStatus: nonlocal counter counter += 1 time.sleep(0.1) return StateStatus.SUCCESS ds1 = CounterState("ds1") ds2 = CounterState("ds2") ds3 = CounterState("ds3") ds4 = CounterState("ds4") ds5 = CounterState("ds5") ds1.add_transition_on_success(ds2) ds2.add_transition_on_success(ds3) ds3.add_transition_on_success(ds4) ds4.add_transition_on_success(ds5) ds5.add_transition_on_success(ds1) exe = Machine('exe', ds1, rate=5) exe.start(None) for i in range(1, 6): time.sleep(1) assert counter == i * 5 assert exe._curr_state == ds5 exe.interrupt() assert counter == (5 * 5)
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_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_repeat_node_in_machine_fast(): counter = 0 class CounterState(State): def execute(self, board: Board) -> StateStatus: nonlocal counter counter += 1 return StateStatus.SUCCESS ds1 = CounterState("ds1") ds2 = CounterState("ds2") ds3 = CounterState("ds3") ds1.add_transition_on_success(ds2) ds2.add_transition_on_success(ds3) ds3.add_transition_on_success(ds1) exe = Machine('exe', ds1, rate=60) exe.start(None) time.sleep(2) exe.interrupt() # the performance of the computer might change this. assert counter >= (60 * 2) - 2 assert counter <= (60 * 2) + 1
def test_validate_transition_immediate(): counter = 0 class CounterState(State): def execute(self, board: Board) -> StateStatus: nonlocal counter counter += 1 return StateStatus.SUCCESS ds1 = CounterState("ds1") ds2 = CounterState("ds2") ds3 = CounterState("ds3") ds1.add_transition(lambda s, b: True, ds2) ds2.add_transition(lambda s, b: True, ds3) ds3.add_transition(lambda s, b: True, ds1) exe = Machine('exe', ds1, rate=60) exe.start(None) time.sleep(2) exe.interrupt() # the performance of the computer might change this. assert counter >= (60 * 2) - 2 assert counter <= (60 * 2) + 1