mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
refactoring stream IDs
This commit is contained in:
@ -80,6 +80,54 @@ async def test_double_response():
|
||||
# Success, terminate pending tasks.
|
||||
return
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_multiple_streams():
|
||||
# Node A should be able to open a stream with node B and then vice versa.
|
||||
# Stream IDs should be generated uniquely so that the stream state is not overwritten
|
||||
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/8004"])
|
||||
node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/8005"])
|
||||
|
||||
async def stream_handler_a(stream):
|
||||
while True:
|
||||
read_string = (await stream.read()).decode()
|
||||
|
||||
response = "ack_a:" + read_string
|
||||
await stream.write(response.encode())
|
||||
|
||||
async def stream_handler_b(stream):
|
||||
while True:
|
||||
read_string = (await stream.read()).decode()
|
||||
|
||||
response = "ack_b:" + read_string
|
||||
await stream.write(response.encode())
|
||||
|
||||
node_a.set_stream_handler("/echo_a/1.0.0", stream_handler_a)
|
||||
node_b.set_stream_handler("/echo_b/1.0.0", stream_handler_b)
|
||||
|
||||
# Associate the peer with local ip address (see default parameters of Libp2p())
|
||||
node_a.get_peerstore().add_addrs(node_b.get_id(), node_b.get_addrs(), 10)
|
||||
node_b.get_peerstore().add_addrs(node_a.get_id(), node_a.get_addrs(), 10)
|
||||
|
||||
stream_a = await node_a.new_stream(node_b.get_id(), ["/echo_b/1.0.0"])
|
||||
stream_b = await node_b.new_stream(node_a.get_id(), ["/echo_a/1.0.0"])
|
||||
|
||||
# A writes to /echo_b via stream_a, and B writes to /echo_a via stream_b
|
||||
messages = ["hello" + str(x) for x in range(10)]
|
||||
for message in messages:
|
||||
a_message = message + "_a"
|
||||
b_message = message + "_b"
|
||||
|
||||
await stream_a.write(a_message.encode())
|
||||
await stream_b.write(b_message.encode())
|
||||
|
||||
response_a = (await stream_a.read()).decode()
|
||||
response_b = (await stream_b.read()).decode()
|
||||
|
||||
assert response_a == ("ack_b:" + a_message) and response_b == ("ack_a:" + b_message)
|
||||
|
||||
# Success, terminate pending tasks.
|
||||
return
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_host_connect():
|
||||
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/8001/"])
|
||||
|
||||
@ -1,9 +1,6 @@
|
||||
import asyncio
|
||||
import pytest
|
||||
|
||||
# from network.connection.raw_connection import RawConnection
|
||||
|
||||
|
||||
async def handle_echo(reader, writer):
|
||||
data = await reader.read(100)
|
||||
writer.write(data)
|
||||
@ -20,7 +17,6 @@ async def test_simple_echo():
|
||||
await asyncio.start_server(handle_echo, server_ip, server_port)
|
||||
|
||||
reader, writer = await asyncio.open_connection(server_ip, server_port)
|
||||
# raw_connection = RawConnection(server_ip, server_port, reader, writer)
|
||||
|
||||
test_message = "hello world"
|
||||
writer.write(test_message.encode())
|
||||
|
||||
Reference in New Issue
Block a user