Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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()
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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_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")
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
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