Ejemplo n.º 1
0
 def setup_class(cls):
     """Set up the test class."""
     cls.connection1 = _make_oef_connection(
         FETCHAI_ADDRESS_ONE, oef_addr="127.0.0.1", oef_port=10000,
     )
     cls.connection2 = _make_oef_connection(
         FETCHAI_ADDRESS_TWO, 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()
Ejemplo n.º 2
0
 def setup_class(cls):
     """Set the test up."""
     cls.connection = _make_oef_connection(
         FETCHAI_ADDRESS_ONE, oef_addr="127.0.0.1", oef_port=10000,
     )
     cls.multiplexer = Multiplexer([cls.connection])
     cls.multiplexer.connect()
Ejemplo n.º 3
0
 def test_connection(self):
     """Test that an OEF connection can be established to the OEF."""
     connection = _make_oef_connection(
         FETCHAI_ADDRESS_ONE, oef_addr="127.0.0.1", oef_port=10000,
     )
     multiplexer = Multiplexer([connection])
     multiplexer.connect()
     multiplexer.disconnect()
Ejemplo n.º 4
0
 def setup_class(cls):
     """Set the test up."""
     cls.connection = _make_oef_connection(
         FETCHAI_ADDRESS_ONE, oef_addr="127.0.0.1", oef_port=10000,
     )
     cls.multiplexer = Multiplexer([cls.connection])
     cls.multiplexer.connect()
     cls.oef_search_dialogues = OefSearchDialogues("agent_address")
Ejemplo n.º 5
0
    async def test_send_oef_message(self, pytestconfig, caplog):
        """Test the send oef message."""
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            oef_connection = _make_oef_connection(
                address=FETCHAI_ADDRESS_ONE,
                oef_addr="127.0.0.1",
                oef_port=10000,
            )
            await oef_connection.connect()
            oef_search_dialogues = OefSearchDialogues(FETCHAI_ADDRESS_ONE)
            msg = OefSearchMessage(
                performative=OefSearchMessage.Performative.OEF_ERROR,
                dialogue_reference=oef_search_dialogues.
                new_self_initiated_dialogue_reference(),
                oef_error_operation=OefSearchMessage.OefErrorOperation.
                SEARCH_SERVICES,
            )
            msg.to = str(oef_connection.connection_id)
            msg.sender = FETCHAI_ADDRESS_ONE
            envelope = Envelope(
                to=msg.to,
                sender=msg.sender,
                message=msg,
            )
            with caplog.at_level(logging.DEBUG,
                                 "aea.packages.fetchai.connections.oef"):
                await oef_connection.send(envelope)
                assert "Could not create dialogue for message=" in caplog.text

            data_model = DataModel("foobar",
                                   attributes=[Attribute("foo", str, True)])
            query = Query(
                constraints=[Constraint("foo", ConstraintType("==", "bar"))],
                model=data_model,
            )

            msg, sending_dialogue = oef_search_dialogues.create(
                counterparty=str(oef_connection.connection_id),
                performative=OefSearchMessage.Performative.SEARCH_SERVICES,
                query=query,
            )
            envelope = Envelope(
                to=msg.to,
                sender=msg.sender,
                message=msg,
            )
            await oef_connection.send(envelope)
            envelope = await oef_connection.receive()
            search_result = envelope.message
            response_dialogue = oef_search_dialogues.update(search_result)
            assert (search_result.performative ==
                    OefSearchMessage.Performative.SEARCH_RESULT)
            assert sending_dialogue == response_dialogue
            await asyncio.sleep(2.0)
            await oef_connection.disconnect()
Ejemplo n.º 6
0
 def setup(self):
     """Set the test up."""
     self.connection = _make_oef_connection(
         FETCHAI_ADDRESS_ONE,
         oef_addr="127.0.0.1",
         oef_port=10000,
     )
     self.multiplexer = Multiplexer([self.connection])
     self.multiplexer.connect()
     self.oef_search_dialogues = OefSearchDialogues(FETCHAI_ADDRESS_ONE)
Ejemplo n.º 7
0
 def setup_class(cls):
     """Set the test up."""
     cls.connection = _make_oef_connection(
         FETCHAI_ADDRESS_ONE,
         oef_addr="127.0.0.1",
         oef_port=10000,
     )
     cls.multiplexer = Multiplexer([cls.connection],
                                   protocols=[FipaMessage, DefaultMessage])
     cls.multiplexer.connect()
Ejemplo n.º 8
0
    async def test_send_oef_message(self, pytestconfig):
        """Test the send oef message."""
        oef_connection = _make_oef_connection(
            address=FETCHAI_ADDRESS_ONE, oef_addr="127.0.0.1", oef_port=10000,
        )
        oef_connection.loop = asyncio.get_event_loop()
        await oef_connection.connect()
        oef_search_dialogues = OefSearchDialogues("agent_address")
        msg = OefSearchMessage(
            performative=OefSearchMessage.Performative.OEF_ERROR,
            dialogue_reference=oef_search_dialogues.new_self_initiated_dialogue_reference(),
            oef_error_operation=OefSearchMessage.OefErrorOperation.SEARCH_SERVICES,
        )
        msg.counterparty = DEFAULT_OEF
        sending_dialogue = oef_search_dialogues.update(msg)
        envelope = Envelope(
            to=DEFAULT_OEF,
            sender=FETCHAI_ADDRESS_ONE,
            protocol_id=OefSearchMessage.protocol_id,
            message=msg,
        )
        with pytest.raises(ValueError):
            await oef_connection.send(envelope)

        data_model = DataModel("foobar", attributes=[Attribute("foo", str, True)])
        query = Query(
            constraints=[Constraint("foo", ConstraintType("==", "bar"))],
            model=data_model,
        )

        msg = OefSearchMessage(
            performative=OefSearchMessage.Performative.SEARCH_SERVICES,
            dialogue_reference=oef_search_dialogues.new_self_initiated_dialogue_reference(),
            query=query,
        )
        msg.counterparty = DEFAULT_OEF
        sending_dialogue = oef_search_dialogues.update(msg)
        envelope = Envelope(
            to=DEFAULT_OEF,
            sender=FETCHAI_ADDRESS_ONE,
            protocol_id=OefSearchMessage.protocol_id,
            message=msg,
        )
        await oef_connection.send(envelope)
        envelope = await oef_connection.receive()
        search_result = envelope.message
        response_dialogue = oef_search_dialogues.update(search_result)
        assert search_result.performative == OefSearchMessage.Performative.SEARCH_RESULT
        assert sending_dialogue == response_dialogue
        await asyncio.sleep(2.0)
        await oef_connection.disconnect()
Ejemplo n.º 9
0
    async def test_connecting_twice_is_ok(self, pytestconfig):
        """Test that calling 'connect' twice works as expected."""
        oef_connection = _make_oef_connection(
            address=FETCHAI_ADDRESS_ONE, 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()
Ejemplo n.º 10
0
    async def test_exception_during_receive(self, pytestconfig):
        """Test the case when there is an exception during a receive request."""
        oef_connection = _make_oef_connection(
            address=FETCHAI_ADDRESS_ONE, 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.channel._in_queue, "get", side_effect=Exception
        ):
            result = await oef_connection.receive()
            assert result is None

        await oef_connection.disconnect()
Ejemplo n.º 11
0
async def test_cannot_connect_to_oef(caplog):
    """Test the case when we can't connect to the OEF."""
    oef_connection = _make_oef_connection(
        address=FETCHAI_ADDRESS_ONE,
        oef_addr="127.0.0.1",
        oef_port=61234,  # use addr instead of hostname to avoid name resolution
    )

    with caplog.at_level(logging.DEBUG, logger="aea.packages.fetchai.connections.oef"):

        task = asyncio.ensure_future(
            oef_connection.connect(), loop=asyncio.get_event_loop()
        )
        await asyncio.sleep(3.0)
        assert "Cannot connect to OEFChannel. Retrying in 5 seconds..." in caplog.text

        await cancel_and_wait(task)
        await oef_connection.disconnect()
Ejemplo n.º 12
0
async def test_cannot_connect_to_oef():
    """Test the case when we can't connect to the OEF."""
    oef_connection = _make_oef_connection(
        address=FETCHAI_ADDRESS_ONE,
        oef_addr="127.0.0.1",
        oef_port=61234,  # use addr instead of hostname to avoid name resolution
    )

    with mock.patch.object(oef_connection.logger, "warning") as mock_logger:

        task = asyncio.ensure_future(oef_connection.connect(),
                                     loop=asyncio.get_event_loop())
        await asyncio.sleep(3.0)
        mock_logger.assert_any_call(
            "Cannot connect to OEFChannel. Retrying in 5 seconds...")

        await cancel_and_wait(task)
        await oef_connection.disconnect()
Ejemplo n.º 13
0
    async def test_cancelled_receive(self, pytestconfig, caplog):
        """Test the case when a receive request is cancelled."""
        oef_connection = _make_oef_connection(
            address=FETCHAI_ADDRESS_ONE, oef_addr="127.0.0.1", oef_port=10000,
        )
        oef_connection.loop = asyncio.get_event_loop()
        await oef_connection.connect()

        with caplog.at_level(logging.DEBUG, "aea.packages.fetchai.connections.oef"):

            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()

            assert "Receive cancelled." in caplog.text
Ejemplo n.º 14
0
async def test_methods_with_logging_only():
    """Test the case when we can't connect to the OEF."""
    oef_connection = _make_oef_connection(
        address=FETCHAI_ADDRESS_ONE,
        oef_addr="127.0.0.1",
        oef_port=61234,  # use addr instead of hostname to avoid name resolution
    )

    with patch.object(oef_connection.channel,
                      "_oef_agent_connect",
                      return_value=True):
        await oef_connection.connect()

    oef_connection.channel.on_cfp(msg_id=1,
                                  dialogue_id=1,
                                  origin="some",
                                  target=1,
                                  query=b"")
    oef_connection.channel.on_decline(msg_id=1,
                                      dialogue_id=1,
                                      origin="some",
                                      target=1)
    oef_connection.channel.on_propose(msg_id=1,
                                      dialogue_id=1,
                                      origin="some",
                                      target=1,
                                      proposals=b"")
    oef_connection.channel.on_accept(msg_id=1,
                                     dialogue_id=1,
                                     origin="some",
                                     target=1)

    try:
        await oef_connection.disconnect()
    except Exception:  # nosec
        pass
Ejemplo n.º 15
0
        def setup_class(cls):
            """
            Set the test up.

            Steps:
            - Register a service
            - Check that the registration worked.
            """
            cls.connection = _make_oef_connection(
                FETCHAI_ADDRESS_ONE,
                oef_addr="127.0.0.1",
                oef_port=10000,
            )
            cls.multiplexer = Multiplexer([cls.connection])
            cls.multiplexer.connect()
            cls.oef_search_dialogues = OefSearchDialogues(FETCHAI_ADDRESS_ONE)

            cls.foo_datamodel = DataModel(
                "foo", [Attribute("bar", int, True, "A bar attribute.")])
            cls.desc = Description({"bar": 1}, data_model=cls.foo_datamodel)
            oef_search_registration, _ = cls.oef_search_dialogues.create(
                counterparty=str(cls.connection.connection_id),
                performative=OefSearchMessage.Performative.REGISTER_SERVICE,
                service_description=cls.desc,
            )

            cls.multiplexer.put(
                Envelope(
                    to=oef_search_registration.to,
                    sender=oef_search_registration.sender,
                    protocol_id=oef_search_registration.protocol_id,
                    message=oef_search_registration,
                ))

            time.sleep(1.0)

            (
                oef_search_request,
                sending_dialogue_2,
            ) = cls.oef_search_dialogues.create(
                counterparty=str(cls.connection.connection_id),
                performative=OefSearchMessage.Performative.SEARCH_SERVICES,
                query=Query(
                    [Constraint("bar", ConstraintType("==", 1))],
                    model=cls.foo_datamodel,
                ),
            )
            cls.multiplexer.put(
                Envelope(
                    to=oef_search_request.to,
                    sender=oef_search_request.sender,
                    protocol_id=oef_search_request.protocol_id,
                    message=oef_search_request,
                ))
            envelope = cls.multiplexer.get(block=True, timeout=5.0)
            oef_search_response = envelope.message
            oef_search_dialogue = cls.oef_search_dialogues.update(
                oef_search_response)
            assert (oef_search_response.performative ==
                    OefSearchMessage.Performative.SEARCH_RESULT)
            assert oef_search_dialogue == sending_dialogue_2
            assert oef_search_response.agents == (
                FETCHAI_ADDRESS_ONE,
            ), "search_result.agents != [FETCHAI_ADDRESS_ONE] FAILED in test_oef/test_communication.py"
Ejemplo n.º 16
0
    async def test_send_oef_message(self, pytestconfig, caplog):
        """Test the send oef message."""
        oef_connection = _make_oef_connection(
            address=FETCHAI_ADDRESS_ONE,
            oef_addr="127.0.0.1",
            oef_port=10000,
        )
        oef_connection.loop = asyncio.get_event_loop()
        await oef_connection.connect()
        oef_search_dialogues = OefSearchDialogues("agent_address")
        msg = OefSearchMessage(
            performative=OefSearchMessage.Performative.OEF_ERROR,
            dialogue_reference=oef_search_dialogues.
            new_self_initiated_dialogue_reference(),
            oef_error_operation=OefSearchMessage.OefErrorOperation.
            SEARCH_SERVICES,
        )
        msg.counterparty = str(oef_connection.connection_id)
        sending_dialogue = oef_search_dialogues.update(msg)
        envelope = Envelope(
            to=str(oef_connection.connection_id),
            sender=FETCHAI_ADDRESS_ONE,
            protocol_id=OefSearchMessage.protocol_id,
            message=msg,
        )
        with caplog.at_level(logging.DEBUG,
                             "aea.packages.fetchai.connections.oef"):
            await oef_connection.send(envelope)
            assert "Could not create dialogue for message=" in caplog.text

        data_model = DataModel("foobar",
                               attributes=[Attribute("foo", str, True)])
        query = Query(
            constraints=[Constraint("foo", ConstraintType("==", "bar"))],
            model=data_model,
        )

        msg = OefSearchMessage(
            performative=OefSearchMessage.Performative.SEARCH_SERVICES,
            dialogue_reference=oef_search_dialogues.
            new_self_initiated_dialogue_reference(),
            query=query,
        )
        msg.counterparty = str(oef_connection.connection_id)
        sending_dialogue = oef_search_dialogues.update(msg)
        envelope = Envelope(
            to=str(oef_connection.connection_id),
            sender=FETCHAI_ADDRESS_ONE,
            protocol_id=OefSearchMessage.protocol_id,
            message=msg,
        )
        await oef_connection.send(envelope)
        envelope = await oef_connection.receive()
        search_result_original = envelope.message
        search_result = copy.copy(search_result_original)
        search_result.counterparty = search_result_original.sender
        search_result.is_incoming = True
        response_dialogue = oef_search_dialogues.update(search_result)
        assert search_result.performative == OefSearchMessage.Performative.SEARCH_RESULT
        assert sending_dialogue == response_dialogue
        await asyncio.sleep(2.0)
        await oef_connection.disconnect()