Skip to content

Commit

Permalink
More tests on topic validation ++
Browse files Browse the repository at this point in the history
  • Loading branch information
klpanagi committed Jan 10, 2025
1 parent 8c718b3 commit fb9fb49
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
7 changes: 5 additions & 2 deletions commlib/transports/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
)
from commlib.compression import CompressionType, deflate, inflate_str
from commlib.connection import BaseConnectionParameters
from commlib.exceptions import RPCClientTimeoutError, RPCRequestError
from commlib.exceptions import RPCClientTimeoutError, RPCRequestError, SubscriberError
from commlib.msg import PubSubMessage, RPCMessage
from commlib.pubsub import TOPIC_PATTERN_REGEX, TOPIC_REGEX, BasePublisher, BaseSubscriber, validate_pubsub_topic, validate_pubsub_topic_strict
from commlib.rpc import (
Expand Down Expand Up @@ -564,7 +564,10 @@ def run_forever(self):
"""
self._transport.start()
for topic, callback in self._subs.items():
self._transport.subscribe(topic, functools.partial(self._on_message, callback))
try:
self._transport.subscribe(topic, functools.partial(self._on_message, callback))
except Exception:
raise SubscriberError(f"<{self.__class__.__name__}> Failed to subscribe to topic {self.topic}", exc_info=True)
while True:
if self._t_stop_event is not None:
if self._t_stop_event.is_set():
Expand Down
19 changes: 14 additions & 5 deletions tests/mqtt/test_mqtt_pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,17 @@ def test_subscriber_strict_topic(self):
node = Node(node_name='test_node',
connection_params=self.connparams,
heartbeats=False,
debug=True)
debug=False)
try:
_ = node.create_subscriber(msg_type=SonarMessage,
topic='sonar.front',
on_message=lambda msg: print(msg))
topic='sonar.front',
on_message=lambda msg: print(msg))
except ValueError as e:
self.fail(str(e))
try:
_ = node.create_subscriber(msg_type=SonarMessage,
topic='sonar.front.123',
on_message=lambda msg: print(msg))
except ValueError as e:
self.fail(str(e))
try:
Expand Down Expand Up @@ -124,13 +130,16 @@ def test_wsubscriber_strict_topic(self):
node = Node(node_name='test_node',
connection_params=self.connparams,
heartbeats=False,
debug=True)
debug=False)
sub = node.create_wsubscriber(msg_type=SonarMessage)
try:

sub.subscribe('sonar.front', lambda msg: print(msg))
except ValueError as e:
self.fail(str(e))
try:
sub.subscribe('sonar.front.123', lambda msg: print(msg))
except ValueError as e:
self.fail(str(e))
try:
sub.subscribe('sonar.front.*', lambda msg: print(msg))
except ValueError as e:
Expand Down
7 changes: 3 additions & 4 deletions tests/redis/test_redis_pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ def setUp(self):
"""Set up test fixtures, if any."""
self.connparams = ConnectionParameters(
host="localhost", port="6379", db=0,
username="", password="",
)
username="", password="", socket_timeout=None)

def tearDown(self):
"""Tear down test fixtures, if any."""
Expand All @@ -62,7 +61,7 @@ def test_subscriber_strict_topic(self):
node = Node(node_name='test_node',
connection_params=self.connparams,
heartbeats=False,
debug=True)
debug=False)
try:
_ = node.create_subscriber(msg_type=SonarMessage,
topic='sonar.front',
Expand Down Expand Up @@ -124,7 +123,7 @@ def test_wsubscriber_strict_topic(self):
node = Node(node_name='test_node',
connection_params=self.connparams,
heartbeats=False,
debug=True)
debug=False)
sub = node.create_wsubscriber(msg_type=SonarMessage)
try:

Expand Down

0 comments on commit fb9fb49

Please sign in to comment.