Exemplo n.º 1
0
def test_preemption_behaviour_during_stop(caplog):
    testing_utils.initialize_environment_core()

    path = testing_utils.get_test_sm_path(
        os.path.join("unit_test_state_machines",
                     "preemption_behaviour_during_stop"))
    state_machine = storage.load_state_machine_from_path(path)
    rafcon.core.singleton.state_machine_manager.add_state_machine(
        state_machine)

    thread = threading.Thread(
        target=trigger_stop,
        args=[
            state_machine, rafcon.core.singleton.state_machine_execution_engine
        ])
    thread.start()

    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 global_variable_manager.get_variable("s1") == 1
        assert global_variable_manager.get_variable("s2") == 1
        assert not global_variable_manager.variable_exist("s3")
    finally:
        testing_utils.shutdown_environment_only_core(caplog=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)
 def test_inner_observer_1_error(self, caplog):
     gvm.set_variable("wait_inner_observer_1", 0.1)
     gvm.set_variable("inner_observer_1_abort", True)
     self.run_state_machine()
     assert_gvm("inner_error_handler", False)
     assert_gvm("error_handler")
     assert_gvm("error_handler_2")
     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)
 def test_observer_2_finish(self, caplog):
     gvm.set_variable("wait_observer_2", 0.1)
     self.run_state_machine()
     self.assert_no_errors()
     assert_gvm("observer_2_finish")
     assert_gvm("inner_exit_handler")
     assert_gvm("exit_handler")
     assert_gvm("exit_handler_2")
     assert_all_false(gvm.get_variable("inner_observer_2_finish"), gvm.get_variable("inner_observer_1_finish"),
                      gvm.get_variable("observer_1_finish"))
     testing_utils.assert_logger_warnings_and_errors(caplog, 0)
Exemplo n.º 5
0
def test_preemptive_wait2_timeout(caplog):
    with testing_utils.test_multithreading_lock:
        gvm.set_variable('state_2_wait', 0.5)
        gvm.set_variable('state_1_wait', 1.)

        run_state_machine()

        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)
Exemplo n.º 6
0
def test_preemptive_wait_daemon(caplog):
    with testing_utils.test_multithreading_lock:
        gvm.set_variable('state_1_wait', 0.5)
        gvm.set_variable('state_2_wait', None)

        run_state_machine()

        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)
