2019-02-25 09:58:23 +08:00
|
|
|
import asyncio
|
2019-08-03 13:36:19 +08:00
|
|
|
|
|
|
|
import pytest
|
2018-11-27 01:39:36 +08:00
|
|
|
|
2019-01-10 02:38:56 +08:00
|
|
|
from libp2p.peer.peerinfo import info_from_p2p_addr
|
|
|
|
from libp2p.protocol_muxer.multiselect_client import MultiselectClientError
|
2019-08-03 13:36:19 +08:00
|
|
|
from tests.utils import cleanup, set_up_nodes_by_transport_opt
|
2018-11-27 01:39:36 +08:00
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
PROTOCOL_ID = "/chat/1.0.0"
|
|
|
|
|
2018-11-27 01:39:36 +08:00
|
|
|
|
|
|
|
async def hello_world(host_a, host_b):
|
|
|
|
async def stream_handler(stream):
|
|
|
|
read = await stream.read()
|
2019-08-01 06:00:12 +08:00
|
|
|
assert read == b"hello world from host b"
|
|
|
|
await stream.write(b"hello world from host a")
|
2018-11-27 01:39:36 +08:00
|
|
|
await stream.close()
|
|
|
|
|
|
|
|
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])
|
2019-08-01 06:00:12 +08:00
|
|
|
await stream.write(b"hello world from host b")
|
2018-11-27 01:39:36 +08:00
|
|
|
read = await stream.read()
|
2019-08-01 06:00:12 +08:00
|
|
|
assert read == b"hello world from host a"
|
2018-11-27 01:39:36 +08:00
|
|
|
await stream.close()
|
|
|
|
|
|
|
|
|
|
|
|
async def connect_write(host_a, host_b):
|
2019-08-01 06:00:12 +08:00
|
|
|
messages = ["data %d" % i for i in range(5)]
|
2019-02-25 09:58:23 +08:00
|
|
|
received = []
|
2018-11-27 01:39:36 +08:00
|
|
|
|
|
|
|
async def stream_handler(stream):
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
received.append((await stream.read()).decode())
|
|
|
|
except Exception: # exception is raised when other side close the stream ?
|
|
|
|
break
|
2019-08-01 06:00:12 +08:00
|
|
|
|
2018-11-27 01:39:36 +08:00
|
|
|
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:
|
2019-02-25 09:58:23 +08:00
|
|
|
await stream.write(message.encode())
|
|
|
|
|
|
|
|
# Reader needs time due to async reads
|
|
|
|
await asyncio.sleep(2)
|
|
|
|
|
2018-11-27 01:39:36 +08:00
|
|
|
await stream.close()
|
2019-02-25 09:58:23 +08:00
|
|
|
assert received == messages
|
2018-11-27 01:39:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
async def connect_read(host_a, host_b):
|
2019-08-01 06:00:12 +08:00
|
|
|
messages = [b"data %d" % i for i in range(5)]
|
2018-11-27 01:39:36 +08:00
|
|
|
|
|
|
|
async def stream_handler(stream):
|
|
|
|
for message in messages:
|
|
|
|
await stream.write(message)
|
|
|
|
await stream.close()
|
|
|
|
|
|
|
|
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])
|
|
|
|
received = []
|
|
|
|
# while True: Seems the close stream event from the other host is not received
|
|
|
|
for _ in range(5):
|
|
|
|
try:
|
|
|
|
received.append(await stream.read())
|
|
|
|
except Exception: # exception is raised when other side close the stream ?
|
|
|
|
break
|
|
|
|
await stream.close()
|
|
|
|
assert received == messages
|
|
|
|
|
|
|
|
|
|
|
|
async def no_common_protocol(host_a, host_b):
|
2019-08-01 06:00:12 +08:00
|
|
|
messages = [b"data %d" % i for i in range(5)]
|
2018-11-27 01:39:36 +08:00
|
|
|
|
|
|
|
async def stream_handler(stream):
|
|
|
|
for message in messages:
|
|
|
|
await stream.write(message)
|
|
|
|
await stream.close()
|
|
|
|
|
|
|
|
host_a.set_stream_handler(PROTOCOL_ID, stream_handler)
|
|
|
|
|
|
|
|
# try to creates a new new with a procotol not known by the other host
|
|
|
|
with pytest.raises(MultiselectClientError):
|
2019-08-08 14:22:06 +08:00
|
|
|
await host_b.new_stream(host_a.get_id(), ["/fakeproto/0.0.1"])
|
2018-11-27 01:39:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2019-08-01 06:00:12 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"test", [(hello_world), (connect_write), (connect_read), (no_common_protocol)]
|
|
|
|
)
|
2018-11-27 01:39:36 +08:00
|
|
|
async def test_chat(test):
|
2019-03-18 07:33:40 +08:00
|
|
|
transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]]
|
|
|
|
(host_a, host_b) = await set_up_nodes_by_transport_opt(transport_opt_list)
|
2018-11-27 01:39:36 +08:00
|
|
|
|
|
|
|
addr = host_a.get_addrs()[0]
|
|
|
|
info = info_from_p2p_addr(addr)
|
|
|
|
await host_b.connect(info)
|
|
|
|
|
|
|
|
await test(host_a, host_b)
|
2019-02-25 09:58:23 +08:00
|
|
|
|
|
|
|
await cleanup()
|