async def test_p2p_receive():
    """Test receive from p2p connection."""
    address = "127.0.0.1"
    port = 8000
    m_fet_key = "6d56fd47e98465824aa85dfe620ad3dbf092b772abc6c6a182e458b5c56ad13b"
    ent = entity.Entity.from_hex(m_fet_key)
    p2p_connection = PeerToPeerClientConnection(
        address=ent.public_key_hex,
        provider_addr=address,
        provider_port=port,
        connection_id=P2P_CLIENT_CONNECTION_PUBLIC_ID,
    )
    p2p_connection.loop = asyncio.get_event_loop()

    fake_get_messages_empty = MagicMock(return_value=[])

    s_msg = {
        "FROM": {
            "NODE_ADDRESS": "node_address",
            "SENDER_ADDRESS": "sender_address"
        },
        "TO": {
            "NODE_ADDRESS": "node_address",
            "RECEIVER_ADDRESS": "receiver_address"
        },
        "PROTOCOL": "author/protocol_name:0.1.0",
        "CONTEXT": "context",
        "PAYLOAD": "payload",
    }
    messages = [s_msg]

    with mock.patch.object(fetch.p2p.api.http_calls.HTTPCalls,
                           "get_messages",
                           return_value=[]):
        with mock.patch.object(
                fetch.p2p.api.http_calls.HTTPCalls,
                "register",
                return_value={"status": "OK"},
        ):
            await p2p_connection.connect()
            assert p2p_connection.connection_status.is_connected is True

    with mock.patch.object(fetch.p2p.api.http_calls.HTTPCalls,
                           "get_messages",
                           return_value=messages) as mock_receive:
        p2p_connection.channel._httpCall.get_messages = mock_receive
        await asyncio.sleep(2.0)
        envelope = await p2p_connection.receive()
        assert envelope is not None

    with mock.patch.object(fetch.p2p.api.http_calls.HTTPCalls,
                           "unregister",
                           return_value={"status": "OK"}):
        p2p_connection.channel._httpCall.get_messages = fake_get_messages_empty
        await p2p_connection.disconnect()
        assert p2p_connection.connection_status.is_connected is False
async def test_p2p_send():
    """Test the send functionality of the p2p connection."""
    address = "127.0.0.1"
    port = 8000
    m_fet_key = "6d56fd47e98465824aa85dfe620ad3dbf092b772abc6c6a182e458b5c56ad13b"
    ent = entity.Entity.from_hex(m_fet_key)
    p2p_client_connection = PeerToPeerClientConnection(
        address=ent.public_key_hex,
        provider_addr=address,
        provider_port=port,
        connection_id=P2P_CLIENT_CONNECTION_PUBLIC_ID,
    )
    p2p_client_connection.loop = asyncio.get_event_loop()
    envelope = Envelope(
        to="receiver",
        sender="sender",
        protocol_id=UNKNOWN_PROTOCOL_PUBLIC_ID,
        message=b"Hello",
    )

    with mock.patch.object(fetch.p2p.api.http_calls.HTTPCalls,
                           "get_messages",
                           return_value=[]):
        with mock.patch.object(
                fetch.p2p.api.http_calls.HTTPCalls,
                "register",
                return_value={"status": "OK"},
        ):
            await p2p_client_connection.connect()
            assert p2p_client_connection.connection_status.is_connected is True

    with mock.patch.object(fetch.p2p.api.http_calls.HTTPCalls,
                           "get_messages",
                           return_value=[]):
        with mock.patch.object(
                fetch.p2p.api.http_calls.HTTPCalls,
                "send_message",
                return_value={"status": "OK"},
        ):
            await p2p_client_connection.send(envelope=envelope)
            # TODO: Consider returning the response from the server in order to be able to assert that the message send!
            assert True

    with mock.patch.object(
            fetch.p2p.api.http_calls.HTTPCalls,
            "unregister",
            return_value={"status": "OK"},
    ):
        await p2p_client_connection.disconnect()
        assert p2p_client_connection.connection_status.is_connected is False
Exemple #3
0
def _make_p2p_client_connection(address: Address, provider_addr: str,
                                provider_port: int):
    p2p_client_connection = PeerToPeerClientConnection(
        provider_addr,
        provider_port,
        address=address,
        connection_id=PublicId("fetchai", "p2p", "0.1.0"),
    )
    return p2p_client_connection
Exemple #4
0
def _make_p2p_client_connection(address: Address, provider_addr: str,
                                provider_port: int):
    configuration = ConnectionConfig(
        addr=provider_addr,
        port=provider_port,
        connection_id=PeerToPeerClientConnection.connection_id,
    )
    p2p_client_connection = PeerToPeerClientConnection(
        configuration=configuration,
        identity=Identity("", address),
    )
    return p2p_client_connection
 def setup_class(cls):
     """Initialise the class."""
     cls.address = "127.0.0.1"
     cls.port = 8000
     m_fet_key = "6d56fd47e98465824aa85dfe620ad3dbf092b772abc6c6a182e458b5c56ad13b"
     cls.ent = entity.Entity.from_hex(m_fet_key)
     cls.p2p_client_connection = PeerToPeerClientConnection(
         address=cls.ent.public_key_hex,
         provider_addr=cls.address,
         provider_port=cls.port,
         connection_id=P2P_CLIENT_CONNECTION_PUBLIC_ID,
     )
     cls.p2p_client_connection.loop = asyncio.get_event_loop()
def test_p2p_from_config():
    """Test the Connection from config File."""
    con = PeerToPeerClientConnection.from_config(
        address="pk", connection_configuration=ConnectionConfig())
    assert not con.connection_status.is_connected, "We are connected..."