Exemplo n.º 7
0
def test_preemptive_wait_timeout(caplog):
    testing_utils.test_multithreading_lock.acquire()

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

    run_state_machine()

    try:
        assert 0.5 < gvm.get_variable('state_1_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()
Exemplo n.º 8
0
def test_preemption_behaviour_in_preemption_state(caplog):
    testing_utils.initialize_environment_core()

    sm = state_machine_execution_engine.execute_state_machine_from_path(
        path=testing_utils.get_test_sm_path(os.path.join("unit_test_state_machines", "preemption_behaviour_test_sm")))
    rafcon.core.singleton.state_machine_manager.remove_state_machine(sm.state_machine_id)
    from rafcon.core.singleton import global_variable_manager
    try:
        assert global_variable_manager.get_variable("s2") == 1.0
        assert not global_variable_manager.variable_exist("s3")
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog)
Exemplo n.º 9
0
def test_multi_events(caplog):
    testing_utils.initialize_environment_core()

    execution_trigger_thread = threading.Thread(target=trigger_exectuion_engine,
                                                args=[global_variable_manager, state_machine_execution_engine])
    execution_trigger_thread.start()

    sm = state_machine_execution_engine.execute_state_machine_from_path(
        path=testing_utils.get_test_sm_path(os.path.join("unit_test_state_machines", "multi_events_test")))

    execution_trigger_thread.join()
    state_machine_manager.remove_state_machine(sm.state_machine_id)
    try:
        assert global_variable_manager.get_variable("sm_status") == 2
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog, expected_warnings=0, expected_errors=0)
Exemplo n.º 10
0
def server_interaction_worker(queue_dict, execution_engine, state_machine_id):
    from rafcon.core.singleton import global_variable_manager as gvm
    from rafcon.core.execution.execution_status import StateMachineExecutionStatus
    from rafcon.core.singleton import state_machine_manager
    from monitoring import server
    from monitoring.monitoring_manager import global_monitoring_manager

    sm = state_machine_manager.state_machines[state_machine_id]
    print("current state machine", sm)
    root_state = sm.root_state
    sleep_time = 0.99

    for key, sv in sm.root_state.scoped_variables.items():
        if sv.name == "bottles":
            sv.default_value = 3

    #######################################################
    print("\n\n\nserver TEST1 stop - start - wait_for_stop")
    #######################################################

    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "start_test_1"
    print("received: ", queue_element)

    # wait before Decimate Bottles
    execution_engine.run_to_selected_state("GLSUJY/NDIVLD",
                                           state_machine_id=state_machine_id)
    # check when run to is finished; run_to is issued from the server thread
    while gvm.get_variable("sing_counter") < 1:
        print("wait for client")
        time.sleep(sleep_time)
    print("starting tests\n\n")

    print("put: started")
    queue_dict[SERVER_TO_CLIENT].put("started")

    # step1: stop start test
    while not execution_engine.finished_or_stopped():
        print("wait until state machine finished!")
        time.sleep(sleep_time)
    print("put: stop_received")
    queue_dict[SERVER_TO_CLIENT].put("stop_received")

    # step2: wait until state machine started
    while execution_engine.status.execution_mode is not StateMachineExecutionStatus.STARTED:
        print("execution_engine.status.execution_mode: ",
              execution_engine.status.execution_mode)
        # this time has to be very small otherwise the state machine start and finish during one sleep period
        # each state has a sleep of 0.01s
        time.sleep(0.01)
    print("put: started")
    queue_dict[SERVER_TO_CLIENT].put("restarted")

    # stop the state machine locally
    execution_engine.stop()  # reset state machine before the next test
    execution_engine.join()

    reset_global_variable_manager(gvm)
    queue_dict[SERVER_TO_CLIENT].put("successfully stopped the state machine")

    # queue_dict[KILL_SERVER_QUEUE].get()
    # os._exit(0)
    # return

    #######################################################
    print("\n\n\nserver TEST2 disconnect -> run sm -> connect -> run sm")
    #######################################################

    print("server: starting test 2")
    reset_global_variable_manager(gvm)

    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "start_test_2"
    print("received: ", queue_element)

    # step 1
    # test disconnect by client run
    print("disconnect/connect by client test")
    queue_dict[SERVER_TO_CLIENT].put("disconnect_me_and_run")
    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "disconnected_and_executed"
    print("received: ", queue_element)

    # the client executed a start command, which must not do anything as the client disconnected beforehand

    assert gvm.get_variable("count_counter") == 0
    assert gvm.get_variable("sing_counter") == 0
    assert gvm.get_variable("decimate_counter") == 0

    # step 2
    queue_dict[SERVER_TO_CLIENT].put("reconnect_me_and_run")
    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "reconnected_and_executed"
    print("received: ", queue_element)

    execution_engine.join()
    print("server sm finished")
    assert gvm.get_variable("count_counter") == 3
    assert gvm.get_variable("sing_counter") == 3
    assert gvm.get_variable("decimate_counter") == 3
    queue_dict[SERVER_TO_CLIENT].put("succeeded")

    execution_engine.stop()
    execution_engine.join()
    reset_global_variable_manager(gvm)
    print("server: disconnect/connect by client test successful\n\n")

    # queue_dict[KILL_SERVER_QUEUE].get()
    # os._exit(0)
    # return

    #######################################################
    print("\n\n\nserver TEST3 disable -> run sm -> enable -> run sm ")
    #######################################################

    reset_global_variable_manager(gvm)

    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "start_test_3"
    print("server received: ", queue_element)

    for address in server.network_manager_model.connected_ip_port:
        global_monitoring_manager.disable(address)
        # while not server.network_manager_model.get_connected_status(address) == "disabled":
        #     time.sleep(0.01)
    queue_dict[SERVER_TO_CLIENT].put("you_are_disabled")

    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "reached end"
    print("server received: ", queue_element)

    assert gvm.get_variable("count_counter") == 0
    assert gvm.get_variable("sing_counter") == 0
    assert gvm.get_variable("decimate_counter") == 0

    # TODO: reconnect is not properly implemented
    # for address in server.network_manager_model.connected_ip_port:
    #     global_monitoring_manager.reconnect(address)

    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "enabled_again"
    print("server received: ", queue_element)

    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "started_execution"
    print("server received: ", queue_element)

    if not execution_engine.finished_or_stopped():
        execution_engine.join()
    print("server sm finished")
    assert gvm.get_variable("count_counter") == 3
    assert gvm.get_variable("sing_counter") == 3
    assert gvm.get_variable("decimate_counter") == 3
    queue_dict[SERVER_TO_CLIENT].put("succeeded")
    execution_engine.stop()
    execution_engine.join()
    reset_global_variable_manager(gvm)
    print("server: dis- /enabled by server test successful\n\n")

    # queue_dict[KILL_SERVER_QUEUE].get()
    # os._exit(0)
    # return

    #######################################################
    print(
        "server TEST4 change client ID in config -> apply -> check connected client ID"
    )
    #######################################################
    reset_global_variable_manager(gvm)

    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "start_test_4"
    print("server received: ", queue_element)

    queue_dict[SERVER_TO_CLIENT].put("ready_to_change_config")

    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "on_apply_button_clicked"
    print("server received: ", queue_element)

    client_id = []
    for address in server.network_manager_model.connected_ip_port:
        client_id.append(
            server.network_manager_model.get_connected_id(address))
    assert "apply_test_client_id" in client_id
    queue_dict[SERVER_TO_CLIENT].put("succeeded")
    print("apply config test successful\n\n")

    execution_engine.stop()
    execution_engine.join()
    queue_dict[KILL_SERVER_QUEUE].get()
    os._exit(0)
