예제 #1
0
async def test_stream_protocol_messages(request, event_loop, monkeypatch):
    alice_multiplexer, bob_multiplexer = MultiplexerPairFactory()
    await run_multiplexers([alice_multiplexer, bob_multiplexer], request,
                           event_loop)

    alice_p2p_protocol = alice_multiplexer.get_protocol_by_type(P2PProtocolV5)
    alice_p2p_protocol.send(Ping(None))
    alice_p2p_protocol.send(Pong(None))

    monkeypatch.setattr(bob_multiplexer, '_stream_idle_timeout', 0.1)
    stream = bob_multiplexer.stream_protocol_messages(P2PProtocolV5)
    cmd = await asyncio.wait_for(stream.asend(None), timeout=DEFAULT_TIMEOUT)
    assert isinstance(cmd, Ping)
    cmd = await asyncio.wait_for(stream.asend(None), timeout=DEFAULT_TIMEOUT)
    assert isinstance(cmd, Pong)

    # Stopping streaming on bob's multiplexer should cause stream_protocol_messages() to
    # terminate.
    stop_streaming_task = asyncio.create_task(bob_multiplexer.stop_streaming())
    try:
        cmd = await asyncio.wait_for(stream.asend(None),
                                     timeout=DEFAULT_TIMEOUT)
    except StopAsyncIteration:
        pass

    assert stop_streaming_task.done()
예제 #2
0
async def test_last_msg_time(monkeypatch):
    alice_multiplexer, bob_multiplexer = MultiplexerPairFactory()

    async with alice_multiplexer.multiplex():
        async with bob_multiplexer.multiplex():
            assert alice_multiplexer.last_msg_time == 0
            assert bob_multiplexer.last_msg_time == 0
            alice_stream = alice_multiplexer.stream_protocol_messages(
                P2PProtocolV5)
            bob_stream = bob_multiplexer.stream_protocol_messages(
                P2PProtocolV5)

            alice_p2p_protocol = alice_multiplexer.get_protocol_by_type(
                P2PProtocolV5)
            bob_p2p_protocol = bob_multiplexer.get_protocol_by_type(
                P2PProtocolV5)

            now = time.monotonic()
            monkeypatch.setattr(time, 'monotonic', lambda: now)

            alice_p2p_protocol.send(Ping(None))
            cmd = await asyncio.wait_for(bob_stream.asend(None),
                                         timeout=DEFAULT_TIMEOUT)
            assert isinstance(cmd, Ping)
            assert bob_multiplexer.last_msg_time == now

            bob_p2p_protocol.send(Pong(None))
            cmd = await asyncio.wait_for(alice_stream.asend(None),
                                         timeout=DEFAULT_TIMEOUT)
            assert isinstance(cmd, Pong)
            assert alice_multiplexer.last_msg_time == now
예제 #3
0
async def test_multiplexer_p2p_and_paragon_protocol():
    alice_multiplexer, bob_multiplexer = MultiplexerPairFactory(
        protocol_types=(SecondProtocol, ), )

    async with alice_multiplexer.multiplex():
        async with bob_multiplexer.multiplex():
            alice_p2p_stream = alice_multiplexer.stream_protocol_messages(
                P2PProtocolV5)
            bob_p2p_stream = bob_multiplexer.stream_protocol_messages(
                P2PProtocolV5)
            alice_second_stream = alice_multiplexer.stream_protocol_messages(
                SecondProtocol)
            bob_second_stream = bob_multiplexer.stream_protocol_messages(
                SecondProtocol)

            alice_p2p_protocol = alice_multiplexer.get_protocol_by_type(
                P2PProtocolV5)
            alice_second_protocol = alice_multiplexer.get_protocol_by_type(
                SecondProtocol)

            bob_p2p_protocol = bob_multiplexer.get_protocol_by_type(
                P2PProtocolV5)
            bob_second_protocol = bob_multiplexer.get_protocol_by_type(
                SecondProtocol)

            alice_second_protocol.send(CommandA(None))
            alice_p2p_protocol.send(Ping(None))
            alice_second_protocol.send(CommandB(None))
            cmd = await asyncio.wait_for(bob_p2p_stream.asend(None),
                                         timeout=DEFAULT_TIMEOUT)
            assert isinstance(cmd, Ping)

            bob_second_protocol.send(CommandA(None))
            bob_p2p_protocol.send(Pong(None))
            bob_second_protocol.send(CommandB(None))

            cmd = await asyncio.wait_for(alice_p2p_stream.asend(None),
                                         timeout=DEFAULT_TIMEOUT)
            assert isinstance(cmd, Pong)

            cmd_1 = await asyncio.wait_for(bob_second_stream.asend(None),
                                           timeout=DEFAULT_TIMEOUT
                                           )  # noqa: E501
            cmd_2 = await asyncio.wait_for(bob_second_stream.asend(None),
                                           timeout=DEFAULT_TIMEOUT
                                           )  # noqa: E501
            cmd_3 = await asyncio.wait_for(alice_second_stream.asend(None),
                                           timeout=DEFAULT_TIMEOUT
                                           )  # noqa: E501
            cmd_4 = await asyncio.wait_for(alice_second_stream.asend(None),
                                           timeout=DEFAULT_TIMEOUT
                                           )  # noqa: E501

            assert isinstance(cmd_1, CommandA)
            assert isinstance(cmd_2, CommandB)
            assert isinstance(cmd_3, CommandA)
            assert isinstance(cmd_4, CommandB)
