From bb0da41edafb12ac076a4b2314bd2a305eaea711 Mon Sep 17 00:00:00 2001 From: mhchia Date: Mon, 9 Sep 2019 23:09:33 +0800 Subject: [PATCH] Remove `cleanup` `cleanup` cancels all tasks in the loop, including the main one run by `run_until_complete` --- tests/examples/test_chat.py | 4 +--- tests/libp2p/test_libp2p.py | 9 +-------- tests/libp2p/test_notify.py | 9 +-------- tests/protocol_muxer/test_protocol_muxer.py | 5 +---- .../floodsub_integration_test_settings.py | 3 +-- tests/pubsub/test_dummyaccount_demo.py | 3 +-- tests/pubsub/test_floodsub.py | 4 +--- tests/pubsub/test_gossipsub.py | 17 +---------------- tests/security/test_security_multistream.py | 3 +-- tests/utils.py | 17 ----------------- 10 files changed, 9 insertions(+), 65 deletions(-) diff --git a/tests/examples/test_chat.py b/tests/examples/test_chat.py index 75d8ec7..18a172c 100644 --- a/tests/examples/test_chat.py +++ b/tests/examples/test_chat.py @@ -4,7 +4,7 @@ import pytest from libp2p.peer.peerinfo import info_from_p2p_addr from libp2p.protocol_muxer.exceptions import MultiselectClientError -from tests.utils import cleanup, set_up_nodes_by_transport_opt +from tests.utils import set_up_nodes_by_transport_opt PROTOCOL_ID = "/chat/1.0.0" @@ -101,5 +101,3 @@ async def test_chat(test): await host_b.connect(info) await test(host_a, host_b) - - await cleanup() diff --git a/tests/libp2p/test_libp2p.py b/tests/libp2p/test_libp2p.py index 8090f5e..793444c 100644 --- a/tests/libp2p/test_libp2p.py +++ b/tests/libp2p/test_libp2p.py @@ -3,7 +3,7 @@ import pytest from libp2p.peer.peerinfo import info_from_p2p_addr from tests.constants import MAX_READ_LEN -from tests.utils import cleanup, set_up_nodes_by_transport_opt +from tests.utils import set_up_nodes_by_transport_opt @pytest.mark.asyncio @@ -34,7 +34,6 @@ async def test_simple_messages(): assert response == ("ack:" + message) # Success, terminate pending tasks. - await cleanup() @pytest.mark.asyncio @@ -69,7 +68,6 @@ async def test_double_response(): assert response2 == ("ack2:" + message) # Success, terminate pending tasks. - await cleanup() @pytest.mark.asyncio @@ -120,7 +118,6 @@ async def test_multiple_streams(): ) # Success, terminate pending tasks. - await cleanup() @pytest.mark.asyncio @@ -183,7 +180,6 @@ async def test_multiple_streams_same_initiator_different_protocols(): ) # Success, terminate pending tasks. - await cleanup() @pytest.mark.asyncio @@ -264,7 +260,6 @@ async def test_multiple_streams_two_initiators(): ) # Success, terminate pending tasks. - await cleanup() @pytest.mark.asyncio @@ -326,7 +321,6 @@ async def test_triangle_nodes_connection(): assert response == ("ack:" + message) # Success, terminate pending tasks. - await cleanup() @pytest.mark.asyncio @@ -353,4 +347,3 @@ async def test_host_connect(): assert addr.encapsulate(ma_node_b) in node_b.get_addrs() # Success, terminate pending tasks. - await cleanup() diff --git a/tests/libp2p/test_notify.py b/tests/libp2p/test_notify.py index e21030a..b9a8707 100644 --- a/tests/libp2p/test_notify.py +++ b/tests/libp2p/test_notify.py @@ -17,7 +17,7 @@ from libp2p.crypto.rsa import create_new_key_pair from libp2p.host.basic_host import BasicHost from libp2p.network.notifee_interface import INotifee from tests.constants import MAX_READ_LEN -from tests.utils import cleanup, perform_two_host_set_up +from tests.utils import perform_two_host_set_up ACK = "ack:" @@ -91,7 +91,6 @@ async def test_one_notifier(): assert response == expected_resp # Success, terminate pending tasks. - await cleanup() @pytest.mark.asyncio @@ -138,7 +137,6 @@ async def test_one_notifier_on_two_nodes(): assert response == expected_resp # Success, terminate pending tasks. - await cleanup() @pytest.mark.asyncio @@ -203,7 +201,6 @@ async def test_one_notifier_on_two_nodes_with_listen(): assert response == expected_resp # Success, terminate pending tasks. - await cleanup() @pytest.mark.asyncio @@ -235,7 +232,6 @@ async def test_two_notifiers(): assert response == expected_resp # Success, terminate pending tasks. - await cleanup() @pytest.mark.asyncio @@ -271,7 +267,6 @@ async def test_ten_notifiers(): assert response == expected_resp # Success, terminate pending tasks. - await cleanup() @pytest.mark.asyncio @@ -325,7 +320,6 @@ async def test_ten_notifiers_on_two_nodes(): assert response == expected_resp # Success, terminate pending tasks. - await cleanup() @pytest.mark.asyncio @@ -355,4 +349,3 @@ async def test_invalid_notifee(): assert response == expected_resp # Success, terminate pending tasks. - await cleanup() diff --git a/tests/protocol_muxer/test_protocol_muxer.py b/tests/protocol_muxer/test_protocol_muxer.py index 7830aaa..d7523ac 100644 --- a/tests/protocol_muxer/test_protocol_muxer.py +++ b/tests/protocol_muxer/test_protocol_muxer.py @@ -1,7 +1,7 @@ import pytest from libp2p.protocol_muxer.exceptions import MultiselectClientError -from tests.utils import cleanup, echo_stream_handler, set_up_nodes_by_transport_opt +from tests.utils import echo_stream_handler, set_up_nodes_by_transport_opt # TODO: Add tests for multiple streams being opened on different # protocols through the same connection @@ -35,7 +35,6 @@ async def perform_simple_test( assert expected_selected_protocol == stream.get_protocol() # Success, terminate pending tasks. - await cleanup() @pytest.mark.asyncio @@ -52,7 +51,6 @@ async def test_single_protocol_fails(): await perform_simple_test("", ["/echo/1.0.0"], ["/potato/1.0.0"]) # Cleanup not reached on error - await cleanup() @pytest.mark.asyncio @@ -83,4 +81,3 @@ async def test_multiple_protocol_fails(): await perform_simple_test("", protocols_for_client, protocols_for_listener) # Cleanup not reached on error - await cleanup() diff --git a/tests/pubsub/floodsub_integration_test_settings.py b/tests/pubsub/floodsub_integration_test_settings.py index d96fc2b..0a533e2 100644 --- a/tests/pubsub/floodsub_integration_test_settings.py +++ b/tests/pubsub/floodsub_integration_test_settings.py @@ -4,7 +4,7 @@ import pytest from tests.configs import LISTEN_MADDR from tests.factories import PubsubFactory -from tests.utils import cleanup, connect +from tests.utils import connect from .configs import FLOODSUB_PROTOCOL_ID @@ -258,4 +258,3 @@ async def perform_test_from_obj(obj, router_factory): assert node_map[origin_node_id].get_id().to_bytes() == msg.from_id # Success, terminate pending tasks. - await cleanup() diff --git a/tests/pubsub/test_dummyaccount_demo.py b/tests/pubsub/test_dummyaccount_demo.py index b365134..edc2f51 100644 --- a/tests/pubsub/test_dummyaccount_demo.py +++ b/tests/pubsub/test_dummyaccount_demo.py @@ -3,7 +3,7 @@ from threading import Thread import pytest -from tests.utils import cleanup, connect +from tests.utils import connect from .dummy_account_node import DummyAccountNode @@ -64,7 +64,6 @@ async def perform_test(num_nodes, adjacency_map, action_func, assertion_func): assertion_func(dummy_node) # Success, terminate pending tasks. - await cleanup() @pytest.mark.asyncio diff --git a/tests/pubsub/test_floodsub.py b/tests/pubsub/test_floodsub.py index 7e079d1..c6d28bf 100644 --- a/tests/pubsub/test_floodsub.py +++ b/tests/pubsub/test_floodsub.py @@ -4,7 +4,7 @@ import pytest from libp2p.peer.id import ID from tests.factories import FloodsubFactory -from tests.utils import cleanup, connect +from tests.utils import connect from .floodsub_integration_test_settings import ( floodsub_protocol_pytest_params, @@ -36,7 +36,6 @@ async def test_simple_two_nodes(pubsubs_fsub): assert res_b.topicIDs == [topic] # Success, terminate pending tasks. - await cleanup() # Initialize Pubsub with a cache_size of 4 @@ -82,7 +81,6 @@ async def test_lru_cache_two_nodes(pubsubs_fsub, monkeypatch): assert sub_b.empty() # Success, terminate pending tasks. - await cleanup() @pytest.mark.parametrize("test_case_obj", floodsub_protocol_pytest_params) diff --git a/tests/pubsub/test_gossipsub.py b/tests/pubsub/test_gossipsub.py index 7a0efc2..95775be 100644 --- a/tests/pubsub/test_gossipsub.py +++ b/tests/pubsub/test_gossipsub.py @@ -3,7 +3,7 @@ import random import pytest -from tests.utils import cleanup, connect +from tests.utils import connect from .configs import GossipsubParams from .utils import dense_connect, one_to_all_connect @@ -61,8 +61,6 @@ async def test_join(num_hosts, hosts, pubsubs_gsub): assert hosts[i].get_id() not in gossipsubs[central_node_index].mesh[topic] assert topic not in gossipsubs[i].mesh - await cleanup() - @pytest.mark.parametrize("num_hosts", (1,)) @pytest.mark.asyncio @@ -81,8 +79,6 @@ async def test_leave(pubsubs_gsub): # Test re-leave await gossipsub.leave(topic) - await cleanup() - @pytest.mark.parametrize("num_hosts", (2,)) @pytest.mark.asyncio @@ -133,8 +129,6 @@ async def test_handle_graft(pubsubs_gsub, hosts, event_loop, monkeypatch): # Check that bob is now alice's mesh peer assert id_bob in gossipsubs[index_alice].mesh[topic] - await cleanup() - @pytest.mark.parametrize( "num_hosts, gossipsub_params", ((2, GossipsubParams(heartbeat_interval=3)),) @@ -174,8 +168,6 @@ async def test_handle_prune(pubsubs_gsub, hosts): assert id_alice not in gossipsubs[index_bob].mesh[topic] assert id_bob in gossipsubs[index_alice].mesh[topic] - await cleanup() - @pytest.mark.parametrize("num_hosts", (10,)) @pytest.mark.asyncio @@ -210,7 +202,6 @@ async def test_dense(num_hosts, pubsubs_gsub, hosts): for queue in queues: msg = await queue.get() assert msg.data == msg_content - await cleanup() @pytest.mark.parametrize("num_hosts", (10,)) @@ -268,8 +259,6 @@ async def test_fanout(hosts, pubsubs_gsub): msg = await queue.get() assert msg.data == msg_content - await cleanup() - @pytest.mark.parametrize("num_hosts", (10,)) @pytest.mark.asyncio @@ -340,8 +329,6 @@ async def test_fanout_maintenance(hosts, pubsubs_gsub): msg = await queue.get() assert msg.data == msg_content - await cleanup() - @pytest.mark.parametrize( "num_hosts, gossipsub_params", @@ -380,5 +367,3 @@ async def test_gossip_propagation(hosts, pubsubs_gsub): # should be able to read message msg = await queue_1.get() assert msg.data == msg_content - - await cleanup() diff --git a/tests/security/test_security_multistream.py b/tests/security/test_security_multistream.py index 1d87e7b..c8e83c1 100644 --- a/tests/security/test_security_multistream.py +++ b/tests/security/test_security_multistream.py @@ -6,7 +6,7 @@ from libp2p import new_node from libp2p.crypto.rsa import create_new_key_pair from libp2p.security.insecure.transport import InsecureSession, InsecureTransport from tests.configs import LISTEN_MADDR -from tests.utils import cleanup, connect +from tests.utils import connect # TODO: Add tests for multiple streams being opened on different # protocols through the same connection @@ -57,7 +57,6 @@ async def perform_simple_test( assertion_func(node2_conn.secured_conn) # Success, terminate pending tasks. - await cleanup() @pytest.mark.asyncio diff --git a/tests/utils.py b/tests/utils.py index 8ae72d6..e9d6c09 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,6 +1,3 @@ -import asyncio -from contextlib import suppress - import multiaddr from libp2p import new_node @@ -17,20 +14,6 @@ async def connect(node1, node2): await node1.connect(info) -# FIXME: Should be deprecated, since it also kills the main task. -async def cleanup(): - pending = asyncio.all_tasks() - for task in pending: - task.cancel() - - # Now we should await task to execute it's cancellation. - # Cancelled task raises asyncio.CancelledError that we can suppress: - # NOTE: Changed from `asyncio.CancelledError` to `Exception`, to suppress all exceptions - # including the one in `run_until_complete`. - with suppress(Exception): - await task - - async def set_up_nodes_by_transport_opt(transport_opt_list): nodes_list = [] for transport_opt in transport_opt_list: