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 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_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 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 receiveMsg_StartMessage(self, message: StartMessage, sender: ActorAddress): """ When receiving a StartMessage : - if the actor is already initialized, answer with a ErrorMessage - otherwise initialize the actor with the abstract method _initialization and answer with an OKMessage """ self.name = message.name self.log_debug('received message ' + str(message)) self.parent = sender if self.initialized: self.send(sender, ErrorMessage(self.name, 'Actor already initialized')) return if not isinstance(message, self.start_message_cls): self.send( sender, ErrorMessage( self.name, 'use ' + self.start_message_cls.__name__ + ' instead of StartMessage')) return try: self._initialization(message) except InitializationException as exn: self.send(sender, ErrorMessage(self.name, exn.msg)) self.send(self.myAddress, ActorExitRequest()) return self.initialized = True self.send(sender, OKMessage(self.name)) self.log_info(self.name + ' started')
def receiveMsg_PingMessage(self, message: PingMessage, sender: ActorAddress): """ When receiving a PingMessage, the actor answer with a OKMessage to its sender """ self.log_debug('received message ' + str(message)) self.send(sender, OKMessage(self.name))
def test_pusher_power_handler(self): """ Test the PowerHandler 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 PowerHandler power_handler = PowerHandler() # Test Random message when state is not initialized to_send = [OKMessage(), ErrorMessage("Error"), create_report_root({})] for msg in to_send: power_handler.handle(msg, pusher_state) assert pusher_state.database.method_calls == [] assert pusher_state.initialized is False pusher_state.initialized = True # Test Random message when state is initialized to_send = [OKMessage(), ErrorMessage("Error"), create_report_root({})] for msg in to_send: power_handler.handle(msg, pusher_state) assert pusher_state.database.method_calls == [] # Test with a PowerMessage for _ in range(101): power_handler.handle( PowerReport("10", "test", "test", "test", "test"), pusher_state) assert len(pusher_state.buffer) == 101
def handle(self, msg): if self.state.initialized: self.state.actor.send_control( ErrorMessage('Actor already initialized')) return if not isinstance(msg, StartMessage): return self.initialization() if self.state.alive: self.state.initialized = True self.state.actor.send_control(OKMessage()) self.pull_db()
def handle(self, msg): """ Allow to initialize the state of the actor, then reply to the control socket. :param powerapi.StartMessage msg: Message that initialize the actor """ if self.state.initialized: self.state.actor.send_control( ErrorMessage('Actor already initialized')) return if not isinstance(msg, StartMessage): return self.initialization() self.state.initialized = True self.state.actor.send_control(OKMessage())
def test_pusher_power_handler(self): """ Test the ReportHandler of PusherActor """ # Define PusherState fake_database = get_fake_db() fake_socket_interface = get_fake_socket_interface() pusher_state = PusherState(Mock(), fake_database, HWPCModel()) pusher_state.actor.socket_interface = fake_socket_interface assert pusher_state.initialized is False # Define PowerHandler power_handler = ReportHandler(pusher_state) # Test Random message when state is not initialized to_send = [OKMessage(), ErrorMessage("Error"), create_report_root({})] for msg in to_send: power_handler.handle_message(msg) assert pusher_state.database.method_calls == [] assert pusher_state.initialized is False pusher_state.initialized = True
def handle(self, msg, state): """ Allow to initialize the state of the actor, then reply to the control socket. :param powerapi.StartMessage msg: Message that initialize the actor :param powerapi.State state: State of the actor :rtype powerapi.State: the new state of the actor """ if state.initialized: state.socket_interface.send_control( ErrorMessage('Actor already initialized')) return state if not isinstance(msg, StartMessage): return state state = self.initialization(state) state.initialized = True state.socket_interface.send_control(OKMessage()) return state
def receive_control(self, timeout=None): return OKMessage()