Beispiel #1
0
def test_lock_state_machine(caplog):

    state_machine = StateMachine()

    @lock_state_machine
    def custom_function(object, number):
        raise AttributeError("Test error")

    State.custom_method = custom_function

    state1 = ExecutionState("s1")
    state_machine.root_state = state1

    try:
        state1.custom_method(5)
    except Exception as e:
        import traceback
        print("Could not stop state machine: {0} {1}".format(
            e, traceback.format_exc()))

    assert global_lock_counter == 0

    state1.add_outcome("outcome1", 3)
    assert len(state1.outcomes) == 4

    assert_logger_warnings_and_errors(caplog)
Beispiel #2
0
def test_scoped_data(caplog):
    storage_path = testing_utils.get_unique_temp_path()

    sm = create_state_machine()

    storage.save_state_machine_to_path(sm, storage_path)
    sm_loaded = storage.load_state_machine_from_path(storage_path)

    state_machine = StateMachine(sm_loaded.root_state)

    testing_utils.test_multithreading_lock.acquire()
    rafcon.core.singleton.state_machine_manager.add_state_machine(
        state_machine)
    rafcon.core.singleton.state_machine_manager.active_state_machine_id = state_machine.state_machine_id
    rafcon.core.singleton.state_machine_execution_engine.start()
    rafcon.core.singleton.state_machine_execution_engine.join()
    rafcon.core.singleton.state_machine_manager.remove_state_machine(
        state_machine.state_machine_id)

    try:
        assert state_machine.root_state.output_data[
            "data_output_port1"] == 42.0
        testing_utils.assert_logger_warnings_and_errors(caplog)
    finally:
        testing_utils.test_multithreading_lock.release()
Beispiel #3
0
def test_references(caplog):
    gvm = GlobalVariableManager()
    d = {'a': 1, 'b': 2}

    # Test access by reference
    gvm.set_variable('d', d, per_reference=True)
    _d = gvm.get_variable('d', per_reference=True)
    d['a'] = 3
    assert d['a'] == _d['a'] == 3
    __d = gvm.get_variable('d', per_reference=True)
    assert d['a'] == __d['a'] == 3
    ___d = gvm.get_variable('d')
    d['a'] = 4
    assert d['a'] == __d['a'] == ___d['a'] == 4

    # Test set by reference, get by copy
    gvm.set_variable('x', d, per_reference=True)
    cd = gvm.get_variable('x', per_reference=False)
    d['a'] = 5
    assert d['a'] == 5 and cd['a'] == 4

    # Test access by copy
    dc = gvm.get_variable('d', per_reference=False)
    d['b'] = 5
    assert d['a'] == dc['a']
    assert d['b'] != dc['b']

    gvm.set_variable('c', d)
    cc = gvm.get_variable('c')
    d['a'] = 10
    assert d['a'] != cc['a']

    with raises(RuntimeError):
        gvm.get_variable('c', per_reference=True)
    testing_utils.assert_logger_warnings_and_errors(caplog)
def test_start_stop_pause_step(caplog):
    sm = return_loop_state_machine()
    rafcon.core.singleton.global_variable_manager.set_variable("counter", 0)

    testing_utils.test_multithreading_lock.acquire()
    rafcon.core.singleton.state_machine_manager.add_state_machine(sm)
    rafcon.core.singleton.state_machine_manager.active_state_machine_id = sm.state_machine_id
    rafcon.core.singleton.state_machine_execution_engine.step_mode()

    for i in range(5):
        time.sleep(0.2)
        rafcon.core.singleton.state_machine_execution_engine.step_into()

    # give the state machine time to execute
    time.sleep(0.2)
    rafcon.core.singleton.state_machine_execution_engine.stop()
    rafcon.core.singleton.state_machine_execution_engine.join()

    try:
        assert rafcon.core.singleton.global_variable_manager.get_variable(
            "counter") == 5
        rafcon.core.singleton.state_machine_manager.remove_state_machine(
            sm.state_machine_id)
        testing_utils.assert_logger_warnings_and_errors(caplog)
    finally:
        testing_utils.test_multithreading_lock.release()
