Esempio n. 1
0
 def block(socket):
     """Block until the server replies."""
     socket.settimeout(0.01)
     peer = server.getsockname()
     data = os.urandom(3)
     socket.sendto(data, peer)
     try:
         assert recvfrom(socket, peer) == data
     except RecvTimeout:
         pass
Esempio n. 2
0
 def pound(socket):
     host = socket.getsockname()
     peer = server.getsockname()
     socket.settimeout(0.01)
     try:
         packets = [os.urandom(3) for i in range(3)]
         for data in packets:
             print('C:', host, 'send')
             socket.sendto(data, peer)
         for data in packets:
             print('C:', host, 'recv')
             assert recvfrom(socket, peer) == data
     except RecvTimeout:
         pass
Esempio n. 3
0
def test_recvfrom():
    """Drop packet from unexpected peers."""

    socket = mock.MagicMock()
    socket.recvfrom.side_effect = [
        (b'abc', ('127.0.0.1', 8888)),
        (b'def', ('127.0.0.1', 9999)),
        (b'ghi', ('127.0.0.1', 7777)),
    ]

    data = recvfrom(socket, ('127.0.0.1', 7777))
    assert data == b'ghi'

    assert socket.recvfrom.call_count == 3
Esempio n. 4
0
def test_udp_client(stack):
    """UDP client can exchange packets with a server."""

    client = stack.enter_context(udpsocket())
    server = stack.enter_context(udpsocket())

    def echo(push, pull, peer):
        assert peer == str(server.getsockname())
        while True:
            push(pull())

    stack.enter_context(
        concurrently(
            udp_client,
            client, echo,
            server.getsockname(),
        )
    )

    peer = client.getsockname()
    for i in range(3):
        data = os.urandom(3)
        server.sendto(data, peer)
        assert recvfrom(server, peer) == data
Esempio n. 5
0
 def pound(socket):
     peer = server.getsockname()
     for i in range(3):
         data = os.urandom(3)
         socket.sendto(data, peer)
         assert recvfrom(socket, peer) == data