Skip to content

Commit 6919c68

Browse files
committed
Ignore messages with other topics in handle_msg
1 parent 8d8b5a1 commit 6919c68

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

mqterm/terminal.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,13 @@ async def disconnect(self):
6464
"""Stop processing messages in the input stream."""
6565
await self.mqtt_client.unsubscribe(self.in_topic)
6666

67-
async def handle_msg(self, _topic, msg, properties={}):
67+
async def handle_msg(self, topic, msg, properties={}):
6868
"""Process a single MQTT message and apply to the appropriate job."""
69+
topic = topic.decode("utf-8")
70+
if not topic.startswith(self.in_topic):
71+
self.logger.debug(f"Terminal received message on {topic}; ignoring")
72+
return
73+
6974
client_id = parse_client_id(properties)
7075
seq = parse_seq(properties)
7176
try:

tests/test_terminal.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def output(self):
3131

3232
class TestMqttTerminal(TestCase):
3333
def setUp(self):
34-
self.in_topic = "/tty/in"
34+
self.in_topic = b"/tty/in"
3535
self.mqtt_client = Mock()
3636
self.mqtt_client.publish = Mock()
3737
self.term = MqttTerminal(self.mqtt_client)
@@ -64,6 +64,16 @@ def test_handle_msg_bad_seq(self):
6464
self.term.handle_msg(self.in_topic, payload.encode("utf-8"), props)
6565
)
6666

67+
def test_handle_msg_wrong_topic(self):
68+
"""MqttTerminal should ignore messages on wrong topic"""
69+
payload = "get_file file.txt"
70+
topic = b"/wrong/topic"
71+
self.term.update_job = AsyncMock()
72+
asyncio.run(
73+
self.term.handle_msg(topic, payload.encode("utf-8"), format_properties("localhost", -1))
74+
)
75+
self.term.update_job.assert_not_awaited()
76+
6777
def test_handle_msg(self):
6878
"""MqttTerminal should parse MQTT messages to update jobs"""
6979
self.term.update_job = AsyncMock()

tests/utils.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@ def __init__(self):
1414
def __call__(self, *args, **kwargs):
1515
self._calls.append(call(*args, **kwargs))
1616

17+
def assert_called(self):
18+
"""Assert that the mock was called at least once."""
19+
assert len(self._calls) > 0, "Expected mock to be called, but it was not."
20+
21+
def assert_not_called(self):
22+
"""Assert that the mock was not called."""
23+
assert len(self._calls) == 0, "Expected mock to not be called, but it was."
24+
1725
def assert_called_with(self, *args, **kwargs):
26+
"""Assert that the mock was last called with the given arguments."""
1827
# First call should be self, so we prepend it
1928
expected_args = [self] + list(args)
2029
expectation = call(*expected_args, **kwargs)
@@ -24,15 +33,31 @@ def assert_called_with(self, *args, **kwargs):
2433
expectation, self._calls[-1]
2534
)
2635

36+
def assert_has_calls(self, calls):
37+
"""Assert that the mock has the expected calls with arguments."""
38+
assert self._calls == calls, "Expected calls {}, got {}".format(
39+
calls, self._calls
40+
)
41+
2742

2843
class AsyncMock(Mock):
2944
"""An async version of Mock that can be awaited."""
3045

3146
async def __call__(self, *args, **kwargs):
3247
return super().__call__(self, *args, **kwargs)
3348

49+
def assert_awaited(self):
50+
"""Assert that the async mock was awaited at least once."""
51+
return super().assert_called()
52+
53+
def assert_not_awaited(self):
54+
"""Assert that the async mock was not awaited."""
55+
return super().assert_not_called()
56+
3457
def assert_awaited_with(self, *args, **kwargs):
58+
"""Assert that the async mock was last awaited with the given arguments."""
3559
return super().assert_called_with(*args, **kwargs)
3660

3761
def assert_has_awaits(self, awaits):
38-
assert self._calls == awaits
62+
"""Assert that the async mock has the expected awaits with arguments."""
63+
return super().assert_has_calls(awaits)

0 commit comments

Comments
 (0)