Skip to content

Commit

Permalink
fixing a bug in the subscribe all function and adding some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CameronDevine committed Oct 16, 2024
1 parent b74ce82 commit 23b56b3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pigeon/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ def subscribe_all(self, callback: Callable, include_core=False):

# Additional logic here is to avoid subscribe_all changing behavior and always subscribing to core topics.
for topic in self._topics:
if topic in messages.topics and not include_core:
if topic in messages.core_topics and not include_core:
continue
if topic is "&_request_state":
if topic == "&_request_state":
continue
self.subscribe(topic, callback, send_update=False)
self._update_state()
Expand Down
30 changes: 29 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MockMessage(BaseMessage):
@pytest.fixture
def pigeon_client():
with patch("pigeon.utils.setup_logging") as mock_logging:
topics = {"topic1": MockMessage}
topics = {"topic1": MockMessage, "topic2": MockMessage}
client = Pigeon(
"test",
host="localhost",
Expand Down Expand Up @@ -167,6 +167,34 @@ def test_subscribe_no_such_topic(pigeon_client, topic):
pigeon_client.subscribe(topic, callback)


def test_subscribe_all(pigeon_client, mocker):
pigeon_client._connection = mocker.MagicMock()
pigeon_client.subscribe = mocker.MagicMock()

callback = mocker.MagicMock()

pigeon_client.subscribe_all(callback)

assert len(pigeon_client.subscribe.mock_calls) == 2
pigeon_client.subscribe.assert_any_call("topic1", callback, send_update=False)
pigeon_client.subscribe.assert_any_call("topic2", callback, send_update=False)


def test_subscribe_all_with_core(pigeon_client, mocker):
pigeon_client._connection = mocker.MagicMock()
pigeon_client.subscribe = mocker.MagicMock()

callback = mocker.MagicMock()

pigeon_client.subscribe_all(callback, include_core=True)

assert len(pigeon_client.subscribe.mock_calls) == 4
pigeon_client.subscribe.assert_any_call("topic1", callback, send_update=False)
pigeon_client.subscribe.assert_any_call("topic2", callback, send_update=False)
pigeon_client.subscribe.assert_any_call("&_announce_connection", callback, send_update=False)
pigeon_client.subscribe.assert_any_call("&_update_state", callback, send_update=False)


@pytest.mark.parametrize(
"topic, expected_log",
[
Expand Down

0 comments on commit 23b56b3

Please sign in to comment.