예제 #4
0
async def test_p2p_api_pongs_when_pinged(bob, alice):
    async with P2PAPI().as_behavior().apply(alice):
        got_pong = asyncio.Event()

        async def handle_pong(connection, msg):
            got_pong.set()
        bob.add_command_handler(Pong, handle_pong)
        bob.get_base_protocol().send(Ping(None))
        await asyncio.wait_for(got_pong.wait(), timeout=1)
예제 #5
0
async def test_command_handler_logic():
    got_ping = asyncio.Event()

    class HandlePing(CommandHandler):
        command_type = Ping

        async def handle(self, connection, msg):
            got_ping.set()

    async with ConnectionPairFactory() as (alice, bob):
        ping_handler = HandlePing()
        async with ping_handler.as_behavior().apply(alice):
            bob.get_base_protocol().send(Ping(None))
            await asyncio.wait_for(got_ping.wait(), timeout=2)
예제 #6
0
async def test_connection_waits_to_feed_protocol_streams():
    async with ConnectionPairFactory(start_streams=False) as (alice_connection,
                                                              bob_connection):
        got_ping = asyncio.Event()

        async def _handle_ping(conn, cmd):
            got_ping.set()

        alice_connection.add_command_handler(Ping, _handle_ping)

        bob_base_protocol = bob_connection.get_base_protocol()
        bob_base_protocol.send(Ping(None))

        with pytest.raises(asyncio.TimeoutError):
            await asyncio.wait_for(got_ping.wait(), timeout=0.1)

        alice_connection.start_protocol_streams()

        await asyncio.wait_for(got_ping.wait(), timeout=1)
async def do_ping_pong_test(alice_connection: ConnectionAPI,
                            bob_connection: ConnectionAPI) -> None:
    got_ping = asyncio.Event()
    got_pong = asyncio.Event()

    async def _handle_ping(connection: ConnectionAPI, msg: Any) -> None:
        got_ping.set()
        bob_connection.get_base_protocol().send(Pong(None))

    async def _handle_pong(connection: ConnectionAPI, msg: Any) -> None:
        got_pong.set()

    alice_connection.add_command_handler(Pong, _handle_pong)
    bob_connection.add_command_handler(Ping, _handle_ping)

    alice_connection.get_base_protocol().send(Ping(None))

    await asyncio.wait_for(got_ping.wait(), timeout=1)
    await asyncio.wait_for(got_pong.wait(), timeout=1)
예제 #8
0
async def test_multiplexer_only_p2p_protocol(request, event_loop):
    alice_multiplexer, bob_multiplexer = MultiplexerPairFactory()
    await run_multiplexers([alice_multiplexer, bob_multiplexer], request,
                           event_loop)

    alice_stream = alice_multiplexer.stream_protocol_messages(P2PProtocolV5)
    bob_stream = bob_multiplexer.stream_protocol_messages(P2PProtocolV5)

    alice_p2p_protocol = alice_multiplexer.get_protocol_by_type(P2PProtocolV5)
    bob_p2p_protocol = bob_multiplexer.get_protocol_by_type(P2PProtocolV5)

    alice_p2p_protocol.send(Ping(None))
    cmd = await asyncio.wait_for(bob_stream.asend(None),
                                 timeout=DEFAULT_TIMEOUT)
    assert isinstance(cmd, Ping)

    bob_p2p_protocol.send(Pong(None))
    cmd = await asyncio.wait_for(alice_stream.asend(None),
                                 timeout=DEFAULT_TIMEOUT)
