def receive_message(port_name, slot=None, default=None): if port_name == 'muscle_settings_in': return Message(0.0, None, Settings(), Settings()) elif port_name == 'in': data = 'test {}'.format(slot) return Message(0.0, None, data, Settings()) assert False # pragma: no cover
def __receive_settings(self) -> bool: """Receives settings on muscle_settings_in. Returns: False iff the port is connnected and ClosePort was received. """ default_message = Message(0.0, None, Settings(), Settings()) message = self._communicator.receive_message('muscle_settings_in', None, default_message) if isinstance(message.data, ClosePort): return False if not isinstance(message.data, Settings): err_msg = ('"{}" received a message on' ' muscle_settings_in that is not a' ' Settings. It seems that your' ' simulation is miswired or the sending' ' instance is broken.'.format(self._instance_name())) self.__shutdown(err_msg) raise RuntimeError(err_msg) settings = cast(Settings, message.settings) for key, value in message.data.items(): settings[key] = value self._settings_manager.overlay = settings return True
def test_receive_message_default(communicator) -> None: communicator._peer_manager.is_connected.return_value = False default_msg = Message(3.0, 4.0, 'test', Settings()) msg = communicator.receive_message('not_connected', default=default_msg) assert msg.timestamp == 3.0 assert msg.next_timestamp == 4.0 assert msg.data == 'test' assert len(msg.settings) == 0
def test_reuse_instance_no_f_init_ports(instance): instance._communicator.receive_message.return_value = Message( 0.0, None, Settings(), Settings()) instance._communicator.list_ports.return_value = {} instance._communicator.settings_in_connected.return_value = False do_reuse = instance.reuse_instance() assert do_reuse is True do_reuse = instance.reuse_instance() assert do_reuse is False
def test_reuse_instance_receive_overlay(instance): instance._settings_manager.overlay = Settings() test_base_settings = Settings() test_base_settings['test1'] = 24 test_base_settings['test2'] = [1.3, 2.0] test_overlay = Settings() test_overlay['test2'] = 'abc' recv = instance._communicator.receive_message recv.return_value = Message(0.0, None, test_overlay, test_base_settings) instance.reuse_instance() assert instance._communicator.receive_message.called_with( 'muscle_settings_in') assert len(instance._settings_manager.overlay) == 2 assert instance._settings_manager.overlay['test1'] == 24 assert instance._settings_manager.overlay['test2'] == 'abc'
def instance(sys_argv_instance): with patch('libmuscle.instance.MMPClient') as mmp_client, \ patch('libmuscle.instance.Communicator') as comm_type: communicator = MagicMock() settings = Settings() settings['test1'] = 12 msg = Message(0.0, 1.0, 'message', settings) communicator.receive_message.return_value = msg comm_type.return_value = communicator mmp_client_object = MagicMock() mmp_client_object.request_peers.return_value = (None, None, None) mmp_client.return_value = mmp_client_object instance = Instance({ Operator.F_INIT: ['in', 'not_connected'], Operator.O_F: ['out']}) instance._f_init_cache = dict() instance._f_init_cache[('in', None)] = msg yield instance
def send(self, port_name: str, message: Message, slot: Optional[int] = None) -> None: """Send a message to the outside world. Sending is non-blocking, a copy of the message will be made and stored until the receiver is ready to receive it. Args: port_name: The port on which this message is to be sent. message: The message to be sent. slot: The slot to send the message on, if any. """ self.__check_port(port_name) if message.settings is None: message = copy(message) message.settings = self._settings_manager.overlay self._communicator.send_message(port_name, message, slot)
def receive_message(port_name, slot=None, default=None): if port_name == 'muscle_settings_in': return Message(0.0, None, Settings(), Settings()) elif port_name == 'in': return Message(0.0, None, ClosePort(), Settings()) assert False # pragma: no cover
def message2() -> Message: return Message(0.0, None, {'test': 17}, Settings())
def message() -> Message: return Message(0.0, None, b'test', Settings())