diff --git a/tests/examples/test_chat.py b/tests/examples/test_chat.py index 0ea1979..ee192cc 100644 --- a/tests/examples/test_chat.py +++ b/tests/examples/test_chat.py @@ -1,7 +1,8 @@ import pytest import asyncio +import multiaddr -from tests.utils import cleanup +from tests.utils import cleanup, set_up_nodes_by_transport_opt from libp2p import new_node from libp2p.peer.peerinfo import info_from_p2p_addr from libp2p.protocol_muxer.multiselect_client import MultiselectClientError @@ -9,7 +10,6 @@ from libp2p.protocol_muxer.multiselect_client import MultiselectClientError PROTOCOL_ID = '/chat/1.0.0' - async def hello_world(host_a, host_b): async def stream_handler(stream): read = await stream.read() @@ -100,8 +100,8 @@ async def no_common_protocol(host_a, host_b): (no_common_protocol), ]) async def test_chat(test): - host_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) - host_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) + 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) addr = host_a.get_addrs()[0] info = info_from_p2p_addr(addr) diff --git a/tests/libp2p/test_libp2p.py b/tests/libp2p/test_libp2p.py index 4b7e59c..3d74516 100644 --- a/tests/libp2p/test_libp2p.py +++ b/tests/libp2p/test_libp2p.py @@ -1,17 +1,16 @@ import multiaddr import pytest -from tests.utils import cleanup +from tests.utils import cleanup, set_up_nodes_by_transport_opt from libp2p import new_node from libp2p.peer.peerinfo import info_from_p2p_addr + # pylint: disable=too-many-locals - - @pytest.mark.asyncio async def test_simple_messages(): - node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) - node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) + transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]] + (node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list) async def stream_handler(stream): while True: @@ -41,8 +40,8 @@ async def test_simple_messages(): @pytest.mark.asyncio async def test_double_response(): - node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) - node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) + transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]] + (node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list) async def stream_handler(stream): while True: @@ -78,8 +77,8 @@ async def test_double_response(): 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/0"]) - node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) + transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]] + (node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list) async def stream_handler_a(stream): while True: @@ -124,8 +123,8 @@ async def test_multiple_streams(): @pytest.mark.asyncio async def test_multiple_streams_same_initiator_different_protocols(): - node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) - node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) + transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]] + (node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list) async def stream_handler_a1(stream): while True: @@ -184,8 +183,8 @@ async def test_multiple_streams_same_initiator_different_protocols(): @pytest.mark.asyncio async def test_multiple_streams_two_initiators(): - node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) - node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) + transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]] + (node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list) async def stream_handler_a1(stream): while True: @@ -262,9 +261,9 @@ async def test_multiple_streams_two_initiators(): @pytest.mark.asyncio async def test_triangle_nodes_connection(): - node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) - node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) - node_c = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) + transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"],\ + ["/ip4/127.0.0.1/tcp/0"]] + (node_a, node_b, node_c) = await set_up_nodes_by_transport_opt(transport_opt_list) async def stream_handler(stream): while True: @@ -315,8 +314,8 @@ async def test_triangle_nodes_connection(): @pytest.mark.asyncio async def test_host_connect(): - node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) - node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) + transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]] + (node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list) assert not node_a.get_peerstore().peers() diff --git a/tests/libp2p/test_notify.py b/tests/libp2p/test_notify.py index 108f04a..a2fecaf 100644 --- a/tests/libp2p/test_notify.py +++ b/tests/libp2p/test_notify.py @@ -11,7 +11,7 @@ features are implemented in swarm import pytest -from tests.utils import cleanup +from tests.utils import * from libp2p import new_node from libp2p.network.notifee_interface import INotifee @@ -65,26 +65,9 @@ class InvalidNotifee(): async def listen(self): assert False -async def perform_two_host_simple_set_up(): - node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) - node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) - - async def my_stream_handler(stream): - while True: - read_string = (await stream.read()).decode() - - resp = "ack:" + read_string - await stream.write(resp.encode()) - - node_b.set_stream_handler("/echo/1.0.0", my_stream_handler) - - # 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) - return node_a, node_b - -async def perform_two_host_simple_set_up_custom_handler(handler): - node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) - node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) +async def perform_two_host_set_up_custom_handler(handler): + transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]] + (node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list) node_b.set_stream_handler("/echo/1.0.0", handler) @@ -94,7 +77,7 @@ async def perform_two_host_simple_set_up_custom_handler(handler): @pytest.mark.asyncio async def test_one_notifier(): - node_a, node_b = await perform_two_host_simple_set_up() + node_a, node_b = await perform_two_host_set_up_custom_handler(echo_stream_handler) # Add notifee for node_a events = [] @@ -135,7 +118,7 @@ async def test_one_notifier_on_two_nodes(): resp = "ack:" + read_string await stream.write(resp.encode()) - node_a, node_b = await perform_two_host_simple_set_up_custom_handler(my_stream_handler) + node_a, node_b = await perform_two_host_set_up_custom_handler(my_stream_handler) # Add notifee for node_a events_a = [] @@ -165,7 +148,7 @@ async def test_one_notifier_on_two_nodes(): @pytest.mark.asyncio async def test_two_notifiers(): - node_a, node_b = await perform_two_host_simple_set_up() + node_a, node_b = await perform_two_host_set_up_custom_handler(echo_stream_handler) # Add notifee for node_a events0 = [] @@ -198,7 +181,7 @@ async def test_two_notifiers(): async def test_ten_notifiers(): num_notifiers = 10 - node_a, node_b = await perform_two_host_simple_set_up() + node_a, node_b = await perform_two_host_set_up_custom_handler(echo_stream_handler) # Add notifee for node_a events_lst = [] @@ -244,7 +227,7 @@ async def test_ten_notifiers_on_two_nodes(): resp = "ack:" + read_string await stream.write(resp.encode()) - node_a, node_b = await perform_two_host_simple_set_up_custom_handler(my_stream_handler) + node_a, node_b = await perform_two_host_set_up_custom_handler(my_stream_handler) # Add notifee for node_a and node_b events_lst_a = [] @@ -278,7 +261,7 @@ async def test_ten_notifiers_on_two_nodes(): async def test_invalid_notifee(): num_notifiers = 10 - node_a, node_b = await perform_two_host_simple_set_up() + node_a, node_b = await perform_two_host_set_up_custom_handler(echo_stream_handler) # Add notifee for node_a events_lst = [] diff --git a/tests/protocol_muxer/test_protocol_muxer.py b/tests/protocol_muxer/test_protocol_muxer.py index a7e19af..1eff211 100644 --- a/tests/protocol_muxer/test_protocol_muxer.py +++ b/tests/protocol_muxer/test_protocol_muxer.py @@ -1,6 +1,6 @@ import pytest -from tests.utils import cleanup +from tests.utils import cleanup, set_up_nodes_by_transport_opt from libp2p import new_node from libp2p.protocol_muxer.multiselect_client import MultiselectClientError @@ -15,12 +15,8 @@ from libp2p.protocol_muxer.multiselect_client import MultiselectClientError async def perform_simple_test(expected_selected_protocol, protocols_for_client, protocols_with_handlers): - transport_opt_a = ["/ip4/127.0.0.1/tcp/0"] - transport_opt_b = ["/ip4/127.0.0.1/tcp/0"] - node_a = await new_node( - transport_opt=transport_opt_a) - node_b = await new_node( - transport_opt=transport_opt_b) + transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]] + (node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list) async def stream_handler(stream): while True: