async def test_cannot_connect_to_oef():
    """Test the case when we can't connect to the OEF."""
    address = FetchAICrypto().address
    oef_connection = OEFConnection(
        address=address,
        oef_addr="a_fake_address",
        oef_port=10000,
        connection_id=PublicId("fetchai", "oef", "0.1.0"),
    )
    oef_connection.loop = asyncio.get_event_loop()

    patch = unittest.mock.patch.object(
        packages.fetchai.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."""
    address = FetchAICrypto().address
    oef_connection = OEFConnection(
        address=address,
        oef_addr="127.0.0.1",
        oef_port=10000,
        connection_id=PublicId("fetchai", "oef", "0.1.0"),
    )
    oef_connection.loop = asyncio.get_event_loop()
    await oef_connection.connect()

    patch = unittest.mock.patch.object(
        packages.fetchai.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.")
Example #3
0
def _make_oef_connection(address: Address, oef_addr: str, oef_port: int):
    configuration = ConnectionConfig(
        addr=oef_addr, port=oef_port, connection_id=OEFConnection.connection_id
    )
    oef_connection = OEFConnection(
        configuration=configuration, identity=Identity("name", address),
    )
    oef_connection._default_logger_name = "aea.packages.fetchai.connections.oef"
    return oef_connection
Example #4
0
def _make_oef_connection(address: Address, oef_addr: str, oef_port: int):
    oef_connection = OEFConnection(
        oef_addr,
        oef_port,
        address=address,
        connection_id=PublicId("fetchai", "oef", "0.1.0"),
    )
    return oef_connection
async def test_connecting_twice_is_ok(network_node):
    """Test that calling 'connect' twice works as expected."""
    address = FetchAICrypto().address
    oef_connection = OEFConnection(
        address=address,
        oef_addr="127.0.0.1",
        oef_port=10000,
        connection_id=PublicId("fetchai", "oef", "0.1.0"),
    )
    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()
async def test_exception_during_receive(network_node):
    """Test the case when there is an exception during a receive request."""
    address = FetchAICrypto().address
    oef_connection = OEFConnection(
        address=address,
        oef_addr="127.0.0.1",
        oef_port=10000,
        connection_id=PublicId("fetchai", "oef", "0.1.0"),
    )
    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()
async def test_send_oef_message(network_node):
    """Test the send oef message."""
    address = FetchAICrypto().address
    oef_connection = OEFConnection(
        address=address,
        oef_addr="127.0.0.1",
        oef_port=10000,
        connection_id=PublicId("fetchai", "oef", "0.1.0"),
    )
    oef_connection.loop = asyncio.get_event_loop()
    await oef_connection.connect()
    msg = OEFMessage(
        type=OEFMessage.Type.OEF_ERROR,
        id=0,
        operation=OEFMessage.OEFErrorOperation.SEARCH_AGENTS,
    )
    msg_bytes = OEFSerializer().encode(msg)
    envelope = Envelope(
        to=DEFAULT_OEF,
        sender=address,
        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(type=OEFMessage.Type.SEARCH_AGENTS, id=0, query=query)
    msg_bytes = OEFSerializer().encode(msg)
    envelope = Envelope(
        to=DEFAULT_OEF,
        sender=address,
        protocol_id=OEFMessage.protocol_id,
        message=msg_bytes,
    )
    await oef_connection.send(envelope)
    search_result = await oef_connection.receive()
    assert isinstance(search_result, Envelope)
    await asyncio.sleep(2.0)
    await oef_connection.disconnect()
 def setup_class(cls):
     """Set the test up."""
     cls.crypto1 = FetchAICrypto()
     cls.connection = OEFConnection(
         cls.crypto1.address,
         oef_addr="127.0.0.1",
         oef_port=10000,
         connection_id=PublicId("fetchai", "oef", "0.1.0"),
     )
     cls.multiplexer = Multiplexer([cls.connection])
     cls.multiplexer.connect()
 def setup_class(cls):
     """Set up the test class."""
     cls.crypto1 = FetchAICrypto()
     cls.crypto2 = FetchAICrypto()
     cls.connection1 = OEFConnection(
         cls.crypto1.address,
         oef_addr="127.0.0.1",
         oef_port=10000,
         connection_id=PublicId("fetchai", "oef", "0.1.0"),
     )
     cls.connection2 = OEFConnection(
         cls.crypto2.address,
         oef_addr="127.0.0.1",
         oef_port=10000,
         connection_id=PublicId("fetchai", "oef", "0.1.0"),
     )
     cls.multiplexer1 = Multiplexer([cls.connection1])
     cls.multiplexer2 = Multiplexer([cls.connection2])
     cls.multiplexer1.connect()
     cls.multiplexer2.connect()
 def test_connection(self):
     """Test that an OEF connection can be established to the OEF."""
     crypto = FetchAICrypto()
     connection = OEFConnection(
         crypto.address,
         oef_addr="127.0.0.1",
         oef_port=10000,
         connection_id=PublicId("fetchai", "oef", "0.1.0"),
     )
     multiplexer = Multiplexer([connection])
     multiplexer.connect()
     multiplexer.disconnect()
Example #11
0
def run():
    # Create a private key
    _create_fetchai_private_key()

    # Set up the wallet, identity, oef connection, ledger and (empty) resources
    wallet = Wallet({FETCHAI: FETCHAI_PRIVATE_KEY_FILE})
    identity = Identity("my_aea", address=wallet.addresses.get(FETCHAI))
    oef_connection = OEFConnection(address=identity.address,
                                   oef_addr=HOST,
                                   oef_port=PORT)
    ledger_apis = LedgerApis({}, FETCHAI)
    resources = Resources()

    # create the AEA
    my_aea = AEA(
        identity,
        [oef_connection],
        wallet,
        ledger_apis,
        resources,  # stub_connection,
    )

    # Add the default protocol (which is part of the AEA distribution)
    default_protocol = Protocol.from_dir(
        os.path.join(AEA_DIR, "protocols", "default"))
    resources.add_protocol(default_protocol)

    # Add the oef protocol (which is a package)
    oef_protocol = Protocol.from_dir(
        os.path.join(
            os.getcwd(),
            "packages",
            "fetchai",
            "protocols",
            "oef_search",
        ))
    resources.add_protocol(oef_protocol)

    # Add the fipa protocol (which is a package)
    fipa_protocol = Protocol.from_dir(
        os.path.join(
            os.getcwd(),
            "packages",
            "fetchai",
            "protocols",
            "fipa",
        ))
    resources.add_protocol(fipa_protocol)

    # Add the error and weather_station skills
    error_skill = Skill.from_dir(os.path.join(AEA_DIR, "skills", "error"),
                                 my_aea.context)
    weather_skill = Skill.from_dir(
        os.path.join(ROOT_DIR, "packages", "fetchai", "skills",
                     "weather_client"),
        my_aea.context,
    )

    strategy = cast(Strategy, weather_skill.models.get("strategy"))
    strategy.is_ledger_tx = False
    strategy.max_buyer_tx_fee = 100
    strategy.max_row_price = 40

    for skill in [error_skill, weather_skill]:
        resources.add_skill(skill)

    # Set the AEA running in a different thread
    try:
        logger.info("STARTING AEA NOW!")
        t = Thread(target=my_aea.start)
        t.start()

        # Let it run long enough to interact with the weather station
        time.sleep(25)
    finally:
        # Shut down the AEA
        logger.info("STOPPING AEA NOW!")
        my_aea.stop()
        t.join()
 def test_oef_from_config(self):
     """Test the Connection from config File."""
     con = OEFConnection.from_config(
         address="pk", connection_configuration=ConnectionConfig()
     )
     assert not con.connection_status.is_connected, "We are connected..."
        def setup_class(cls):
            """
            Set the test up.

            Steps:
            - Register a service
            - Check that the registration worked.
            """
            cls.crypto1 = FetchAICrypto()
            cls.connection = OEFConnection(
                cls.crypto1.address,
                oef_addr="127.0.0.1",
                oef_port=10000,
                connection_id=PublicId("fetchai", "oef", "0.1.0"),
            )
            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(
                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.address,
                    protocol_id=OEFMessage.protocol_id,
                    message=msg_bytes,
                )
            )

            time.sleep(1.0)

            cls.request_id += 1
            search_request = OEFMessage(
                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.address,
                    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.address]:
                logger.warning(
                    'search_result.get("agents") != [self.crypto1.address] FAILED in test_oef/test_communication.py'
                )
def run():
    # Create a private key
    create_private_key(FetchAICrypto.identifier)

    # Set up the wallet, identity and (empty) resources
    wallet = Wallet({FetchAICrypto.identifier: FETCHAI_PRIVATE_KEY_FILE})
    identity = Identity("my_aea",
                        address=wallet.addresses.get(FetchAICrypto.identifier))
    resources = Resources()

    # specify the default routing for some protocols
    default_routing = {
        PublicId.from_str("fetchai/ledger_api:0.1.0"):
        LedgerConnection.connection_id
    }
    default_connection = OEFConnection.connection_id

    # create the AEA
    my_aea = AEA(
        identity,
        wallet,
        resources,
        default_connection=default_connection,
        default_routing=default_routing,
    )

    # Add the default protocol (which is part of the AEA distribution)
    default_protocol = Protocol.from_dir(
        os.path.join(AEA_DIR, "protocols", "default"))
    resources.add_protocol(default_protocol)

    # Add the signing protocol (which is part of the AEA distribution)
    signing_protocol = Protocol.from_dir(
        os.path.join(AEA_DIR, "protocols", "signing"))
    resources.add_protocol(signing_protocol)

    # Add the ledger_api protocol
    ledger_api_protocol = Protocol.from_dir(
        os.path.join(
            os.getcwd(),
            "packages",
            "fetchai",
            "protocols",
            "ledger_api",
        ))
    resources.add_protocol(ledger_api_protocol)

    # Add the oef_search protocol
    oef_protocol = Protocol.from_dir(
        os.path.join(
            os.getcwd(),
            "packages",
            "fetchai",
            "protocols",
            "oef_search",
        ))
    resources.add_protocol(oef_protocol)

    # Add the fipa protocol
    fipa_protocol = Protocol.from_dir(
        os.path.join(
            os.getcwd(),
            "packages",
            "fetchai",
            "protocols",
            "fipa",
        ))
    resources.add_protocol(fipa_protocol)

    # Add the LedgerAPI connection
    configuration = ConnectionConfig(
        connection_id=LedgerConnection.connection_id)
    ledger_api_connection = LedgerConnection(configuration=configuration,
                                             identity=identity)
    resources.add_connection(ledger_api_connection)

    # Add the OEF connection
    configuration = ConnectionConfig(addr=HOST,
                                     port=PORT,
                                     connection_id=OEFConnection.connection_id)
    oef_connection = OEFConnection(configuration=configuration,
                                   identity=identity)
    resources.add_connection(oef_connection)

    # Add the error and weather_client skills
    error_skill = Skill.from_dir(os.path.join(AEA_DIR, "skills", "error"),
                                 agent_context=my_aea.context)
    weather_skill = Skill.from_dir(
        os.path.join(ROOT_DIR, "packages", "fetchai", "skills",
                     "weather_client"),
        agent_context=my_aea.context,
    )

    strategy = cast(Strategy, weather_skill.models.get("strategy"))
    strategy._is_ledger_tx = False

    for skill in [error_skill, weather_skill]:
        resources.add_skill(skill)

    # Run the AEA
    try:
        logger.info("STARTING AEA NOW!")
        my_aea.start()
    except KeyboardInterrupt:
        logger.info("STOPPING AEA NOW!")
        my_aea.stop()
Example #15
0
    def test_generated_protocol_end_to_end(self):
        """Test that a generated protocol could be used in exchanging messages between two agents."""
        # AEA components
        ledger_apis = LedgerApis({}, FETCHAI)

        wallet_1 = Wallet({FETCHAI: FETCHAI_PRIVATE_KEY_FILE})
        wallet_2 = Wallet({FETCHAI: FETCHAI_PRIVATE_KEY_FILE})

        identity_1 = Identity(
            name="my_aea_1",
            address=wallet_1.addresses.get(FETCHAI),
            default_address_key=FETCHAI,
        )
        identity_2 = Identity(
            name="my_aea_2",
            address=wallet_2.addresses.get(FETCHAI),
            default_address_key=FETCHAI,
        )

        oef_connection_1 = OEFConnection(
            address=identity_1.address, oef_addr=HOST, oef_port=PORT
        )
        oef_connection_2 = OEFConnection(
            address=identity_2.address, oef_addr=HOST, oef_port=PORT
        )

        resources_1 = Resources()
        resources_2 = Resources()

        # add generated protocols to resources
        generated_protocol_configuration = ProtocolConfig.from_json(
            yaml.safe_load(
                open(
                    os.path.join(
                        self.cwd,
                        "tests",
                        "data",
                        "generator",
                        "two_party_negotiation",
                        "protocol.yaml",
                    )
                )
            )
        )
        generated_protocol = Protocol(
            TwoPartyNegotiationMessage.protocol_id,
            TwoPartyNegotiationSerializer(),
            generated_protocol_configuration,
        )
        resources_1.protocol_registry.register(
            TwoPartyNegotiationMessage.protocol_id, generated_protocol
        )
        resources_2.protocol_registry.register(
            TwoPartyNegotiationMessage.protocol_id, generated_protocol
        )

        # create AEAs
        aea_1 = AEA(identity_1, [oef_connection_1], wallet_1, ledger_apis, resources_1)
        aea_2 = AEA(identity_2, [oef_connection_2], wallet_2, ledger_apis, resources_2)

        inform_number = tuple((1370, 1991, 1, 4, 17, 6))
        # message 1
        message = TwoPartyNegotiationMessage(
            message_id=1,
            dialogue_reference=(str(0), ""),
            target=0,
            performative=TwoPartyNegotiationMessage.Performative.INFORM,
            inform_number=inform_number,
        )
        encoded_message_in_bytes = TwoPartyNegotiationSerializer().encode(message)
        envelope = Envelope(
            to=identity_2.address,
            sender=identity_1.address,
            protocol_id=TwoPartyNegotiationMessage.protocol_id,
            message=encoded_message_in_bytes,
        )
        # message 2
        reply_message = {1: "number one", 2: "number two", 7: "number seven"}
        message_2 = TwoPartyNegotiationMessage(
            message_id=2,
            dialogue_reference=(str(0), ""),
            target=1,
            performative=TwoPartyNegotiationMessage.Performative.INFORM_REPLY,
            reply_message=reply_message,
        )
        encoded_message_2_in_bytes = TwoPartyNegotiationSerializer().encode(message_2)

        # add handlers to AEA resources
        agent_1_handler = Agent1Handler(
            skill_context=SkillContext(aea_1.context), name="fake_skill"
        )
        resources_1.handler_registry.register(
            (
                PublicId.from_str("fetchai/fake_skill:0.1.0"),
                TwoPartyNegotiationMessage.protocol_id,
            ),
            agent_1_handler,
        )
        agent_2_handler = Agent2Handler(
            encoded_messsage=encoded_message_2_in_bytes,
            skill_context=SkillContext(aea_2.context),
            name="fake_skill",
        )
        resources_2.handler_registry.register(
            (
                PublicId.from_str("fetchai/fake_skill:0.1.0"),
                TwoPartyNegotiationMessage.protocol_id,
            ),
            agent_2_handler,
        )

        # add error skill to AEAs
        error_skill_1 = Skill.from_dir(
            os.path.join(AEA_DIR, "skills", "error"), aea_1.context
        )
        resources_1.add_skill(error_skill_1)

        error_skill_2 = Skill.from_dir(
            os.path.join(AEA_DIR, "skills", "error"), aea_2.context
        )
        resources_2.add_skill(error_skill_2)

        # Start threads
        t_1 = Thread(target=aea_1.start)
        t_2 = Thread(target=aea_2.start)
        try:
            t_1.start()
            t_2.start()
            time.sleep(1.0)
            aea_1.outbox.put(envelope)
            time.sleep(5.0)
            assert (
                agent_2_handler.handled_message.message_id == message.message_id
            ), "Message from Agent 1 to 2: message ids do not match"
            assert (
                agent_2_handler.handled_message.dialogue_reference
                == message.dialogue_reference
            ), "Message from Agent 1 to 2: dialogue references do not match"
            assert (
                agent_2_handler.handled_message.dialogue_reference[0]
                == message.dialogue_reference[0]
            ), "Message from Agent 1 to 2: dialogue reference[0]s do not match"
            assert (
                agent_2_handler.handled_message.dialogue_reference[1]
                == message.dialogue_reference[1]
            ), "Message from Agent 1 to 2: dialogue reference[1]s do not match"
            assert (
                agent_2_handler.handled_message.target == message.target
            ), "Message from Agent 1 to 2: targets do not match"
            assert (
                agent_2_handler.handled_message.performative == message.performative
            ), "Message from Agent 1 to 2: performatives do not match"
            assert (
                agent_2_handler.handled_message.inform_number == message.inform_number
            ), "Message from Agent 1 to 2: inform_numbers do not match"

            assert (
                agent_1_handler.handled_message.message_id == message_2.message_id
            ), "Message from Agent 1 to 2: dialogue references do not match"
            assert (
                agent_1_handler.handled_message.dialogue_reference
                == message_2.dialogue_reference
            ), "Message from Agent 2 to 1: dialogue references do not match"
            assert (
                agent_1_handler.handled_message.dialogue_reference[0]
                == message_2.dialogue_reference[0]
            ), "Message from Agent 2 to 1: dialogue reference[0]s do not match"
            assert (
                agent_1_handler.handled_message.dialogue_reference[1]
                == message_2.dialogue_reference[1]
            ), "Message from Agent 2 to 1: dialogue reference[1]s do not match"
            assert (
                agent_1_handler.handled_message.target == message_2.target
            ), "Message from Agent 2 to 1: targets do not match"
            assert (
                agent_1_handler.handled_message.performative == message_2.performative
            ), "Message from Agent 2 to 1: performatives do not match"
            assert (
                agent_1_handler.handled_message.reply_message == message_2.reply_message
            ), "Message from Agent 1 to 2: reply_messages do not match"
            time.sleep(2.0)
        finally:
            aea_1.stop()
            aea_2.stop()
            t_1.join()
            t_2.join()