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'
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'
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]
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)
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)
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
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]
def test_sub_sock_options(): with pynng.Pub0(listen=addr) as pub: # test single option topic with pynng.Sub0(dial=addr, topics='beep', recv_timeout=500) as sub: wait_pipe_len(sub, 1) pub.send(b'beep hi') assert sub.recv() == b'beep hi' with pynng.Sub0(dial=addr, topics=['beep', 'hello'], recv_timeout=500) as sub: wait_pipe_len(sub, 1) pub.send(b'beep hi') assert sub.recv() == b'beep hi' pub.send(b'hello there') assert sub.recv() == b'hello there'
def test_context_recv_send_msg(): with pynng.Req0(listen=addr, recv_timeout=to) as s1, \ pynng.Rep0(dial=addr, recv_timeout=to) as s2: with s1.new_context() as ctx1, s2.new_context() as ctx2: wait_pipe_len(s1, 1) msg = pynng.Message(b'do i even know you') ctx1.send_msg(msg) msg2 = ctx2.recv_msg() assert msg2.pipe is s2.pipes[0] assert msg2.bytes == b'do i even know you' msg3 = pynng.Message(b'yes of course i am your favorite platypus') ctx2.send_msg(msg3) msg4 = ctx1.recv_msg() assert msg4.pipe is s1.pipes[0] assert msg4.bytes == b'yes of course i am your favorite platypus'
def test_bus(): with pynng.Bus0(recv_timeout=100) as s0, \ pynng.Bus0(recv_timeout=100) as s1, \ pynng.Bus0(recv_timeout=100) as s2: s0.listen(addr) s1.dial(addr) s2.dial(addr) wait_pipe_len(s0, 2) s0.send(b's1 and s2 get this') assert s1.recv() == b's1 and s2 get this' assert s2.recv() == b's1 and s2 get this' s1.send(b'only s0 gets this') assert s0.recv() == b'only s0 gets this' s2.recv_timeout = 0 with pytest.raises(pynng.Timeout): s2.recv()
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 if sys.platform == 'win32': assert str(local_addr1) == '0.0.0.0:0' else: assert str(local_addr1) == str(remote_addr0)
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
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
def test_pair1_polyamorousness(): with pynng.Pair1(listen=addr, polyamorous=True, recv_timeout=500) as s0, \ pynng.Pair1(dial=addr, polyamorous=True, recv_timeout=500) as s1: wait_pipe_len(s0, 1) # pipe for s1 . p1 = s0.pipes[0] with pynng.Pair1(dial=addr, polyamorous=True, recv_timeout=500) as s2: wait_pipe_len(s0, 2) # pipes is backed by a dict, so we can't rely on order in # Python 3.5. pipes = s0.pipes p2 = pipes[1] if p2 is p1: p2 = pipes[0] p1.send(b'hello s1') assert s1.recv() == b'hello s1' p2.send(b'hello there s2') assert s2.recv() == b'hello there s2'