def test_one_message_between_two(self, http_comms): comm1, comm2 = http_comms comm1.discovery.register_computation('c2', 'a2', ('127.0.0.1', 10002)) comm2.discovery.register_computation('c1', 'a1', ('127.0.0.1', 10001)) comm1.send_msg( 'a1', 'a2', ComputationMessage('c1', 'c2', Message('test', 'test'), MSG_ALGO)) comm2.messaging.post_msg.assert_called_with('c1', 'c2', Message('test', 'test'), MSG_ALGO)
def test_several_messages_between_two(self, http_comms): comm1, comm2 = http_comms comm1.discovery.register_computation('c1', 'a2', ('127.0.0.1', 10002)) comm2.discovery.register_computation('c2', 'a1', ('127.0.0.1', 10001)) comm1.send_msg( 'a1', 'a2', ComputationMessage('c1', 'c2', Message('test', 'test1'), MSG_ALGO)) comm1.send_msg\ ('a1', 'a2', ComputationMessage('c1', 'c2', Message('test', 'test2'), MSG_ALGO)) comm1.send_msg( 'a1', 'a2', ComputationMessage('c1', 'c2', Message('test', 'test3'), MSG_MGT)) comm1.send_msg( 'a1', 'a2', ComputationMessage('c1', 'c2', Message('test', 'test4'), MSG_ALGO)) comm2.messaging.post_msg.assert_has_calls([ call('c1', 'c2', Message('test', 'test1'), MSG_ALGO), call('c1', 'c2', Message('test', 'test2'), MSG_ALGO), call('c1', 'c2', Message('test', 'test3'), MSG_MGT), call('c1', 'c2', Message('test', 'test4'), MSG_ALGO), ])
def test_simple_repr(self): msg = Message('test', 'str_content') r = simple_repr(msg) self.assertEqual(r['msg_type'], 'test') self.assertEqual(r['content'], 'str_content')
def on_start(self): # when starting, subscribe to all sensor variable used in the # condition of the rule for v in self._read_only_variables: self._msg_sender.post_msg(self.name, v.name, Message("SUBSCRIBE", None)) super().on_start()
def test_simple_repr(self): msg = Message("test", "str_content") r = simple_repr(msg) self.assertEqual(r["msg_type"], "test") self.assertEqual(r["content"], "str_content")
def test_from_repr(self): msg = Message('test', 'str_content') r = simple_repr(msg) msg2 = from_repr(r) self.assertEqual(msg, msg2)
def test_from_repr(self): msg = Message("test", "str_content") r = simple_repr(msg) msg2 = from_repr(r) self.assertEqual(msg, msg2)
def test_msg_to_unknown_agent_ignore_mode(self, http_comms): comm1, comm2 = http_comms # on a1, do NOT register a2, and still try to send a message to it # Default mode is ignore : always returns True assert comm1.send_msg( 'a1', 'a2', ComputationMessage('c1', 'c2', Message('a1', 't1'), MSG_ALGO))
def test_msg_to_unknown_agent_fail_mode(self, http_comms): comm1, comm2 = http_comms # on a1, do NOT register a2, and still try to send a message to it with pytest.raises(UnknownAgent): comm1.send_msg('a1', 'a2', ComputationMessage('c1', 'c2', Message('a1', 't1'), MSG_ALGO), on_error='fail')
def test_msg_to_unreachable_agent_ignore_mode(self, http_comms): comm1, comm2 = http_comms # on a1, register a2 with the wrong port number comm1.discovery.register_computation('c2', 'a2', ('127.0.0.1', 10006)) comm2.discovery.register_computation('c1', 'a1', ('127.0.0.1', 10001)) assert comm1.send_msg( 'a1', 'a2', ComputationMessage('c1', 'c2', Message('a1', 't'), MSG_ALGO))
def test_setting_message_sender_on_computation(): c = MessagePassingComputation("c") c.message_sender = MagicMock() msg = Message("type") c.post_msg("target", msg) c.message_sender.assert_called_with("c", "target", msg, None, None)
def test_msg_to_unreachable_agent_fail_mode(self, http_comms): comm1, comm2 = http_comms # on a1, register a2 with the wrong port number comm1.discovery.register_computation('c2', 'a2', ('127.0.0.1', 10006)) comm2.discovery.register_computation('c1', 'a1', ('127.0.0.1', 10001)) with pytest.raises(UnreachableAgent): comm1.send_msg('a1', 'a2', ComputationMessage('c1', 'c2', Message('a1', '1'), MSG_ALGO), on_error='fail')
def test_msg_to_unknown_computation_ignore_mode(self, http_comms): comm1, comm2 = http_comms comm1.discovery.register_computation('c2', 'a2', ('127.0.0.1', 10002)) comm2.discovery.register_computation('c1', 'a1', ('127.0.0.1', 10001)) def raise_unknown(*args): raise UnknownComputation('test') comm2.messaging.post_msg = MagicMock(side_effect=raise_unknown) # Default mode is ignore : always returns True assert comm1.send_msg( 'a1', 'a2', ComputationMessage('c1', 'c2', Message('a1', 'test1'), MSG_ALGO))
def test_handler_decorator_not_called_before_start(): class TestComputation(MessagePassingComputation): def __init__(self): super().__init__("test") self.mock = MagicMock() @register("test_type") def on_msg(self, sender: str, msg: Message, t: float): self.mock(sender, msg, t) c = TestComputation() msg = Message("test_type") c.on_message("foo", msg, 0) # Computation is NOT started, handled must NOT be called c.mock.assert_not_called()
def test_msg_to_unknown_computation_fail_mode(self, http_comms): comm1, comm2 = http_comms comm1.discovery.register_computation('c2', 'a2', ('127.0.0.1', 10002)) comm2.discovery.register_computation('c1', 'a1', ('127.0.0.1', 10001)) def raise_unknown(*args): raise UnknownComputation('test') comm2.messaging.post_msg = MagicMock(side_effect=raise_unknown) with pytest.raises(UnknownComputation): comm1.send_msg('a1', 'a2', ComputationMessage('c1', 'c2', Message('a1', 't1'), MSG_ALGO), on_error='fail')
def test_handler_decorator(): class TestComputation(MessagePassingComputation): def __init__(self): super().__init__("test") self.mock = MagicMock() @register("test_type") def on_msg(self, sender: str, msg: Message, t: float): self.mock(sender, msg, t) # Computation must be started for messages to be handled c = TestComputation() c.start() msg = Message("test_type") c.on_message("foo", msg, 0) c.mock.assert_called_once_with("foo", msg, 0)
def test_message(): msg = Message('msg_type', 'foo') assert msg.type == 'msg_type' assert msg.content == 'foo'
def _on_subscribe_msg(self, var_name, _, t): self.subscribers.add(var_name) self._msg_sender.post_msg( self.name, var_name, Message('VARIABLE_VALUE', self._external_var.value))
def unsubscribe(self, variable): self._msg_sender.post_msg(self.name, variable.name, Message("SUBSCRIBE", None))
def _fire(self): for s in self.subscribers: self._msg_sender.post_msg( self.name, s, Message('VARIABLE_VALUE', self._external_var.value))
def test_message_serialization(): msg = Message("msg_type") r = simple_repr(msg) obtained = from_repr(r) assert msg == obtained
def test_message(): msg = Message("msg_type", "foo") assert msg.type == "msg_type" assert msg.content == "foo"
def on_start(self): # self.logger.info("Starting c1, send to c2") self.post_msg("c2", Message("foo"))
def _mgt_method(self, method: str, arg: Any): self.messaging.post_msg( ORCHESTRATOR_MGT, ORCHESTRATOR_MGT, Message(method, arg), msg_type=5)