def test_dispatcher_start_handler(self): """ Test the StartHandler of DispatcherActor """ # Define DispatcherState fake_socket_interface = get_fake_socket_interface() dispatcher_state = DispatcherState(Actor._initial_behaviour, fake_socket_interface, Mock(), None, None) assert dispatcher_state.initialized is False # Define StartHandler start_handler = StartHandler() # Test Random message when state is not initialized to_send = [ OKMessage(), ErrorMessage("Error"), HWPCReport("test", "test", "test", {}) ] for msg in to_send: start_handler.handle(msg, dispatcher_state) assert fake_socket_interface.method_calls == [] assert dispatcher_state.initialized is False # Try to initialize the state start_handler.handle(StartMessage(), dispatcher_state) assert dispatcher_state.initialized is True
def launch_actor(self, actor, start_message=True): """ Launch the actor : - start the process that execute the actor code - connect the data and control socket - send a StartMessage to initialize the actor if needed :param boolean startMessage: True a StartMessage need to be sent to this actor :raise: zmq.error.ZMQError if a communication error occurs :raise: powerapi.actor.ActorInitError if the actor crash during the initialisation process """ if actor.is_alive(): raise ActorAlreadyLaunchedException() actor.start() actor.connect_control() actor.connect_data() if start_message: actor.send_control(StartMessage()) msg = actor.receive_control(2000) if isinstance(msg, ErrorMessage): raise ActorInitError(msg.error_message) elif msg is None: if actor.is_alive(): actor.terminate() print (actor) raise FailConfigureError("Unable to configure the " + actor.name) else: raise CrashConfigureError("The " + actor.name + " crash during initialisation process") self.supervised_actors.append(actor)
def test_puller_init_already_init(initialized_puller): """ Create a PullerActor and send a StartMessage to an already initialized Actor """ initialized_puller.send_control(StartMessage()) assert is_actor_alive(initialized_puller) assert isinstance(initialized_puller.receive_control(), ErrorMessage)
def test_dispatcher_start_handler(self): """ Test the StartHandler of DispatcherActor """ # Define DispatcherState dispatcher_state = DispatcherState(Mock(), None, None) assert dispatcher_state.initialized is False # Define StartHandler start_handler = StartHandler(dispatcher_state) # Test Random message when state is not initialized to_send = [ OKMessage(), ErrorMessage("Error"), HWPCReport("test", "test", "test", {}) ] for msg in to_send: start_handler.handle(msg) assert dispatcher_state.initialized is False # Try to initialize the state start_handler.handle(StartMessage()) assert dispatcher_state.initialized is True
def started_actor(self, init_actor, fake_db): init_actor.send_control(StartMessage()) # remove OkMessage from control socket _ = init_actor.receive_control(2000) # remove 'connected' string from Queue _ = fake_db.q.get(timeout=2) return init_actor
def test_pusher_start_handler(self): """ Test the StartHandler of PusherActor """ # Define PusherState fake_database = get_fake_db() fake_socket_interface = get_fake_socket_interface() pusher_state = PusherState(Actor._initial_behaviour, fake_socket_interface, fake_database, mock.Mock()) assert pusher_state.initialized is False # Define StartHandler start_handler = PusherStartHandler() # Test Random message when state is not initialized to_send = [OKMessage(), ErrorMessage("Error"), create_report_root({})] for msg in to_send: start_handler.handle(msg, pusher_state) assert fake_database.method_calls == [] assert fake_socket_interface.method_calls == [] assert pusher_state.initialized is False # Try to initialize the state start_handler.handle(StartMessage(), pusher_state) assert pusher_state.initialized is True
def test_puller_start_handler(self): """ Test the StartHandler of PullerActor """ # Define PullerState fake_database = get_fake_db() fake_socket_interface = get_fake_socket_interface() fake_filter = get_fake_filter() puller_state = PullerState(Mock(), fake_database, fake_filter, HWPCModel(), False, 100) puller_state.actor.receive_control = Mock(return_value=None) assert puller_state.initialized is False # Define StartHandler start_handler = PullerStartHandler(puller_state, 0.1) # Test Random message when state is not initialized to_send = [OKMessage(), ErrorMessage("Error"), create_report_root({})] for msg in to_send: start_handler.handle(msg) assert fake_database.method_calls == [] assert fake_socket_interface.method_calls == [] assert fake_filter.method_calls == [] assert puller_state.initialized is False # Try to initialize the state start_handler.handle(StartMessage()) assert puller_state.initialized is True
def test_send_StartMessage_dispatcher_already_init(initialized_dispatcher): """ Create a Dispatcher with a PrimaryDispatchRule rule, initialize it and send it another StartMessage Test : - if the actor is alive - if the actor sent a ErrorMessage on the control canal """ initialized_dispatcher.send_control(StartMessage()) assert is_actor_alive(initialized_dispatcher) assert isinstance(initialized_dispatcher.receive_control(), ErrorMessage)
def test_starting_actor_with_a_non_PullerStartMessage_must_answer_error_message( self, system, actor): puller_start_message = StartMessage('system', 'puller_test') answer = system.ask(actor, puller_start_message) assert isinstance(answer, ErrorMessage) assert answer.error_message == 'use PullerStartMessage instead of StartMessage'
def test_starting_dummy_formula_without_DummyFormulaStartMessage_answer_ErrorMessage(self, system, actor): answer = system.ask(actor, StartMessage('system', 'test')) assert isinstance(answer, ErrorMessage) assert answer.error_message == 'use FormulaStartMessage instead of StartMessage'
def test_starting_actor_make_it_connect_to_database( self, init_actor, fake_db): init_actor.send_control(StartMessage()) assert fake_db.q.get(timeout=2) == 'connected'
def test_send_start_message_to_puller_without_filter_answer_with_error_message( self, init_actor): init_actor.send_control(StartMessage()) msg = init_actor.receive_control(2000) assert isinstance(msg, ErrorMessage)
def test_send_message_on_control_canal_to_non_initialized_actor_raise_NotConnectedException( self, actor): with pytest.raises(NotConnectedException): actor.send_control(StartMessage())
def test_send_StartMessage_to_already_started_actor_answer_ErrorMessage( self, started_actor): started_actor.send_control(StartMessage()) msg = started_actor.receive_control(2000) assert isinstance(msg, ErrorMessage) assert msg.error_message == 'Actor already initialized'
def test_send_StartMessage_answer_OkMessage(self, init_actor): init_actor.send_control(StartMessage()) msg = init_actor.receive_control(2000) print(msg) assert isinstance(msg, OKMessage)
def started_actor(self, init_actor): init_actor.send_control(StartMessage()) _ = init_actor.receive_control(2000) return init_actor
def test_starting_actor_without_DispatcherStartMessage_must_answer_error_message(self, system, actor, logger): answer = system.ask(actor, StartMessage('system', 'test')) assert isinstance(answer, ErrorMessage) assert answer.error_message == 'use DispatcherStartMessage instead of StartMessage'
def started_actor_with_crash_handler(self, actor_with_crash_handler): actor_with_crash_handler.send_control(StartMessage()) _ = actor_with_crash_handler.receive_control(2000) return actor_with_crash_handler
def run(self): handler.handle(StartMessage())