Skip to content

Commit 84e7cf2

Browse files
authored
catch UnicodeException (#100)
* catch UnicodeException * add tests
1 parent 5fe98ce commit 84e7cf2

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

.devcontainer/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#
1313
# SPDX-License-Identifier: Apache-2.0
1414

15-
FROM ghcr.io/eclipse-velocitas/devcontainer-base-images/python:v0.1.2
15+
FROM ghcr.io/eclipse-velocitas/devcontainer-base-images/python:v0.1
1616

1717
# Force dapr to use localhost traffic
1818
ENV DAPR_HOST_IP="127.0.0.1"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
git+https://github.com/eclipse-velocitas/[email protected].1
1+
git+https://github.com/eclipse-velocitas/[email protected].2

sdv/native/mqtt.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ async def subscribe_topic(self, topic, coro):
7575

7676
@self._sub_client.topic_callback(topic)
7777
def handle(client, userdata, msg):
78-
message = str(msg.payload.decode("utf-8"))
78+
try:
79+
message = str(msg.payload.decode("utf-8"))
80+
except UnicodeDecodeError as err:
81+
logger.error(err)
82+
return
7983
if asyncio.iscoroutinefunction(coro):
8084
# run the async callbacks on the main event loop
8185
asyncio.run_coroutine_threadsafe(coro(message), loop)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070

7171
setup(
7272
name="sdv",
73-
version="0.10.1",
73+
version="0.10.2",
7474
description="A Python SDK for Vehicle app",
7575
long_description=long_description,
7676
long_description_content_type="text/markdown",

tests/unit/native_pusbub_client_test.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
""" Tests for methods in PubSubClient """
1616
import os
17+
import time
1718

1819
os.environ["SDV_MIDDLEWARE_TYPE"] = "native"
1920

@@ -61,5 +62,43 @@ async def test_for_get_publish_event():
6162
mocked_client.assert_called_once_with("/test/native", "message")
6263

6364

65+
@pytest.mark.asyncio
66+
async def test_for_subscribe_mqtt_event():
67+
middleware = get_middleware_instance()
68+
mqtt_client = middleware.pubsub_client
69+
callback = CallbackClass()
70+
await middleware.start()
71+
await mqtt_client.run()
72+
await mqtt_client.subscribe_topic("test/test_subscribe", callback)
73+
# wait a moment to really subscribe
74+
time.sleep(0.5)
75+
await mqtt_client.publish_event("test/test_subscribe", "test")
76+
77+
time.sleep(1)
78+
assert callback.executed
79+
80+
81+
@pytest.mark.asyncio
82+
async def test_for_error_message():
83+
middleware = get_middleware_instance()
84+
mqtt_client = middleware.pubsub_client
85+
callback = CallbackClass()
86+
await middleware.start()
87+
await mqtt_client.run()
88+
await mqtt_client.subscribe_topic("test/test_error", callback)
89+
# wait a moment to really subscribe
90+
time.sleep(0.5)
91+
await mqtt_client.publish_event("test/test_error", b"\xc3")
92+
assert not callback.executed
93+
94+
6495
def get_middleware_instance() -> Middleware:
6596
return config.middleware
97+
98+
99+
class CallbackClass:
100+
def __init__(self):
101+
self.executed = False
102+
103+
def __call__(self, message):
104+
self.executed = True

0 commit comments

Comments
 (0)