Beispiel #5
0
def test_invalid_locale_setting(caplog, monkeypatch):
    create_mo_files()

    with use_locale("invalid", monkeypatch):
        i18n.setup_l10n()

    testing_utils.assert_logger_warnings_and_errors(caplog=caplog, expected_warnings=1)
Beispiel #6
0
def test_hierarchy_save_load_test(caplog):
    storage_path = testing_utils.get_unique_temp_path()

    hierarchy_state = create_hierarchy_state()
    sm = StateMachine(hierarchy_state)

    storage.save_state_machine_to_path(sm, storage_path)
    sm_loaded = storage.load_state_machine_from_path(storage_path)

    state_machine = StateMachine(sm_loaded.root_state)

    testing_utils.test_multithreading_lock.acquire()
    rafcon.core.singleton.state_machine_manager.add_state_machine(
        state_machine)
    rafcon.core.singleton.state_machine_manager.active_state_machine_id = state_machine.state_machine_id
    rafcon.core.singleton.state_machine_execution_engine.start()
    rafcon.core.singleton.state_machine_execution_engine.join()
    rafcon.core.singleton.state_machine_manager.remove_state_machine(
        state_machine.state_machine_id)
    try:
        assert state_machine.root_state.output_data["output1"] == 52.0
        # 2 type error -> one child output port data type error and root state scoped data type error
        testing_utils.assert_logger_warnings_and_errors(caplog,
                                                        expected_errors=2)
    finally:
        testing_utils.test_multithreading_lock.release()
Beispiel #7
0
def test_connections_from_object_type(caplog):
    parent_state = HierarchyState("parent")
    child_state = ExecutionState("child")
    parent_state.add_state(child_state)

    parent_obj_port_id = parent_state.add_input_data_port("obj",
                                                          data_type=object,
                                                          default_value=None)
    parent_int_port_id = parent_state.add_input_data_port("int",
                                                          data_type=int,
                                                          default_value=None)
    child_obj_port_id = child_state.add_input_data_port("obj",
                                                        data_type=object,
                                                        default_value=0)
    child_int_port_id = child_state.add_input_data_port("int",
                                                        data_type=int,
                                                        default_value=0)

    # Connection from specific type int to generic type object
    parent_state.add_data_flow(parent_state.state_id, parent_int_port_id,
                               child_state.state_id, child_obj_port_id)

    # Connection from generic type object to specific type int
    parent_state.add_data_flow(parent_state.state_id, parent_obj_port_id,
                               child_state.state_id, child_int_port_id)

    testing_utils.assert_logger_warnings_and_errors(caplog,
                                                    expected_warnings=0,
                                                    expected_errors=0)
 def test_default_run(self, caplog):
     gvm.set_variable("wait_inner_observer_1", 1)
     gvm.set_variable("wait_inner_observer_2", 1)
     gvm.set_variable("wait_observer_1", 1)
     gvm.set_variable("wait_observer_2", 1)
     self.run_state_machine()
     self.assert_no_errors()
     testing_utils.assert_logger_warnings_and_errors(caplog, 0)
Beispiel #9
0
def test_save_nested_library_state(caplog):
    library_with_nested_library_sm = create_hierarchy_state_library_state_machine(
    )

    storage.save_state_machine_to_path(library_with_nested_library_sm,
                                       join(TEST_LIBRARY_PATH,
                                            "library_with_nested_library"),
                                       delete_old_state_machine=True)
    testing_utils.assert_logger_warnings_and_errors(caplog)
 def test_observer_1_exception(self, caplog):
     gvm.set_variable("wait_observer_1", 0.1)
     gvm.set_variable("observer_1_exception", True)
     self.run_state_machine()
     self.assert_no_errors()
     assert_all_false(gvm.get_variable("inner_observer_1_finish"),
                      gvm.get_variable("observer_1_finish"),
                      gvm.get_variable("observer_1_finish"),
                      gvm.get_variable("observer_2_finish"))
     testing_utils.assert_logger_warnings_and_errors(caplog, 0, 1)
