def test_default_values_of_data_ports(caplog): storage_path = testing_utils.get_unique_temp_path() print(storage_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_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) print(root_state.output_data) try: assert root_state.output_data["output_data_port1"] == "default_value" finally: testing_utils.shutdown_environment_only_core(caplog=caplog)
def test_concurrency_barrier_save_load(caplog): concurrency_barrier_state = create_concurrency_barrier_state() state_machine = StateMachine(concurrency_barrier_state) test_path = testing_utils.get_unique_temp_path() storage.save_state_machine_to_path(state_machine, test_path) sm_loaded = storage.load_state_machine_from_path(test_path) root_state = sm_loaded.root_state input_data = {"input_data_port1": 0.1, "input_data_port2": 0.1} output_data = {"output_data_port1": None} root_state.input_data = input_data root_state.output_data = output_data 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_execution_engine.start(state_machine.state_machine_id) rafcon.core.singleton.state_machine_execution_engine.join() try: assert rafcon.core.singleton.global_variable_manager.get_variable("var_x") == 10 assert rafcon.core.singleton.global_variable_manager.get_variable("var_y") == 20 assert root_state.final_outcome.outcome_id == 4 with pytest.raises(ValueError): concurrency_barrier_state.remove(UNIQUE_DECIDER_STATE_ID) with pytest.raises(AttributeError): concurrency_barrier_state.remove_state(UNIQUE_DECIDER_STATE_ID) rafcon.core.singleton.state_machine_manager.remove_state_machine(state_machine.state_machine_id) finally: testing_utils.shutdown_environment_only_core(caplog=caplog, expected_warnings=0, expected_errors=1)
def save_state_machine(with_gui=True): import rafcon from rafcon.core.singleton import state_machine_execution_engine import rafcon.gui.singleton as gui_singleton from rafcon.core.storage import storage path = testing_utils.get_unique_temp_path() if with_gui: state_machine = call_gui_callback(create_models) else: state_machine = create_models() if with_gui: sm_model = rafcon.gui.singleton.state_machine_manager_model.state_machines[ state_machine.state_machine_id] menubar_ctrl = gui_singleton.main_window_controller.get_controller( 'menu_bar_controller') # sm_model.state_machine.base_path = path call_gui_callback(menubar_ctrl.on_save_as_activate, None, None, path) # call_gui_callback(menubar_ctrl.on_quit_activate, None) call_gui_callback(check_that_all_files_are_there, state_machine, with_print=False) else: storage.save_state_machine_to_path(state_machine, path, delete_old_state_machine=False) check_that_all_files_are_there(state_machine, with_print=False)
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()
def perform_temp_storage(self): if self.__perform_storage: return with self.state_machine_model.storage_lock, self.state_machine_model.state_machine.get_modification_lock( ): with self.timer_request_lock: self.__perform_storage = True sm = self.state_machine_model.state_machine logger.debug( 'Performing auto backup of state machine {} to temp folder'. format(sm.state_machine_id)) self.update_tmp_storage_path() storage.save_state_machine_to_path(sm, self._tmp_storage_path, delete_old_state_machine=True, as_copy=True) self.update_last_backup_meta_data() self.write_backup_meta_data() self.state_machine_model.store_meta_data( copy_path=self._tmp_storage_path) self.last_backup_time = time.time() # used as 'last-backup' time with self.timer_request_lock: self._timer_request_time = None self.tmp_timed_storage_thread = None self.__perform_storage = False self.marked_dirty = False self.check_lock_file()
def perform_temp_storage(self): if self.__perform_storage: # logger.debug("Do not perform storage, one is running!") return # logger.debug('acquire lock') self.state_machine_model.storage_lock.acquire() # logger.debug('acquired storage lock') self.state_machine_model.state_machine.acquire_modification_lock() # logger.debug('acquired state machine lock') self.timer_request_lock.acquire() self.__perform_storage = True self.timer_request_lock.release() sm = self.state_machine_model.state_machine logger.debug('Performing auto backup of state machine {} to temp folder'.format(sm.state_machine_id)) self.update_tmp_storage_path() storage.save_state_machine_to_path(sm, self._tmp_storage_path, delete_old_state_machine=True, as_copy=True) self.update_last_backup_meta_data() self.write_backup_meta_data() self.state_machine_model.store_meta_data(copy_path=self._tmp_storage_path) self.last_backup_time = time.time() # used as 'last-backup' time self.timer_request_lock.acquire() self._timer_request_time = None self.timer_request_lock.release() self.tmp_timed_storage_thread = None self.__perform_storage = False self.marked_dirty = False self.check_lock_file() # logger.debug('release lock') self.state_machine_model.state_machine.release_modification_lock() # logger.debug('released state machine lock') self.state_machine_model.storage_lock.release()
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()
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_unchanged_storage_format(caplog): """This test ensures that the state machine storage format does not change in patch releases""" from rafcon.core.storage import storage from rafcon.gui.models.state_machine import StateMachineModel import rafcon path = get_backward_compatibility_state_machines_path() if not os.path.exists(path): logger.info( "test_unchanged_storage_format: the current python interpreter version is not supported" ) return testing_utils.initialize_environment( gui_config={ 'HISTORY_ENABLED': False, 'AUTO_BACKUP_ENABLED': False }, libraries={ 'unit_test_state_machines': testing_utils.get_test_sm_path("unit_test_state_machines") }, gui_already_started=False) try: current_rafcon_version = StrictVersion(rafcon.__version__).version current_minor = "{}.{}".format(current_rafcon_version[0], current_rafcon_version[1]) for filename in os.listdir(path): if filename.startswith(current_minor): old_state_machine_path = os.path.join(path, filename) break else: assert False, "There is no state machine for the current RAFCON minor version {}".format( current_minor) state_machine = storage.load_state_machine_from_path( old_state_machine_path) state_machine_m = StateMachineModel(state_machine) new_state_machine_path = testing_utils.get_unique_temp_path() storage.save_state_machine_to_path(state_machine, new_state_machine_path, True, True) state_machine_m.store_meta_data(copy_path=new_state_machine_path) old_state_machine_hash = calculate_state_machine_hash( old_state_machine_path) new_state_machine_hash = calculate_state_machine_hash( new_state_machine_path) assert old_state_machine_hash.digest( ) == new_state_machine_hash.digest() except Exception: raise finally: testing_utils.shutdown_environment(caplog=caplog, unpatch_threading=False)
def on_save_activate(state_machine_m, logger): if state_machine_m is None or NO_SAVE: return save_path = state_machine_m.state_machine.file_system_path if save_path is None: return logger.debug("Saving state machine to {0}".format(save_path)) storage.save_state_machine_to_path(state_machine_m.state_machine, state_machine_m.state_machine.file_system_path, delete_old_state_machine=True) state_machine_m.root_state.store_meta_data() logger.debug("Successfully saved graphics meta data.")
def test_storage_with_gui(gui): from rafcon.core.storage import storage path = testing_utils.get_unique_temp_path() state_machine = gui(create_models) if gui.with_gui: sm_model = gui.singletons.state_machine_manager_model.state_machines[ state_machine.state_machine_id] menubar_ctrl = gui.singletons.main_window_controller.menu_bar_controller gui(menubar_ctrl.on_save_as_activate, None, None, path) else: storage.save_state_machine_to_path(state_machine, path, delete_old_state_machine=False) gui(check_that_all_files_are_there, state_machine, with_print=False)
def on_save_activate(state_machine_m, logger): from rafcon.core.storage import storage if state_machine_m is None: return save_path = state_machine_m.state_machine.base_path if save_path is None: return logger.debug("Saving state machine to {0}".format(save_path)) storage.save_state_machine_to_path(state_machine_m.state_machine, state_machine_m.state_machine.base_path, delete_old_state_machine=False) state_machine_m.root_state.store_meta_data() logger.debug("Successfully saved graphics meta data.")
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) with testing_utils.test_multithreading_lock: 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() testing_utils.assert_logger_warnings_and_errors(caplog) rafcon.core.singleton.state_machine_manager.delete_all_state_machines()
def test_scoped_variables(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) with testing_utils.test_multithreading_lock: 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) assert state_machine.root_state.output_data["output_data_port1"] == 42 testing_utils.assert_logger_warnings_and_errors(caplog)
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()
def run_copy_performance_test_and_check_storage_copy(*args): """Run general test that """ from rafcon.gui.models.state_machine import StateMachineModel from rafcon.core.storage import storage sm_m = args[0] sm = sm_m.state_machine # storage copy tests if sm.file_system_path is None: tmp_sm_system_path = join(testing_utils.RAFCON_TEMP_PATH_TEST_BASE, 'copy_test_' + str(sm.state_machine_id)) else: tmp_sm_system_path = join(testing_utils.RAFCON_TEMP_PATH_TEST_BASE, 'copy_test' + sm.file_system_path) # performance tests time_only_storage_before = time.time() storage.save_state_machine_to_path(sm_m.state_machine, tmp_sm_system_path, delete_old_state_machine=False, as_copy=True) sm_m.store_meta_data(copy_path=tmp_sm_system_path) time_only_storage_after = time.time() only_storage_duration = round(time_only_storage_after * 1000000) - round( time_only_storage_before * 1000000) time_storage_before = time.time() storage.save_state_machine_to_path(sm_m.state_machine, tmp_sm_system_path, delete_old_state_machine=False, as_copy=True) sm_m.store_meta_data(copy_path=tmp_sm_system_path) sm1 = storage.load_state_machine_from_path(tmp_sm_system_path) time_model_before = time.time() sm1_m = StateMachineModel(sm1) time_model_after = time.time() time_storage_after = time_model_after only_model_duration = round(time_model_after * 1000000) - round( time_model_before * 1000000) storage_copy_duration = round(time_storage_after * 1000000) - round( time_storage_before * 1000000) equal_check_state(sm_m.root_state.state, sm1_m.root_state.state) equal_check_state_model(sm_m.root_state, sm1_m.root_state) time_copy_before = time.time() copy.copy(sm_m.state_machine) time_copy_after = time.time() core_copy_duration = round(time_copy_after * 1000000) - round( time_copy_before * 1000000) time_copy_m_before = time.time() copy.copy(sm_m) time_copy_m_after = time.time() model_copy_duration = round(time_copy_m_after * 1000000) - round( time_copy_m_before * 1000000) sm1_m.destroy() print("only_model_duration: {}".format(only_model_duration)) print("only_storage_duration: {}".format(only_storage_duration)) print("storage_copy_duration: {}".format(storage_copy_duration)) print("core_copy_duration: {}".format(core_copy_duration)) print("model_copy_duration: {}".format(model_copy_duration))