Skip to content

Commit

Permalink
catch UnicodeException (#100)
Browse files Browse the repository at this point in the history
* catch UnicodeException

* add tests
  • Loading branch information
MP91 authored Aug 30, 2023
1 parent 5fe98ce commit 84e7cf2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#
# SPDX-License-Identifier: Apache-2.0

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

# Force dapr to use localhost traffic
ENV DAPR_HOST_IP="127.0.0.1"
Expand Down
2 changes: 1 addition & 1 deletion examples/seat-adjuster/requirements-links.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
git+https://github.com/eclipse-velocitas/vehicle-app-python-sdk.git@v0.10.1
git+https://github.com/eclipse-velocitas/vehicle-app-python-sdk.git@v0.10.2
6 changes: 5 additions & 1 deletion sdv/native/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ async def subscribe_topic(self, topic, coro):

@self._sub_client.topic_callback(topic)
def handle(client, userdata, msg):
message = str(msg.payload.decode("utf-8"))
try:
message = str(msg.payload.decode("utf-8"))
except UnicodeDecodeError as err:
logger.error(err)
return
if asyncio.iscoroutinefunction(coro):
# run the async callbacks on the main event loop
asyncio.run_coroutine_threadsafe(coro(message), loop)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@

setup(
name="sdv",
version="0.10.1",
version="0.10.2",
description="A Python SDK for Vehicle app",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/native_pusbub_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

""" Tests for methods in PubSubClient """
import os
import time

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

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


@pytest.mark.asyncio
async def test_for_subscribe_mqtt_event():
middleware = get_middleware_instance()
mqtt_client = middleware.pubsub_client
callback = CallbackClass()
await middleware.start()
await mqtt_client.run()
await mqtt_client.subscribe_topic("test/test_subscribe", callback)
# wait a moment to really subscribe
time.sleep(0.5)
await mqtt_client.publish_event("test/test_subscribe", "test")

time.sleep(1)
assert callback.executed


@pytest.mark.asyncio
async def test_for_error_message():
middleware = get_middleware_instance()
mqtt_client = middleware.pubsub_client
callback = CallbackClass()
await middleware.start()
await mqtt_client.run()
await mqtt_client.subscribe_topic("test/test_error", callback)
# wait a moment to really subscribe
time.sleep(0.5)
await mqtt_client.publish_event("test/test_error", b"\xc3")
assert not callback.executed


def get_middleware_instance() -> Middleware:
return config.middleware


class CallbackClass:
def __init__(self):
self.executed = False

def __call__(self, message):
self.executed = True

0 comments on commit 84e7cf2

Please sign in to comment.