Beispiel #11
0
def test_runtime_values(caplog):
    state_machine_manager.delete_all_state_machines()

    sm = state_machine_execution_engine.execute_state_machine_from_path(
        path=testing_utils.get_test_sm_path(
            os.path.join("unit_test_state_machines",
                         "library_runtime_value_test")))
    state_machine_manager.remove_state_machine(sm.state_machine_id)
    assert sm.root_state.output_data["data_output_port1"] == 114
    testing_utils.assert_logger_warnings_and_errors(caplog)
def test_slim_observer(caplog):
    testing_utils.dummy_gui(None)
    test_observer = ObserverTest()
    test_observer.test_observable.first_var = 20.0
    assert test_observer.test_value == 20

    test_observer.test_observable.complex_method(1, 3, "Hello world")
    assert test_observer.test_observable.observable_test_var == 4
    assert test_observer.test_value2 == 4
    assert test_observer.test_value3 == 30

    testing_utils.assert_logger_warnings_and_errors(caplog)
 def test_inner_observer_1_finish(self, caplog):
     gvm.set_variable("wait_inner_observer_1", 0.1)
     self.run_state_machine()
     self.assert_no_errors()
     assert_gvm("inner_observer_1_finish")
     assert_gvm("inner_exit_handler")
     assert_gvm("exit_handler")
     assert_gvm("exit_handler_2", False)
     assert_all_false(gvm.get_variable("inner_observer_2_finish"),
                      gvm.get_variable("observer_1_finish"),
                      gvm.get_variable("observer_2_finish"))
     testing_utils.assert_logger_warnings_and_errors(caplog)
Beispiel #14
0
def test_save_libraries(caplog):
    s = storage

    state1 = ExecutionState("library_execution_state1",
                            path=testing_utils.TEST_SCRIPT_PATH,
                            filename="library_execution_state1.py")
    input_state1 = state1.add_input_data_port("data_input_port1", "float")
    output_state1 = state1.add_output_data_port("data_output_port1", "float")

    state2 = ExecutionState("library_execution_state2",
                            path=testing_utils.TEST_SCRIPT_PATH,
                            filename="library_execution_state2.py")
    input_state2 = state2.add_input_data_port("data_input_port1", "float")
    output_state2 = state2.add_output_data_port("data_output_port1", "float")

    state3 = HierarchyState("library_hierarchy_state1")
    state3.add_state(state1)
    state3.add_state(state2)
    state3.set_start_state(state1.state_id)

    state3.add_transition(state1.state_id, 0, state2.state_id, None)
    state3.add_transition(state2.state_id, 0, state3.state_id, 0)
    input_state3 = state3.add_input_data_port("data_input_port1", "float", 1.0)
    output_state3 = state3.add_output_data_port("data_output_port1", "float",
                                                2.0)
    state3.add_data_flow(state3.state_id, input_state3, state1.state_id,
                         input_state1)
    state3.add_data_flow(state1.state_id, output_state1, state2.state_id,
                         input_state2)
    state3.add_data_flow(state2.state_id, output_state2, state3.state_id,
                         output_state3)

    # save hierarchy state as state machine
    s.save_state_machine_to_path(StateMachine(state3),
                                 join(TEST_LIBRARY_PATH, "hierarchy_library"))

    # save execution state as state machine
    s.save_state_machine_to_path(StateMachine(state1),
                                 join(TEST_LIBRARY_PATH, "execution_library"))

    # save hierarchy state as nested state machines
    state3.name = "library_nested1"
    s.save_state_machine_to_path(StateMachine(state3),
                                 join(TEST_LIBRARY_PATH, "library_container",
                                      "library_nested1"),
                                 delete_old_state_machine=True)
    state3.name = "library_nested2"
    s.save_state_machine_to_path(StateMachine(state3),
                                 join(TEST_LIBRARY_PATH, "library_container",
                                      "library_nested2"),
                                 delete_old_state_machine=True)
    testing_utils.assert_logger_warnings_and_errors(caplog)
Beispiel #15
0
def test_locks(caplog):
    gvm = GlobalVariableManager()
    gvm.set_variable('a', 1)
    a = gvm.get_variable('a')
    assert a == 1

    access_key = gvm.lock_variable('a')
    gvm.lock_variable('a')
    a = gvm.get_variable('a', access_key=access_key)
    assert a == 1
    gvm.set_variable('a', 2, access_key=access_key)
    assert gvm.get_variable('a', access_key=access_key) == 2
    gvm.unlock_variable('a', access_key)
    gvm.unlock_variable('a', access_key)
    testing_utils.assert_logger_warnings_and_errors(caplog, expected_errors=2)