예제 #9
0
async def test_multiplexer_only_p2p_protocol():
    alice_multiplexer, bob_multiplexer = MultiplexerPairFactory()

    async with alice_multiplexer.multiplex():
        async with bob_multiplexer.multiplex():
            alice_stream = alice_multiplexer.stream_protocol_messages(
                P2PProtocolV5)
            bob_stream = bob_multiplexer.stream_protocol_messages(
                P2PProtocolV5)

            alice_p2p_protocol = alice_multiplexer.get_protocol_by_type(
                P2PProtocolV5)
            bob_p2p_protocol = bob_multiplexer.get_protocol_by_type(
                P2PProtocolV5)

            alice_p2p_protocol.send(Ping(None))
            cmd = await asyncio.wait_for(bob_stream.asend(None), timeout=0.1)
            assert isinstance(cmd, Ping)

            bob_p2p_protocol.send(Pong(None))
            cmd = await asyncio.wait_for(alice_stream.asend(None), timeout=0.1)
def _send_ping(connection: ConnectionAPI) -> None:
    connection.get_base_protocol().send(Ping(None))
예제 #11
0
async def test_multiplexer_p2p_and_two_more_protocols():
    alice_multiplexer, bob_multiplexer = MultiplexerPairFactory(
        protocol_types=(SecondProtocol, ThirdProtocol), )

    async with alice_multiplexer.multiplex():
        async with bob_multiplexer.multiplex():
            alice_p2p_stream = alice_multiplexer.stream_protocol_messages(
                P2PProtocolV5)
            bob_p2p_stream = bob_multiplexer.stream_protocol_messages(
                P2PProtocolV5)
            alice_second_stream = alice_multiplexer.stream_protocol_messages(
                SecondProtocol)
            bob_second_stream = bob_multiplexer.stream_protocol_messages(
                SecondProtocol)
            alice_third_stream = alice_multiplexer.stream_protocol_messages(
                ThirdProtocol)
            bob_third_stream = bob_multiplexer.stream_protocol_messages(
                ThirdProtocol)

            alice_p2p_protocol = alice_multiplexer.get_protocol_by_type(
                P2PProtocolV5)
            alice_second_protocol = alice_multiplexer.get_protocol_by_type(
                SecondProtocol)
            alice_third_protocol = alice_multiplexer.get_protocol_by_type(
                ThirdProtocol)

            bob_p2p_protocol = bob_multiplexer.get_protocol_by_type(
                P2PProtocolV5)
            bob_second_protocol = bob_multiplexer.get_protocol_by_type(
                SecondProtocol)
            bob_third_protocol = bob_multiplexer.get_protocol_by_type(
                ThirdProtocol)

            alice_second_protocol.send(CommandA(None))
            alice_third_protocol.send(CommandC(None))
            alice_p2p_protocol.send(Ping(None))
            alice_second_protocol.send(CommandB(None))
            alice_third_protocol.send(CommandD(None))
            cmd = await asyncio.wait_for(bob_p2p_stream.asend(None),
                                         timeout=0.1)
            assert isinstance(cmd, Ping)

            bob_second_protocol.send(CommandA(None))
            bob_third_protocol.send(CommandC(None))
            bob_p2p_protocol.send(Pong(None))
            bob_second_protocol.send(CommandB(None))
            bob_third_protocol.send(CommandD(None))
            cmd = await asyncio.wait_for(alice_p2p_stream.asend(None),
                                         timeout=0.1)
            assert isinstance(cmd, Pong)

            cmd_1 = await asyncio.wait_for(bob_third_stream.asend(None),
                                           timeout=0.1)  # noqa: E501
            cmd_2 = await asyncio.wait_for(bob_third_stream.asend(None),
                                           timeout=0.1)  # noqa: E501
            cmd_3 = await asyncio.wait_for(bob_second_stream.asend(None),
                                           timeout=0.1)  # noqa: E501
            cmd_4 = await asyncio.wait_for(bob_second_stream.asend(None),
                                           timeout=0.1)  # noqa: E501
            cmd_5 = await asyncio.wait_for(alice_third_stream.asend(None),
                                           timeout=0.1)  # noqa: E501
            cmd_6 = await asyncio.wait_for(alice_third_stream.asend(None),
                                           timeout=0.1)  # noqa: E501
            cmd_7 = await asyncio.wait_for(alice_second_stream.asend(None),
                                           timeout=0.1)  # noqa: E501
            cmd_8 = await asyncio.wait_for(alice_second_stream.asend(None),
                                           timeout=0.1)  # noqa: E501

            assert isinstance(cmd_1, CommandC)
            assert isinstance(cmd_2, CommandD)
            assert isinstance(cmd_3, CommandA)
            assert isinstance(cmd_4, CommandB)
            assert isinstance(cmd_5, CommandC)
            assert isinstance(cmd_6, CommandD)
            assert isinstance(cmd_7, CommandA)
            assert isinstance(cmd_8, CommandB)
예제 #12
0
 def send_ping(self) -> None:
     self.connection.get_base_protocol().send(Ping(None))