def assert_gvm(key, value=True):
    assert gvm.get_variable(key) == value
Exemplo n.º 12
0
def synchronize_with_clients_threads(queue_dict, execution_engine):
    from rafcon.core.singleton import global_variable_manager as gvm
    from rafcon.core.execution.execution_status import StateMachineExecutionStatus
    from rafcon.core.singleton import state_machine_manager
    from rafcon.core.execution.execution_engine import ExecutionEngine
    assert isinstance(execution_engine, ExecutionEngine)
    active_sm = state_machine_manager.get_active_state_machine()
    root_state = active_sm.root_state
    sleep_time = 0.01

    # check when run to is finished
    while gvm.get_variable("sing_counter") < 1:
        time.sleep(sleep_time)
    synchronize_with_root_state(root_state, execution_engine, "PXTKIH")

    # wait for the client to start
    queue_dict[CLIENT1_TO_SERVER_QUEUE].get()

    queue_dict[SERVER_TO_CLIENT1_QUEUE].put("start stepping")
    print "starting tests\n\n"

    print "server: cp0"
    # step test
    while gvm.get_variable("decimate_counter") < 1:
        time.sleep(sleep_time)  # sleep just to prevent busy loop
    synchronize_with_root_state(root_state, execution_engine, "NDIVLD")
    queue_dict[SERVER_TO_CLIENT1_QUEUE].put(
        TestSteps[0])  # step mode also means a step into

    print "server: cp1"
    while gvm.get_variable("count_counter") < 1:
        time.sleep(sleep_time)
    synchronize_with_root_state(root_state, execution_engine, "SFZGMH")
    queue_dict[SERVER_TO_CLIENT1_QUEUE].put(TestSteps[1])  # step over

    print "server: cp2"
    while gvm.get_variable("sing_counter") < 2:
        time.sleep(sleep_time)
    synchronize_with_root_state(root_state, execution_engine, "PXTKIH")
    queue_dict[SERVER_TO_CLIENT1_QUEUE].put(TestSteps[2])  # step into

    print "server: cp3"
    while gvm.get_variable("sing_counter") > 1:
        time.sleep(sleep_time)
    # wait until the backward execution of the state is done
    synchronize_with_root_state(root_state, execution_engine, "PXTKIH")
    queue_dict[SERVER_TO_CLIENT1_QUEUE].put(TestSteps[3])  # backward step

    print "server: cp4"
    while not execution_engine.finished_or_stopped():
        time.sleep(sleep_time)
    execution_engine.join()
    queue_dict[SERVER_TO_CLIENT1_QUEUE].put(
        TestSteps[4])  # step out and run until the end
    reset_global_variable_manager(gvm)
    print "server: step test successful\n\n"

    # start and finish execution test
    while execution_engine.status.execution_mode is not StateMachineExecutionStatus.STARTED:
        time.sleep(sleep_time)
    # wait until the state machine executed successfully
    while not execution_engine.finished_or_stopped():
        time.sleep(sleep_time)
    reset_global_variable_manager(gvm)
    # as the state machine run to the end this is safe
    execution_engine.stop()  # reset state machine before the next test
    # do not notify the client before the state machine is really stopped
    execution_engine.join()
    print "server: start and wait until finished test successful\n\n"
    # old_sync_counter = execution_engine.synchronization_counter
    queue_dict[SERVER_TO_CLIENT1_QUEUE].put(TestSteps[5])  # run until end

    # run-until test
    # this is dangerous: the child state is still being executed when setting the "decimate_counter"
    # global variable and thus the root_state=Hierarchy state is not YET handling the execution mode
    # while gvm.get_variable("decimate_counter") < 1 and not root_state.handling_execution_mode:
    #     time.sleep(sleep_time)
    # this also can produce race conditions
    # while not(execution_engine.synchronization_counter == old_sync_counter + 3):
    #     time.sleep(sleep_time)
    # this is safe:
    while gvm.get_variable("decimate_counter") < 1:
        time.sleep(sleep_time)
    synchronize_with_root_state(root_state, execution_engine, "NDIVLD")
    assert gvm.get_variable("count_counter") == 0
    queue_dict[SERVER_TO_CLIENT1_QUEUE].put(TestSteps[6])

    while not execution_engine.finished_or_stopped():
        time.sleep(sleep_time)
    execution_engine.join()
    reset_global_variable_manager(gvm)
    print "server: run until test successful\n\n"
    queue_dict[SERVER_TO_CLIENT1_QUEUE].put(TestSteps[7])

    # start from test
    while execution_engine.status.execution_mode is not StateMachineExecutionStatus.STARTED:
        time.sleep(sleep_time)
    while not execution_engine.finished_or_stopped():
        time.sleep(sleep_time)
    assert gvm.get_variable("sing_counter") == 2
    assert gvm.get_variable("decimate_counter") == 3
    # as the state machine run to the end this is safe
    execution_engine.stop()
    execution_engine.join()
    print "server: start from test successful\n"
    queue_dict[SERVER_TO_CLIENT1_QUEUE].put(TestSteps[8])

    execution_engine.stop()
    execution_engine.join()
    print "server: wait for sync message from client\n"
    queue_dict[CLIENT1_TO_SERVER_QUEUE].get()
    print "server: send sync message to client\n"
    queue_dict[SERVER_TO_CLIENT1_QUEUE].put("sync")
    print "server: wait for kill command from main queue\n"
    # set a timeout of 3 seconds
    queue_dict[KILL_SERVER_QUEUE].get(3)
    os._exit(0)
