示例#1
0
async def test_can_asend_from_pipe():
    with pynng.Pair0(listen=addr) as s0, pynng.Pair0(dial=addr) as s1:
        wait_pipe_len(s0, 1)
        await s0.asend(b'hello')
        assert await s1.arecv() == b'hello'
        await s0.asend_msg(pynng.Message(b'it is me again'))
        assert await s1.arecv() == b'it is me again'
示例#2
0
def test_closing_pipe_in_pre_connect_works():
    with pynng.Pair0(listen=addr) as s0, pynng.Pair0() as s1:
        s0.name = 's0'
        s1.name = 's1'
        pre_called = False
        post_connect_called = False
        post_remove_called = False

        def pre_connect_cb(pipe):
            pipe.close()
            nonlocal pre_called
            pre_called = True

        def post_connect_cb(pipe):
            nonlocal post_connect_called
            post_connect_called = True

        def post_remove_cb(pipe):
            nonlocal post_remove_called
            post_remove_called = True

        s0.add_pre_pipe_connect_cb(pre_connect_cb)
        s0.add_post_pipe_connect_cb(post_connect_cb)
        s1.dial(addr)
        later = time.time() + 5
        while later > time.time():
            if pre_called:
                break
        assert pre_called
        time.sleep(0.5)
        assert not post_connect_called
        assert not post_remove_called
        assert len(s0.pipes) == 0
        assert len(s1.pipes) == 0
示例#3
0
def test_context_manager_works():
    # we have to grab a reference to the sockets so garbage collection doesn't
    # close the socket for us automatically.
    with pynng.Pair0(listen=addr) as s0:  # noqa
        pass
    # we should be able to do it again if the context manager worked
    with pynng.Pair0(listen=addr) as s1:  # noqa
        pass
示例#4
0
def test_socket_send_recv_msg():
    with pynng.Pair0(listen=addr, recv_timeout=to) as s1, \
            pynng.Pair0(dial=addr, recv_timeout=to) as s2:
        wait_pipe_len(s1, 1)
        msg = pynng.Message(b'we are friends, old buddy')
        s1.send_msg(msg)
        msg2 = s2.recv_msg()
        assert msg2.bytes == b'we are friends, old buddy'
示例#5
0
def test_arecv_trio():
    with pynng.Pair0(listen=addr, recv_timeout=1000) as listener, \
            pynng.Pair0(dial=addr) as dialer:

        thread = _send_data_bg(dialer, 0.1, b'hello there buddy')
        hey_there = trio.run(listener.arecv)
        assert hey_there == b'hello there buddy'
        thread.join()
示例#6
0
def test_can_send_from_pipe():
    with pynng.Pair0(listen=addr) as s0, pynng.Pair0(dial=addr) as s1:
        wait_pipe_len(s0, 1)
        s0.send(b'hello')
        assert s1.recv() == b'hello'
        s0.send_msg(pynng.Message(b'it is me again'))
        assert s1.recv() == b'it is me again'
        time.sleep(0.05)
示例#7
0
def test_asend_trio():
    async def asend_and_arecv(sender, receiver, msg):
        await sender.asend(msg)
        return await receiver.arecv()

    with pynng.Pair0(listen=addr, recv_timeout=2000) as listener, \
            pynng.Pair0(dial=addr, send_timeout=2000) as dialer:
        msg = trio.run(asend_and_arecv, dialer, listener, b'hello there')
        assert msg == b'hello there'
示例#8
0
async def test_socket_arecv_asend_msg():
    with pynng.Pair0(listen=addr, recv_timeout=to) as s1, \
            pynng.Pair0(dial=addr, recv_timeout=to) as s2:
        wait_pipe_len(s1, 1)
        msg = pynng.Message(b'you truly are a pal')
        await s1.asend_msg(msg)
        msg2 = await s2.arecv_msg()
        assert msg2.bytes == b'you truly are a pal'
        assert msg2.pipe is s2.pipes[0]
示例#9
0
def test_arecv_asyncio():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    with pynng.Pair0(listen=addr, recv_timeout=1000) as listener, \
            pynng.Pair0(dial=addr) as dialer:

        thread = _send_data_bg(dialer, 0.1, b'hello there buddy')
        hey_there = loop.run_until_complete(listener.arecv())
        assert hey_there == b'hello there buddy'
        thread.join()
