2018-11-29 02:51:50 +08:00
|
|
|
import pytest
|
|
|
|
|
2019-08-21 23:04:59 +08:00
|
|
|
from libp2p.protocol_muxer.exceptions import MultiselectClientError
|
2019-09-06 17:26:40 +08:00
|
|
|
from tests.utils import cleanup, set_up_nodes_by_transport_opt, echo_stream_handler
|
2018-11-29 02:51:50 +08:00
|
|
|
|
|
|
|
# TODO: Add tests for multiple streams being opened on different
|
|
|
|
# protocols through the same connection
|
|
|
|
|
|
|
|
# Note: async issues occurred when using the same port
|
|
|
|
# so that's why I use different ports here.
|
|
|
|
# TODO: modify tests so that those async issues don't occur
|
|
|
|
# when using the same ports across tests
|
|
|
|
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
async def perform_simple_test(
|
|
|
|
expected_selected_protocol, protocols_for_client, protocols_with_handlers
|
|
|
|
):
|
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"]]
|
2019-03-18 09:30:56 +08:00
|
|
|
(node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list)
|
2018-11-29 02:51:50 +08:00
|
|
|
|
|
|
|
for protocol in protocols_with_handlers:
|
2019-09-06 17:26:40 +08:00
|
|
|
node_b.set_stream_handler(protocol, echo_stream_handler)
|
2018-11-29 02:51:50 +08:00
|
|
|
|
|
|
|
# Associate the peer with local ip address (see default parameters of Libp2p())
|
2018-11-29 23:06:40 +08:00
|
|
|
node_a.get_peerstore().add_addrs(node_b.get_id(), node_b.get_addrs(), 10)
|
2018-11-29 02:51:50 +08:00
|
|
|
|
2018-11-29 23:06:40 +08:00
|
|
|
stream = await node_a.new_stream(node_b.get_id(), protocols_for_client)
|
2018-11-29 02:51:50 +08:00
|
|
|
messages = ["hello" + str(x) for x in range(10)]
|
|
|
|
for message in messages:
|
2019-09-06 17:26:40 +08:00
|
|
|
expected_resp = "ack:" + message
|
2018-11-29 02:51:50 +08:00
|
|
|
await stream.write(message.encode())
|
2019-09-06 17:26:40 +08:00
|
|
|
response = (await stream.read(len(expected_resp))).decode()
|
|
|
|
assert response == expected_resp
|
2018-11-29 02:51:50 +08:00
|
|
|
|
|
|
|
assert expected_selected_protocol == stream.get_protocol()
|
|
|
|
|
|
|
|
# Success, terminate pending tasks.
|
2019-02-25 09:58:23 +08:00
|
|
|
await cleanup()
|
2018-11-29 02:51:50 +08:00
|
|
|
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_single_protocol_succeeds():
|
|
|
|
expected_selected_protocol = "/echo/1.0.0"
|
2019-08-14 05:36:42 +08:00
|
|
|
await perform_simple_test(
|
|
|
|
expected_selected_protocol, ["/echo/1.0.0"], ["/echo/1.0.0"]
|
|
|
|
)
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_single_protocol_fails():
|
|
|
|
with pytest.raises(MultiselectClientError):
|
2019-08-01 06:00:12 +08:00
|
|
|
await perform_simple_test("", ["/echo/1.0.0"], ["/potato/1.0.0"])
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2019-02-25 09:58:23 +08:00
|
|
|
# Cleanup not reached on error
|
|
|
|
await cleanup()
|
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_multiple_protocol_first_is_valid_succeeds():
|
|
|
|
expected_selected_protocol = "/echo/1.0.0"
|
|
|
|
protocols_for_client = ["/echo/1.0.0", "/potato/1.0.0"]
|
|
|
|
protocols_for_listener = ["/foo/1.0.0", "/echo/1.0.0"]
|
2019-08-01 06:00:12 +08:00
|
|
|
await perform_simple_test(
|
|
|
|
expected_selected_protocol, protocols_for_client, protocols_for_listener
|
|
|
|
)
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_multiple_protocol_second_is_valid_succeeds():
|
|
|
|
expected_selected_protocol = "/foo/1.0.0"
|
|
|
|
protocols_for_client = ["/rock/1.0.0", "/foo/1.0.0"]
|
|
|
|
protocols_for_listener = ["/foo/1.0.0", "/echo/1.0.0"]
|
2019-08-01 06:00:12 +08:00
|
|
|
await perform_simple_test(
|
|
|
|
expected_selected_protocol, protocols_for_client, protocols_for_listener
|
|
|
|
)
|
2018-12-08 04:00:46 +08:00
|
|
|
|
2018-11-29 02:51:50 +08:00
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_multiple_protocol_fails():
|
|
|
|
protocols_for_client = ["/rock/1.0.0", "/foo/1.0.0", "/bar/1.0.0"]
|
|
|
|
protocols_for_listener = ["/aspyn/1.0.0", "/rob/1.0.0", "/zx/1.0.0", "/alex/1.0.0"]
|
|
|
|
with pytest.raises(MultiselectClientError):
|
2019-08-01 06:00:12 +08:00
|
|
|
await perform_simple_test("", protocols_for_client, protocols_for_listener)
|
2019-02-25 09:58:23 +08:00
|
|
|
|
|
|
|
# Cleanup not reached on error
|
|
|
|
await cleanup()
|