Beispiel #16
0
def test_concurrency_barrier_state_execution(caplog):

    testing_utils.test_multithreading_lock.acquire()
    sm = create_state_machine()
    root_state = sm.root_state
    state_machine = StateMachine(root_state)
    rafcon.core.singleton.state_machine_manager.add_state_machine(state_machine)
    rafcon.core.singleton.state_machine_manager.active_state_machine_id = state_machine.state_machine_id
    rafcon.core.singleton.state_machine_execution_engine.start()
    rafcon.core.singleton.state_machine_execution_engine.join()
    rafcon.core.singleton.state_machine_manager.remove_state_machine(state_machine.state_machine_id)
    try:
        assert root_state.output_data["output_data_port1"] == 42
        testing_utils.assert_logger_warnings_and_errors(caplog)
    finally:
        testing_utils.test_multithreading_lock.release()
Beispiel #17
0
def test_preemptive_wait2_timeout(caplog):
    testing_utils.test_multithreading_lock.acquire()

    gvm.set_variable('state_2_wait', 0.5)
    gvm.set_variable('state_1_wait', 1.)

    run_state_machine()

    try:
        assert 0.5 < gvm.get_variable('state_2_wait_time')
        assert gvm.get_variable('state_1_preempted')
        assert not gvm.get_variable('state_2_preempted')

        testing_utils.assert_logger_warnings_and_errors(caplog)
    finally:
        testing_utils.test_multithreading_lock.release()
Beispiel #18
0
def test_preemptive_wait_daemon(caplog):
    testing_utils.test_multithreading_lock.acquire()

    gvm.set_variable('state_1_wait', 0.5)
    gvm.set_variable('state_2_wait', None)

    run_state_machine()

    try:
        assert 0.5 < gvm.get_variable('state_1_wait_time')
        # cannot assert this as state 2 may be launched later and will thus have a shorter execution time
        # assert 0.5 < gvm.get_variable('state_2_wait_time')
        assert not gvm.get_variable('state_1_preempted')
        assert gvm.get_variable('state_2_preempted')

        testing_utils.assert_logger_warnings_and_errors(caplog)
    finally:
        testing_utils.test_multithreading_lock.release()
Beispiel #19
0
def test_port_and_outcome_removal(caplog):
    container = ContainerState("Container")
    input_container_state = container.add_input_data_port("input", "float")
    output_container_state = container.add_output_data_port("output", "float")
    scoped_variable_container_state = container.add_scoped_variable(
        "scope", "float")

    assert len(container.transitions) == 0
    assert len(container.data_flows) == 0
    assert len(container.outcomes) == 3
    assert len(container.input_data_ports) == 1
    assert len(container.output_data_ports) == 1
    assert len(container.scoped_variables) == 1

    state1 = ExecutionState("test_execution_state")
    input_state1 = state1.add_input_data_port("input", "float")
    output_state1 = state1.add_output_data_port("output", "float")

    container.add_state(state1)
    container.add_transition(state1.state_id, 0, container.state_id, -2)
    container.add_data_flow(container.state_id, input_container_state,
                            state1.state_id, input_state1)
    container.add_data_flow(state1.state_id, output_state1, container.state_id,
                            output_container_state)
    container.add_data_flow(container.state_id, input_container_state,
                            container.state_id,
                            scoped_variable_container_state)

    assert len(container.transitions) == 1
    assert len(container.data_flows) == 3

    state1.remove_outcome(0)
    assert len(container.transitions) == 0

    state1.remove_output_data_port(output_state1)
    assert len(container.data_flows) == 2

    state1.remove_input_data_port(input_state1)
    assert len(container.data_flows) == 1

    container.remove_scoped_variable(scoped_variable_container_state)
    assert len(container.data_flows) == 0

    assert_logger_warnings_and_errors(caplog)
Beispiel #20
0
def test_hierarchy_state_library(caplog):
    testing_utils.test_multithreading_lock.acquire()
    library_container_state_sm = create_hierarchy_state_library_state_machine()

    rafcon.core.singleton.state_machine_manager.add_state_machine(
        library_container_state_sm)
    rafcon.core.singleton.state_machine_execution_engine.start(
        library_container_state_sm.state_machine_id)
    rafcon.core.singleton.state_machine_execution_engine.join()

    # print output_data["data_output_port1"]
    try:
        assert library_container_state_sm.root_state.output_data[
            "data_output_port1"] == 42.0
        rafcon.core.singleton.state_machine_manager.remove_state_machine(
            library_container_state_sm.state_machine_id)
        testing_utils.assert_logger_warnings_and_errors(caplog)
    finally:
        testing_utils.test_multithreading_lock.release()
def test_concurrency_preemption_state_execution(caplog):

    preemption_state_sm = create_preemption_state_machine()

    testing_utils.test_multithreading_lock.acquire()
    rafcon.core.singleton.state_machine_manager.add_state_machine(
        preemption_state_sm)
    rafcon.core.singleton.state_machine_execution_engine.start(
        preemption_state_sm.state_machine_id)
    rafcon.core.singleton.state_machine_execution_engine.join()

    try:
        assert rafcon.core.singleton.global_variable_manager.get_variable(
            "preempted_state2_code") == "DF3LFXD34G"
        assert preemption_state_sm.root_state.final_outcome.outcome_id == 3
        rafcon.core.singleton.state_machine_manager.remove_state_machine(
            preemption_state_sm.state_machine_id)
        testing_utils.assert_logger_warnings_and_errors(caplog)
    finally:
        testing_utils.test_multithreading_lock.release()
def test_hierarchy_state_execution(caplog):
    hierarchy_state = create_hierarchy_state()

    state_machine = StateMachine(hierarchy_state)

    try:
        # Changing the data type has to fail, as the data port is already connected to a data flow
        state_machine.root_state.input_data_ports[42].data_type = str
    except Exception as e:
        assert isinstance(e, ValueError)

    testing_utils.test_multithreading_lock.acquire()
    rafcon.core.singleton.state_machine_manager.add_state_machine(state_machine)
    rafcon.core.singleton.state_machine_execution_engine.start(state_machine.state_machine_id)
    rafcon.core.singleton.state_machine_execution_engine.join()
    rafcon.core.singleton.state_machine_manager.remove_state_machine(state_machine.state_machine_id)
    try:
        assert hierarchy_state.output_data["output1"] == 52.0
        # 2 type error -> one child output port data type error and root state scoped data type error
        testing_utils.assert_logger_warnings_and_errors(caplog, expected_errors=2)
    finally:
        testing_utils.test_multithreading_lock.release()
Beispiel #23
0
def test_nested_library_state_machine(caplog):
    testing_utils.test_multithreading_lock.acquire()
    # TODO: the library_manager is initialized a second time here
    rafcon.core.singleton.library_manager.initialize()
    nested_library_state = LibraryState("temporary_libraries",
                                        "library_with_nested_library", "0.1",
                                        "nested_library_state_name",
                                        "nested_library_state_id")
    state_machine = StateMachine(nested_library_state)

    rafcon.core.singleton.state_machine_manager.add_state_machine(
        state_machine)
    rafcon.core.singleton.state_machine_execution_engine.start(
        state_machine.state_machine_id)
    rafcon.core.singleton.state_machine_execution_engine.join()

    # print output_data["data_output_port1"]
    try:
        assert nested_library_state.output_data["data_output_port1"] == 42.0
        rafcon.core.singleton.state_machine_manager.remove_state_machine(
            state_machine.state_machine_id)
        testing_utils.assert_logger_warnings_and_errors(caplog)
    finally:
        testing_utils.test_multithreading_lock.release()