Exemplo n.º 13
0
def synchronize_with_clients_threads(queue_dict, execution_engine):
    from rafcon.core.singleton import global_variable_manager as gvm
    from rafcon.core.execution.execution_status import StateMachineExecutionStatus
    from rafcon.core.singleton import state_machine_manager
    active_sm = state_machine_manager.get_active_state_machine()
    root_state = active_sm.root_state
    sleep_time = 0.01

    # check when run to is finished
    while gvm.get_variable("sing_counter") < 1:
        time.sleep(sleep_time)
    """
    TEST1
    """
    queue_dict[SERVER_TO_CLIENT].put("start")
    print "starting tests\n\n"

    # stop start test
    while not execution_engine.finished_or_stopped():
        time.sleep(sleep_time)

    queue_dict[SERVER_TO_CLIENT].put("stop received")
    # wait until state machine started
    while execution_engine.status.execution_mode is not StateMachineExecutionStatus.STARTED:
        time.sleep(sleep_time)

    while not execution_engine.finished_or_stopped():
        time.sleep(sleep_time)

    reset_global_variable_manager(gvm)
    execution_engine.stop()  # reset state machine before the next test
    if execution_engine.status.execution_mode is not StateMachineExecutionStatus.STOPPED:
        execution_engine.join()
    queue_dict[SERVER_TO_CLIENT].put(
        "start received and successfully ran the state machine")
    print "server: stop start test successful\n\n"
    """
    TEST2 disconnect -> run sm -> connect -> run sm
    """
    from monitoring import server
    from monitoring.monitoring_manager import global_monitoring_manager
    # test disconnect by client run
    print "disconnect/connect by client test"
    queue_dict[SERVER_TO_CLIENT].put("disconnect_me_and_run")
    queue_dict[CLIENT_TO_SERVER].get()
    assert gvm.get_variable("count_counter") == 0
    assert gvm.get_variable("sing_counter") == 0
    assert gvm.get_variable("decimate_counter") == 0
    queue_dict[SERVER_TO_CLIENT].put("reconnect_me_and_run")
    queue_dict[CLIENT_TO_SERVER].get()
    if not execution_engine.finished_or_stopped():
        execution_engine.join()
    print "server sm finished"
    assert gvm.get_variable("count_counter") == 3
    assert gvm.get_variable("sing_counter") == 3
    assert gvm.get_variable("decimate_counter") == 3
    queue_dict[SERVER_TO_CLIENT].put("succeeded")
    execution_engine.stop()
    if not execution_engine.finished_or_stopped():
        execution_engine.join()
    reset_global_variable_manager(gvm)
    print "server: disconnect/connect by client test successful\n\n"
    """
    TEST3 disable -> run sm -> enable -> run sm
    """
    # test dis- enabled by server run
    print "dis- /enabled test by server"
    for address in server.network_manager_model.connected_ip_port:
        global_monitoring_manager.disable(address)
        # while not server.network_manager_model.get_connected_status(address) == "disabled":
        #     time.sleep(0.01)
    queue_dict[SERVER_TO_CLIENT].put("you are disabled")
    queue_dict[CLIENT_TO_SERVER].get()
    print "sm on client executed and stopped"
    assert gvm.get_variable("count_counter") == 0
    assert gvm.get_variable("sing_counter") == 0
    assert gvm.get_variable("decimate_counter") == 0
    for address in server.network_manager_model.connected_ip_port:
        global_monitoring_manager.disable(address)
    queue_dict[SERVER_TO_CLIENT].put("you are enabled")
    queue_dict[CLIENT_TO_SERVER].get()
    if not execution_engine.finished_or_stopped():
        execution_engine.join()
    print "server sm finished"
    assert gvm.get_variable("count_counter") == 3
    assert gvm.get_variable("sing_counter") == 3
    assert gvm.get_variable("decimate_counter") == 3
    queue_dict[SERVER_TO_CLIENT].put("succeeded")
    execution_engine.stop()
    if not execution_engine.finished_or_stopped():
        execution_engine.join()
    reset_global_variable_manager(gvm)
    print "server: dis- /enabled by server test successful\n\n"
    """
    TEST4 change client ID in config -> apply -> check connected client ID
    """
    print "apply config test"
    queue_dict[SERVER_TO_CLIENT].put("ready to change config")
    queue_dict[CLIENT_TO_SERVER].get()
    client_id = []
    for address in server.network_manager_model.connected_ip_port:
        client_id.append(
            server.network_manager_model.get_connected_id(address))
    assert "apply_test_client_id" in client_id
    queue_dict[SERVER_TO_CLIENT].put("succeeded")
    print "apply config test successful\n\n"

    execution_engine.stop()
    execution_engine.join()
    queue_dict[KILL_SERVER_QUEUE].get()
    os._exit(0)