From 6ff97ebb3e3e74b0cbdfd284d9ff07ea81585b97 Mon Sep 17 00:00:00 2001 From: Alex Haynes Date: Mon, 26 Nov 2018 23:09:16 -0500 Subject: [PATCH] added double response test (#80) * added double response test * moved to existing file --- tests/libp2p/test_libp2p.py | 42 ++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/tests/libp2p/test_libp2p.py b/tests/libp2p/test_libp2p.py index ea9cb183..39f2ec74 100644 --- a/tests/libp2p/test_libp2p.py +++ b/tests/libp2p/test_libp2p.py @@ -23,7 +23,6 @@ async def test_simple_messages(): stream = await node_a.new_stream("node_b", "/echo/1.0.0") messages = ["hello" + str(x) for x in range(10)] - for message in messages: await stream.write(message.encode()) @@ -34,3 +33,44 @@ async def test_simple_messages(): # Success, terminate pending tasks. return + +@pytest.mark.asyncio +async def test_double_response(): + hostA = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/8002/ipfs/hostA"]) + hostB = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/8003/ipfs/hostB"]) + + async def stream_handler(stream): + while True: + read_string = (await stream.read()).decode() + print("host B received:" + read_string) + + response = "ack1:" + read_string + print("sending response:" + response) + await stream.write(response.encode()) + + response = "ack2:" + read_string + print("sending response:" + response) + await stream.write(response.encode()) + + hostB.set_stream_handler("/echo/1.0.0", stream_handler) + + # Associate the peer with local ip address (see default parameters of Libp2p()) + hostA.get_peerstore().add_addr("hostB", "/ip4/127.0.0.1/tcp/8003", 10) + print("hostA about to open stream") + stream = await hostA.new_stream("hostB", "/echo/1.0.0") + messages = ["hello" + str(x) for x in range(10)] + for message in messages: + await stream.write(message.encode()) + + response1 = (await stream.read()).decode() + + print("res1: " + response1) + assert response1 == ("ack1:" + message) + + response2 = (await stream.read()).decode() + + print("res2: " + response2) + assert response2 == ("ack2:" + message) + + # Success, terminate pending tasks. + return