Beispiel #24
0
def test_transition_creation(caplog):

    storage_path = testing_utils.get_unique_temp_path()

    sm = create_state_machine()

    storage.save_state_machine_to_path(sm, storage_path)
    sm_loaded = storage.load_state_machine_from_path(storage_path)

    root_state = sm_loaded.root_state

    state_machine = StateMachine(root_state)
    testing_utils.test_multithreading_lock.acquire()
    rafcon.core.singleton.state_machine_manager.add_state_machine(
        state_machine)
    rafcon.core.singleton.state_machine_manager.active_state_machine_id = state_machine.state_machine_id
    rafcon.core.singleton.state_machine_execution_engine.start()
    rafcon.core.singleton.state_machine_execution_engine.join()
    try:
        testing_utils.assert_logger_warnings_and_errors(caplog)
    finally:
        testing_utils.test_multithreading_lock.release()

    rafcon.core.singleton.state_machine_manager.delete_all_state_machines()
def test_concurrency_preemption_save_load(caplog):
    testing_utils.test_multithreading_lock.acquire()

    storage_path = testing_utils.get_unique_temp_path()

    preemption_state_sm = create_preemption_state_machine()

    storage.save_state_machine_to_path(preemption_state_sm, storage_path)
    storage.load_state_machine_from_path(storage_path)

    rafcon.core.singleton.state_machine_manager.add_state_machine(
        preemption_state_sm)
    rafcon.core.singleton.state_machine_execution_engine.start(
        preemption_state_sm.state_machine_id)
    rafcon.core.singleton.state_machine_execution_engine.join()

    try:
        assert rafcon.core.singleton.global_variable_manager.get_variable(
            "preempted_state2_code") == "DF3LFXD34G"
        rafcon.core.singleton.state_machine_manager.remove_state_machine(
            preemption_state_sm.state_machine_id)
        testing_utils.assert_logger_warnings_and_errors(caplog)
    finally:
        testing_utils.test_multithreading_lock.release()
Beispiel #26
0
def test_create_container_state(caplog):

    container = ContainerState("Container")
    assert len(container.states) == 0

    input_port_container_state = container.add_input_data_port(
        "input", "float")
    output_port_container_state = container.add_output_data_port(
        "output", "float")
    scoped_var_1_container_state = container.add_scoped_variable(
        "scope_1", "float")
    scoped_var_2_container_state = container.add_scoped_variable(
        "scope_2", "float")

    state1 = ExecutionState("MyFirstState")
    container.add_state(state1)
    assert len(container.states) == 1

    # As the ID of two states is identical, the add method should adapt the state_id and return the new state_id
    new_state_id = container.add_state(
        ExecutionState("test_execution_state", state_id=state1.state_id))
    assert len(container.states) == 2
    assert not new_state_id == state1.state_id
    container.remove_state(new_state_id)

    state2 = ExecutionState("2nd State", state_id=container.state_id)
    logger.debug("Old state id: {0}".format(str(state2.state_id)))
    new_state_id = container.add_state(state2)
    logger.debug("New state id: {0}".format(str(new_state_id)))

    assert len(container.states) == 2

    input_state1 = state1.add_input_data_port("input", "float")
    output_state1 = state1.add_output_data_port("output", "float")
    input_state2 = state2.add_input_data_port("input", "float")
    input2_state2 = state2.add_input_data_port("input2", "float")
    output_state2 = state2.add_output_data_port("output", "float")

    assert len(container.data_flows) == 0
    container.add_data_flow(state1.state_id, output_state1, state2.state_id,
                            input_state2)
    assert len(container.data_flows) == 1

    with raises(ValueError):
        # Data flow to connected input port
        container.add_data_flow(state1.state_id, input_state1, state2.state_id,
                                input_state2)
    with raises(ValueError):
        # Data flow to non-existing port
        wrong_data_port_id = 218347
        container.add_data_flow(state1.state_id, output_state1,
                                state2.state_id, wrong_data_port_id)
    with raises(ValueError):
        # Data flow from non-existing port
        wrong_data_port_id = 239847
        container.add_data_flow(state1.state_id, wrong_data_port_id,
                                state2.state_id, input_state2)
    with raises(ValueError):
        # Data flow from non-existing state
        container.add_data_flow(-1, output_state1, state2.state_id,
                                input_state2)
    with raises(ValueError):
        # Data flow to non-existing state
        container.add_data_flow(state1.state_id, output_state1, -1,
                                input_state2)
    with raises(ValueError):
        # Connect port to itself
        container.add_data_flow(state1.state_id, output_state1,
                                state1.state_id, output_state1)
    with raises(ValueError):
        # Connect two scoped variable
        container.add_data_flow(container.state_id,
                                scoped_var_1_container_state,
                                container.state_id,
                                scoped_var_2_container_state)

    container.add_data_flow(container.state_id, input_port_container_state,
                            state1.state_id, input_state1)

    # with raises(ValueError):  # cannot connect data flow to same child state
    container.add_data_flow(state2.state_id, output_state2, state2.state_id,
                            input2_state2)

    assert len(container.data_flows) == 3

    assert len(container.transitions) == 0

    container.add_transition(state1.state_id, -1, state2.state_id, None)
    assert len(container.transitions) == 1
    container.add_transition(state1.state_id, -2, container.state_id, -2)
    assert len(container.transitions) == 2
    t3 = container.add_transition(state2.state_id, -1, container.state_id, -1)
    assert len(container.transitions) == 3

    with raises(ValueError):
        # Transition from connected outcome
        container.add_transition(state1.state_id, -1, state2.state_id, None)
    with raises(ValueError):
        # Non-existing from state id
        container.add_transition(-1, -1, state2.state_id, None)
    with raises(ValueError):
        # Non-existing from outcome
        container.add_transition(state1.state_id, -3, state2.state_id, None)
    with raises(ValueError):
        # Non-existing to state id
        container.add_transition(state1.state_id, -1, -1, None)
    with raises(ValueError):
        # Non-existing to outcome
        container.add_transition(state1.state_id, -1, container.state_id, -3)
    with raises(ValueError):
        # Transition pointing to the state itself
        container.add_transition(state1.state_id, -2, state1.state_id, None)
    with raises(ValueError):
        # to_state_id and to_outcome not None
        container.add_transition(state1.state_id, -2, state1.state_id, -1)
    with raises(ValueError):
        # Transition from connected outcome
        container.add_transition(state2.state_id, -1, state2.state_id, -2)
    with raises(ValueError):
        # Transition going from one outcome to another outcome of the same state
        container.add_transition(state2.state_id, -1, state2.state_id, -2)

    with raises(AttributeError):
        # The removal of an undefined transition should throw an AttributeError
        container.remove_transition(-1)

    container.remove_transition(t3)
    assert len(container.transitions) == 2
    with raises(AttributeError):
        # The removal of an undefined transition should throw an AttributeError
        container.remove_transition(t3)
    container.add_transition(state2.state_id, -1, container.state_id, -1)
    assert len(container.transitions) == 3

    container.remove_state(state1.state_id)
    assert len(container.states) == 1
    assert len(container.transitions) == 1
    assert len(container.data_flows) == 1

    # barrier state remove test and bug test elements for issue #346
    barrier_state_id = container.add_state(BarrierConcurrencyState())
    container.remove(container.states[barrier_state_id])

    barrier_state_id = container.add_state(BarrierConcurrencyState())
    with raises(AttributeError):
        container.states[barrier_state_id].remove(
            container.states[barrier_state_id].states.values()[0])
    container.remove_state(barrier_state_id)
    ###########################################

    assert_logger_warnings_and_errors(caplog)
Beispiel #27
0
def test_basic_string_translation(caplog, monkeypatch):
    with use_locale("de_DE.UTF-8", monkeypatch):
        i18n.setup_l10n()
        assert _("Remove") == "Entfernen"

    testing_utils.assert_logger_warnings_and_errors(caplog=caplog)
Beispiel #28
0
def test_lock_state_machine(caplog):

    state_machine = StateMachine()

    @lock_state_machine
    def custom_function(object, number):
        raise AttributeError("Test error")

    State.custom_method = custom_function

    state1 = ExecutionState("s1")
    state_machine.root_state = state1

    try:
        state1.custom_method(5)
    except Exception, e:
        import traceback
        print "Could not stop state machine: {0} {1}".format(
            e.message, traceback.format_exc())

    assert global_lock_counter == 0

    state1.add_outcome("outcome1", 3)
    assert len(state1.outcomes) == 4

    assert_logger_warnings_and_errors(caplog)


if __name__ == '__main__':
    test_lock_state_machine(None)
Beispiel #29
0
def test_create_state(caplog):
    state1 = ExecutionState("MyFirstState")

    assert len(state1.outcomes) == 3

    out = state1.add_outcome("MyFirstOutcome", 3)

    assert len(state1.outcomes) == 4

    state1.remove_outcome(out)

    with raises(AttributeError):
        # AttributeError should be raised if not existing outcome ID is to be removed
        state1.remove_outcome(out)
    with raises(AttributeError):
        # AttributeError should be raised if outcome preempted is to be removed
        state1.remove_outcome(-1)
    with raises(AttributeError):
        # AttributeError should be raised if outcome aborted is to be removed
        state1.remove_outcome(-2)

    assert len(state1.outcomes) == 3

    assert len(state1.input_data_ports) == 0
    assert len(state1.output_data_ports) == 0

    input_port_id = state1.add_input_data_port("input", "str")
    output_port_id = state1.add_output_data_port("output", "float")

    assert len(state1.input_data_ports) == 1
    assert len(state1.output_data_ports) == 1

    state1.remove_input_data_port(input_port_id)
    state1.remove_output_data_port(output_port_id)

    assert len(state1.input_data_ports) == 0
    assert len(state1.output_data_ports) == 0

    with raises(AttributeError):
        # AttributeError should be raised if not existing input is to be removed
        state1.remove_input_data_port(input_port_id)

    with raises(AttributeError):
        # AttributeError should be raised if not existing output is to be removed
        state1.remove_output_data_port(output_port_id)

    state2 = ExecutionState(name="State2", state_id=state1.state_id)

    # This should work, as data_type and default_value are optional parameters
    port = InputDataPort('input', data_port_id=99)

    with raises(AttributeError):
        # The name of the port differs in key and class member
        ExecutionState("test_execution_state",
                       input_data_ports={'diff_input': port})

    # UTF8 strings should be allowed at least for descriptions
    state1.description = u'My English is not v\xc3ry good'

    assert_logger_warnings_and_errors(caplog)

    a = ExecutionState("test", state_id=10)
    b = ExecutionState("test", state_id=10)

    assert a == b
    assert a is not b
Beispiel #30
0
def test_type_check(caplog):
    # valid
    gvm = GlobalVariableManager()
    gvm.set_variable("a", 1, data_type=int)
    a = gvm.get_variable("a")
    assert a == 1

    gvm.set_variable("b", "test", data_type=str)
    b = gvm.get_variable("b")
    assert b == "test"

    gvm.set_variable("c", 12.0, data_type=float)
    c = gvm.get_variable("c")
    assert c == 12.0

    gvm.set_variable("d", True, data_type=bool)
    d = gvm.get_variable("d")
    assert d

    e_list = [1, 2, 3]
    gvm.set_variable("e", e_list, data_type=list)
    e = gvm.get_variable("e")
    assert e == e_list

    f_dict = {'a': 1, 'b': 2}
    gvm.set_variable("f", f_dict, data_type=dict)
    f = gvm.get_variable("f")
    assert f == f_dict

    # invalid
    with raises(TypeError):
        gvm.set_variable("g", "test", data_type=int)
    testing_utils.assert_logger_warnings_and_errors(caplog)

    with raises(TypeError):
        gvm.set_variable("g", "test", data_type=float)
    testing_utils.assert_logger_warnings_and_errors(caplog)

    # overwriting
    gvm.set_variable("a", 3, data_type=int)
    a = gvm.get_variable("a")
    assert a == 3

    # invalid overwriting
    with raises(TypeError):
        gvm.set_variable("a", "string", data_type=int)
    a = gvm.get_variable("a")
    assert a == 3

    # invalid overwriting
    with raises(TypeError):
        gvm.set_variable("a", "any_string")
    a = gvm.get_variable("a")
    assert a == 3

    # backward compatibility
    gvm.delete_variable("a")
    gvm.set_variable("a", "test")
    a = gvm.get_variable("a")
    assert a == "test"

    gvm.set_variable("a", 123)
    a = gvm.get_variable("a")
    assert a == 123