From 7bbecc4d3fd9b266c7bc6fbd69bced97f1258d21 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 08:50:32 -0400 Subject: [PATCH] Common: Only send event to `subscribe_all()` subscribers once --- monkey/common/event_queue/pypubsub_event_queue.py | 8 +------- .../common/event_queue/test_pypubsub_event_queue.py | 12 +++++------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index efac70881..053d7bdca 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -13,14 +13,8 @@ class PyPubSubEventQueue(IEventQueue): self._pypubsub_publisher = pypubsub_publisher def subscribe_all(self, subscriber: Callable[[AbstractEvent], None]): - # From the documentation (https://pypubsub.readthedocs.io/en/v4.0.3/usage/ - # usage_advanced_debug.html#listen-for-messages-from-all-topics): - # "PyPubSub defines a special topic named pub.ALL_TOPICS. A listener that subscribes - # to this topic will receives all messages of every topic. By default, the listener - # will not receive any data since pub.ALL_TOPICS is the parent of all root topics (...)" - self._pypubsub_publisher.subscribe( - listener=subscriber, topicName=self._pypubsub_publisher.ALL_TOPICS + listener=subscriber, topicName=INTERNAL_ALL_EVENT_TYPES_TOPIC ) def subscribe_type( diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index 867673588..bedaf0c44 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -64,18 +64,16 @@ def test_topic_subscription(subscriber_1, subscriber_2, subscriber_1_calls, subs def test_subscribe_all(): subscriber_calls = [] - def subscriber(topic=pub.AUTO_TOPIC): + def subscriber(event, topic=pub.AUTO_TOPIC): subscriber_calls.append(topic.getName()) pypubsub_event_queue.subscribe_all(subscriber) pypubsub_event_queue.publish(EventType) - assert subscriber_calls == [ - EventType.__name__, - INTERNAL_ALL_EVENT_TYPES_TOPIC, - EVENT_TAG_1, - EVENT_TAG_2, - ] + assert len(subscriber_calls) == 1 + assert EventType.__name__ not in subscriber_calls + assert EVENT_TAG_1 not in subscriber_calls + assert EVENT_TAG_2 not in subscriber_calls @pytest.mark.usefixtures("subscriber_1", "subscriber_1_calls")