From 183eee0e85506499329e5f19a5521bff97fc34f7 Mon Sep 17 00:00:00 2001 From: NIC619 Date: Thu, 18 Jul 2019 13:39:22 +0800 Subject: [PATCH] Add self subscription tests --- tests/pubsub/test_subscription.py | 63 +++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/pubsub/test_subscription.py diff --git a/tests/pubsub/test_subscription.py b/tests/pubsub/test_subscription.py new file mode 100644 index 0000000..c8f46f5 --- /dev/null +++ b/tests/pubsub/test_subscription.py @@ -0,0 +1,63 @@ +# pylint: disable=redefined-outer-name +import pytest + +from libp2p import new_node +from libp2p.pubsub.pubsub import Pubsub +from libp2p.pubsub.floodsub import FloodSub + +SUPPORTED_PUBSUB_PROTOCOLS = ["/floodsub/1.0.0"] +TESTING_TOPIC = "TEST_SUBSCRIBE" + + +class NoConnNode: + # pylint: disable=too-few-public-methods + + def __init__(self, host, pubsub): + self.host = host + self.pubsub = pubsub + + @classmethod + async def create(cls): + host = await new_node() + floodsub = FloodSub(SUPPORTED_PUBSUB_PROTOCOLS) + pubsub = Pubsub(host, floodsub, "test") + return cls(host, pubsub) + + +@pytest.fixture +async def node(): + return await NoConnNode.create() + + +@pytest.mark.asyncio +async def test_subscribe_unsubscribe(node): + await node.pubsub.subscribe(TESTING_TOPIC) + assert TESTING_TOPIC in node.pubsub.my_topics + + await node.pubsub.unsubscribe(TESTING_TOPIC) + assert TESTING_TOPIC not in node.pubsub.my_topics + + +@pytest.mark.asyncio +async def test_re_subscribe(node): + await node.pubsub.subscribe(TESTING_TOPIC) + assert TESTING_TOPIC in node.pubsub.my_topics + + await node.pubsub.subscribe(TESTING_TOPIC) + assert TESTING_TOPIC in node.pubsub.my_topics + + +@pytest.mark.asyncio +async def test_re_unsubscribe(node): + # Unsubscribe from topic we didn't even subscribe to + assert "NOT_MY_TOPIC" not in node.pubsub.my_topics + await node.pubsub.unsubscribe("NOT_MY_TOPIC") + + await node.pubsub.subscribe(TESTING_TOPIC) + assert TESTING_TOPIC in node.pubsub.my_topics + + await node.pubsub.unsubscribe(TESTING_TOPIC) + assert TESTING_TOPIC not in node.pubsub.my_topics + + await node.pubsub.unsubscribe(TESTING_TOPIC) + assert TESTING_TOPIC not in node.pubsub.my_topics