示例#10
0
def test_pipe_gets_added_and_removed():
    # add sleeps to ensure the nng_pipe_cb gets called.
    with pynng.Pair0(listen=addr) as s0, pynng.Pair0() as s1:
        assert len(s0.pipes) == 0
        assert len(s1.pipes) == 0
        s1.dial(addr)
        assert wait_pipe_len(s0, 1)
        assert wait_pipe_len(s1, 1)
    assert wait_pipe_len(s0, 0)
    assert wait_pipe_len(s1, 0)
示例#11
0
def test_close_pipe_works():
    # this is some racy business
    with pynng.Pair0(listen=addr) as s0, \
            pynng.Pair0(reconnect_time_min=40000, dial=addr) as s1:
        assert wait_pipe_len(s0, 1)
        assert wait_pipe_len(s1, 1)
        pipe0 = s0.pipes[0]
        pipe0.close()
        assert wait_pipe_len(s0, 0)
        assert wait_pipe_len(s1, 0)
示例#12
0
def test_asend_asyncio():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    with pynng.Pair0(listen=addr, recv_timeout=2000) as listener, \
            pynng.Pair0(dial=addr, send_timeout=2000) as dialer:
        arecv = listener.arecv()
        asend = dialer.asend(b'hello friend')
        g = asyncio.gather(asend, arecv)
        sent, received = loop.run_until_complete(g)
        assert received == b'hello friend'
        assert sent is None
示例#13
0
def test_pre_pipe_connect_cb_totally_works():
    with pynng.Pair0(listen=addr) as s0, pynng.Pair0() as s1:
        called = False

        def pre_connect_cb(_):
            nonlocal called
            called = True
        s0.add_pre_pipe_connect_cb(pre_connect_cb)
        s1.dial(addr)
        assert wait_pipe_len(s0, 1)
        assert wait_pipe_len(s1, 1)
        assert called
示例#14
0
def test_socket_send_recv_msg_from_pipe():
    with pynng.Pair0(listen=addr, recv_timeout=to) as s1, \
            pynng.Pair0(dial=addr, recv_timeout=to) as s2:
        wait_pipe_len(s1, 1)
        pipe = s1.pipes[0]
        msg = pynng.Message(b'oh hello friend', pipe)
        assert isinstance(msg, pynng.Message)
        assert msg.bytes == b'oh hello friend'
        s1.send_msg(msg)
        assert msg.pipe is pipe
        msg2 = s2.recv_msg()
        assert isinstance(msg2, pynng.Message)
        assert msg2.bytes == b'oh hello friend'
        assert msg2.pipe is s2.pipes[0]
示例#15
0
def test_pipe_local_and_remote_addresses():
    with pynng.Pair0(listen=addr) as s0, pynng.Pair0(dial=addr) as s1:
        assert wait_pipe_len(s0, 1)
        assert wait_pipe_len(s1, 1)
        p0 = s0.pipes[0]
        p1 = s1.pipes[0]
        local_addr0 = p0.local_address
        remote_addr0 = p0.remote_address
        local_addr1 = p1.local_address
        remote_addr1 = p1.remote_address
        assert str(local_addr0) == addr.replace('tcp://', '')
        assert str(local_addr0) == str(remote_addr1)
        # on Windows, the local address is 0.0.0.0:0
        assert str(local_addr1) == str(remote_addr0)
示例#16
0
def test_dialers_get_added():
    with pynng.Pair0() as s:
        assert len(s.dialers) == 0
        s.dial(addr, block=False)
        assert len(s.dialers) == 1
        s.dial(addr2, block=False)
        assert len(s.dialers) == 2
示例#17
0
def test_post_pipe_connect_cb_works():
    with pynng.Pair0(listen=addr) as s0, pynng.Pair0() as s1:
        post_called = False

        def post_connect_cb(pipe):
            nonlocal post_called
            post_called = True

        s0.add_post_pipe_connect_cb(post_connect_cb)
        s1.dial(addr)

        later = time.time() + 10
        while later > time.time():
            if post_called:
                break
        assert post_called
示例#18
0
def test_listeners_get_added():
    with pynng.Pair0() as s:
        assert len(s.listeners) == 0
        s.listen(addr)
        assert len(s.listeners) == 1
        s.listen(addr2)
        assert len(s.listeners) == 2
示例#19
0
def test_arecv_trio_cancel():
    async def cancel_real_fast(sock):
        with trio.fail_after(0.02):
            return await sock.arecv()

    with pynng.Pair0(listen=addr, recv_timeout=5000) as p0:
        with pytest.raises(trio.TooSlowError):
            trio.run(cancel_real_fast, p0)
示例#20
0
def test_bad_callbacks_dont_cause_extra_failures():
    called_pre_connect = False

    def pre_connect_cb(pipe):
        nonlocal called_pre_connect
        called_pre_connect = True

    with pynng.Pair0(listen=addr) as s0:
        # adding something that is not a callback should still allow correct
        # things to work.
        s0.add_pre_pipe_connect_cb(8)
        s0.add_pre_pipe_connect_cb(pre_connect_cb)
        with pynng.Pair0(dial=addr) as _:
            wait_pipe_len(s0, 1)
            later = time.time() + 10
            while later > time.time():
                if called_pre_connect:
                    break
            assert called_pre_connect
示例#21
0
def test_closing_listener_works():
    with pynng.Pair0(listen=addr) as s:
        assert len(s.listeners) == 1
        s.listeners[0].close()
        assert len(s.listeners) == 0
        # if the listener is really closed, we should be able to listen at the
        # same address again
        s.listen(addr)
        assert len(s.listeners) == 1
    assert len(s.listeners) == 0
示例#22
0
def test_post_pipe_remove_cb_works():
    with pynng.Pair0(listen=addr) as s0, pynng.Pair0() as s1:
        post_called = False

        def post_remove_cb(pipe):
            nonlocal post_called
            post_called = True

        s0.add_post_pipe_remove_cb(post_remove_cb)
        s1.dial(addr)
        wait_pipe_len(s0, 1)
        wait_pipe_len(s1, 1)
        assert not post_called

    later = time.time() + 10
    while later > time.time():
        if post_called:
            break
    assert post_called
示例#23
0
def test_closing_listener_works():
    with pynng.Pair0(listen=addr) as s:
        assert len(s.listeners) == 1
        s.listeners[0].close()
        assert len(s.listeners) == 0
        # if the listener is really closed, we should be able to listen at the
        # same address again; we'll sleep a little so OS X CI will pass.
        time.sleep(0.01)
        s.listen(addr)
        assert len(s.listeners) == 1
    assert len(s.listeners) == 0
示例#24
0
async def test_arecv_asyncio_cancel():
    async def cancel_soon(fut, sleep_time=0.05):
        # need to sleep for some amount of time to ensure the arecv actually
        # had time to start.
        await asyncio.sleep(sleep_time)
        fut.cancel()

    with pynng.Pair0(listen=addr, recv_timeout=5000) as p0:
        arecv = p0.arecv()
        fut = asyncio.ensure_future(arecv)
        with pytest.raises(asyncio.CancelledError):
            await asyncio.gather(fut, cancel_soon(fut))
示例#25
0
def test_arecv_asyncio_cancel():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    async def cancel_soon(fut, sleep_time=0.2):
        await asyncio.sleep(sleep_time)
        fut.cancel()

    with pynng.Pair0(listen=addr, recv_timeout=5000) as p0:
        arecv = p0.arecv()
        fut = asyncio.ensure_future(arecv)
        g = asyncio.gather(fut, cancel_soon(fut))
        with pytest.raises(asyncio.CancelledError):
            loop.run_until_complete(g)
示例#26
0
def main():
    if len(sys.argv) < 3:
        usage()
    node = sys.argv[1]
    if node not in ('node0', 'node1'):
        usage()
    addr = sys.argv[2]
    with pynng.Pair0(recv_timeout=100, send_timeout=100) as sock:
        if node == 'node0':
            sock.listen(addr)
        else:
            sock.dial(addr)
        while True:
            try:
                msg = sock.recv()
                print('got message from', msg.decode())
            except pynng.Timeout:
                pass
            time.sleep(0.5)
            try:
                sock.send(node.encode())
            except pynng.Timeout:
                pass
示例#27
0
def test_nonblocking_recv_works():
    with pynng.Pair0(listen=addr) as s:
        with pytest.raises(pynng.TryAgain):
            s.recv(block=False)
示例#28
0
def test_closing_dialer_works():
    with pynng.Pair0(dial=addr, block_on_dial=False) as s:
        assert len(s.dialers) == 1
        s.dialers[0].close()
    assert len(s.listeners) == 0
示例#29
0
async def test_arecv_trio_cancel():
    with pynng.Pair0(listen=addr, recv_timeout=5000) as p0:
        with pytest.raises(trio.TooSlowError):
            with trio.fail_after(0.001):
                await p0.arecv()
示例#30
0
async def test_asend_trio_send_timeout():
    with pytest.raises(pynng.exceptions.Timeout):
        with pynng.Pair0(listen=addr, send_timeout=1) as p0:
            await p0.asend(b'foo')