From b340b8981985a906bbb97b9a080d9688650425b1 Mon Sep 17 00:00:00 2001 From: Stuckinaboot Date: Sun, 3 Mar 2019 23:44:15 +1100 Subject: [PATCH] Refactor test setup to remove duplicate code --- tests/libp2p/test_notify.py | 38 ++++++++----------------------------- 1 file changed, 8 insertions(+), 30 deletions(-) diff --git a/tests/libp2p/test_notify.py b/tests/libp2p/test_notify.py index 3021cf1..80dd6c1 100644 --- a/tests/libp2p/test_notify.py +++ b/tests/libp2p/test_notify.py @@ -45,8 +45,7 @@ class MyNotifee(INotifee): async def listen_close(self, network, multiaddr): pass -@pytest.mark.asyncio -async def test_one_notifier(): +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"]) @@ -61,6 +60,11 @@ async def test_one_notifier(): # 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 + +@pytest.mark.asyncio +async def test_one_notifier(): + node_a, node_b = await perform_two_host_simple_set_up() # Add notifee for node_a events = [] @@ -87,20 +91,7 @@ async def test_one_notifier(): @pytest.mark.asyncio async def test_two_notifiers(): - 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 stream_handler(stream): - while True: - read_string = (await stream.read()).decode() - - response = "ack:" + read_string - await stream.write(response.encode()) - - node_b.set_stream_handler("/echo/1.0.0", 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) + node_a, node_b = await perform_two_host_simple_set_up() # Add notifee for node_a events0 = [] @@ -133,20 +124,7 @@ async def test_two_notifiers(): async def test_ten_notifiers(): num_notifiers = 10 - 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 stream_handler(stream): - while True: - read_string = (await stream.read()).decode() - - response = "ack:" + read_string - await stream.write(response.encode()) - - node_b.set_stream_handler("/echo/1.0.0", 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) + node_a, node_b = await perform_two_host_simple_set_up() # Add notifee for node_a events_lst = []