mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Stream rearchitecture (#126)
* Add generic protocol handler * Add generic protocol handler to stream muxing pipeline * Modify conn_handler to only deal with connections * mplex accept stream architecture changes * Add create generic protocol handler * Fix minor bugs * who would win 4 devs or one not * Debugging * rearch with handle_incoming infinite loop, seems to work, needs cleanup" * passing linting, still needs cleanup * fixing linting again; code still needs cleanup * fixing tests; code still needs cleanup * adding test cleanup and task cleanup, removing prints * linting, and cleanup complete * storing connections based on peer id * remove dead code * remove unnecessary peer_id
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
import pytest
|
||||
import asyncio
|
||||
|
||||
from tests.utils import cleanup
|
||||
from libp2p import new_node
|
||||
from libp2p.peer.peerinfo import info_from_p2p_addr
|
||||
from libp2p.protocol_muxer.multiselect_client import MultiselectClientError
|
||||
@ -27,25 +29,28 @@ async def hello_world(host_a, host_b):
|
||||
|
||||
|
||||
async def connect_write(host_a, host_b):
|
||||
messages = [b'data %d' % i for i in range(5)]
|
||||
messages = ['data %d' % i for i in range(5)]
|
||||
received = []
|
||||
|
||||
async def stream_handler(stream):
|
||||
received = []
|
||||
while True:
|
||||
try:
|
||||
received.append((await stream.read()).decode())
|
||||
except Exception: # exception is raised when other side close the stream ?
|
||||
break
|
||||
await stream.close()
|
||||
assert received == messages
|
||||
host_a.set_stream_handler(PROTOCOL_ID, stream_handler)
|
||||
|
||||
# Start a stream with the destination.
|
||||
# Multiaddress of the destination peer is fetched from the peerstore using 'peerId'.
|
||||
stream = await host_b.new_stream(host_a.get_id(), [PROTOCOL_ID])
|
||||
for message in messages:
|
||||
await stream.write(message)
|
||||
await stream.write(message.encode())
|
||||
|
||||
# Reader needs time due to async reads
|
||||
await asyncio.sleep(2)
|
||||
|
||||
await stream.close()
|
||||
assert received == messages
|
||||
|
||||
|
||||
async def connect_read(host_a, host_b):
|
||||
@ -103,3 +108,5 @@ async def test_chat(test):
|
||||
await host_b.connect(info)
|
||||
|
||||
await test(host_a, host_b)
|
||||
|
||||
await cleanup()
|
||||
|
||||
Reference in New Issue
Block a user