async def test_send_oef_message(network_node): """Test the send oef message.""" private_key_pem_path = os.path.join(CUR_PATH, "data", "priv.pem") wallet = Wallet({'default': private_key_pem_path}) public_key = wallet.public_keys['default'] oef_connection = OEFConnection(public_key=public_key, oef_addr="127.0.0.1", oef_port=10000) oef_connection.loop = asyncio.get_event_loop() await oef_connection.connect() msg = OEFMessage(oef_type=OEFMessage.Type.OEF_ERROR, id=0, operation=OEFMessage.OEFErrorOperation.SEARCH_AGENTS) msg_bytes = OEFSerializer().encode(msg) envelope = Envelope(to=DEFAULT_OEF, sender=public_key, protocol_id=OEFMessage.protocol_id, message=msg_bytes) with pytest.raises(ValueError): await oef_connection.send(envelope) data_model = DataModel("foobar", attributes=[]) query = Query(constraints=[], model=data_model) msg = OEFMessage(oef_type=OEFMessage.Type.SEARCH_AGENTS, id=0, query=query) msg_bytes = OEFSerializer().encode(msg) envelope = Envelope(to="recipient", sender=public_key, protocol_id=OEFMessage.protocol_id, message=msg_bytes) await oef_connection.send(envelope) await oef_connection.disconnect()
def setup_class(cls): """Set up the test class.""" cls.crypto1 = DefaultCrypto() cls.crypto2 = DefaultCrypto() cls.connection1 = OEFConnection(cls.crypto1.public_key, oef_addr="127.0.0.1", oef_port=10000) cls.connection2 = OEFConnection(cls.crypto2.public_key, oef_addr="127.0.0.1", oef_port=10000) cls.multiplexer1 = Multiplexer([cls.connection1]) cls.multiplexer2 = Multiplexer([cls.connection2]) cls.multiplexer1.connect() cls.multiplexer2.connect()
async def test_connecting_twice_is_ok(network_node): """Test that calling 'connect' twice works as expected.""" private_key_pem_path = os.path.join(CUR_PATH, "data", "priv.pem") wallet = Wallet({'default': private_key_pem_path}) oef_connection = OEFConnection(public_key=wallet.public_keys['default'], oef_addr="127.0.0.1", oef_port=10000) oef_connection.loop = asyncio.get_event_loop() assert not oef_connection.connection_status.is_connected await oef_connection.connect() assert oef_connection.connection_status.is_connected await oef_connection.connect() assert oef_connection.connection_status.is_connected await oef_connection.disconnect()
def setup_class(cls): """Set the test up.""" cls.crypto1 = DefaultCrypto() cls.connection = OEFConnection(cls.crypto1.public_key, oef_addr="127.0.0.1", oef_port=10000) cls.multiplexer = Multiplexer([cls.connection]) cls.multiplexer.connect()
async def test_exception_during_receive(network_node): """Test the case when there is an exception during a receive request.""" private_key_pem_path = os.path.join(CUR_PATH, "data", "priv.pem") wallet = Wallet({'default': private_key_pem_path}) oef_connection = OEFConnection(public_key=wallet.public_keys['default'], oef_addr="127.0.0.1", oef_port=10000) oef_connection.loop = asyncio.get_event_loop() await oef_connection.connect() with unittest.mock.patch.object(oef_connection.in_queue, "get", side_effect=Exception): result = await oef_connection.receive() assert result is None await oef_connection.disconnect()
def test_connection(self): """Test that an OEF connection can be established to the OEF.""" crypto = DefaultCrypto() connection = OEFConnection(crypto.public_key, oef_addr="127.0.0.1", oef_port=10000) multiplexer = Multiplexer([connection]) multiplexer.connect() multiplexer.disconnect()
def setup_class(cls): """ Set the test up. Steps: - Register a service - Check that the registration worked. """ cls.crypto1 = DefaultCrypto() cls.connection = OEFConnection(cls.crypto1.public_key, oef_addr="127.0.0.1", oef_port=10000) cls.multiplexer = Multiplexer([cls.connection]) cls.multiplexer.connect() cls.request_id = 1 cls.foo_datamodel = DataModel( "foo", [Attribute("bar", int, True, "A bar attribute.")]) cls.desc = Description({"bar": 1}, data_model=cls.foo_datamodel) msg = OEFMessage(oef_type=OEFMessage.Type.REGISTER_SERVICE, id=cls.request_id, service_description=cls.desc, service_id="") msg_bytes = OEFSerializer().encode(msg) cls.multiplexer.put( Envelope(to=DEFAULT_OEF, sender=cls.crypto1.public_key, protocol_id=OEFMessage.protocol_id, message=msg_bytes)) time.sleep(1.0) cls.request_id += 1 search_request = OEFMessage( oef_type=OEFMessage.Type.SEARCH_SERVICES, id=cls.request_id, query=Query([Constraint("bar", ConstraintType("==", 1))], model=cls.foo_datamodel)) cls.multiplexer.put( Envelope(to=DEFAULT_OEF, sender=cls.crypto1.public_key, protocol_id=OEFMessage.protocol_id, message=OEFSerializer().encode(search_request))) envelope = cls.multiplexer.get(block=True, timeout=5.0) search_result = OEFSerializer().decode(envelope.message) assert search_result.get("type") == OEFMessage.Type.SEARCH_RESULT assert search_result.get("id") == cls.request_id if search_result.get("agents") != [cls.crypto1.public_key]: logger.warning( 'search_result.get("agents") != [self.crypto1.public_key] FAILED in test_oef/test_communication.py' )
async def test_cannot_connect_to_oef(): """Test the case when we can't connect to the OEF.""" private_key_pem_path = os.path.join(CUR_PATH, "data", "priv.pem") wallet = Wallet({'default': private_key_pem_path}) oef_connection = OEFConnection(public_key=wallet.public_keys['default'], oef_addr="a_fake_address", oef_port=10000) oef_connection.loop = asyncio.get_event_loop() patch = unittest.mock.patch.object(aea.connections.oef.connection.logger, 'warning') mocked_logger_warning = patch.__enter__() async def try_to_connect(): await oef_connection.connect() task = asyncio.ensure_future(try_to_connect(), loop=asyncio.get_event_loop()) await asyncio.sleep(1.0) mocked_logger_warning.assert_called_with( "Cannot connect to OEFChannel. Retrying in 5 seconds...") task.cancel() await asyncio.sleep(1.0)
async def test_cancelled_receive(network_node): """Test the case when a receive request is cancelled.""" private_key_pem_path = os.path.join(CUR_PATH, "data", "priv.pem") wallet = Wallet({'default': private_key_pem_path}) oef_connection = OEFConnection(public_key=wallet.public_keys['default'], oef_addr="127.0.0.1", oef_port=10000) oef_connection.loop = asyncio.get_event_loop() await oef_connection.connect() patch = unittest.mock.patch.object(aea.connections.oef.connection.logger, 'debug') mocked_logger_debug = patch.__enter__() async def receive(): await oef_connection.receive() task = asyncio.ensure_future(receive(), loop=asyncio.get_event_loop()) await asyncio.sleep(0.1) task.cancel() await asyncio.sleep(0.1) await oef_connection.disconnect() mocked_logger_debug.assert_called_once_with("Receive cancelled.")
def test_oef_from_config(self): """Test the Connection from config File.""" con = OEFConnection.from_config( public_key="pk", connection_configuration=ConnectionConfig()) assert not con.connection_status.is_connected, "We are connected..."