Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: com aux able to receive timestamps #466

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/whats_new/version_ongoing.rst
@@ -1,6 +1,12 @@
Version ongoing
---------------

Communication Auxiliary
^^^^^^^^^^^^^^^^^^^^^^^

Receive method of the Communication Auxiliary can now also return the message timestamp
if the corresponding parameter is set to True.

PCAN Connector
^^^^^^^^^^^^^^

Expand Down
11 changes: 8 additions & 3 deletions src/pykiso/lib/auxiliaries/communication_auxiliary.py
Expand Up @@ -27,7 +27,7 @@
import queue
import threading
from contextlib import ContextDecorator
from typing import Any, Optional
from typing import Any, Optional, Tuple

from pykiso import CChannel, Message
from pykiso.auxiliary import AuxiliaryInterface, close_connector, open_connector
Expand Down Expand Up @@ -136,11 +136,13 @@ def receive_message(
self,
blocking: bool = True,
timeout_in_s: float = None,
) -> Optional[bytes]:
receive_timestamp: bool = False,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter docstring is missing 🙈

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouuups, sorry I was trying to be quick so it can be merge before 0.28.0 🚑

) -> Optional[bytes | Tuple[bytes, int] | Tuple[bytes, int, float]]:
"""Receive a raw message.

:param blocking: wait for message till timeout elapses?
:param timeout_in_s: maximum time in second to wait for a response
:param receive_timestamp: True if timestamp should be returned, False otherwise

:returns: raw message
"""
Expand All @@ -167,9 +169,12 @@ def receive_message(

msg = response.get("msg")
remote_id = response.get("remote_id")
timestamp = response.get("timestamp")

# stay with the old return type to not making a breaking change
if remote_id is not None:
if receive_timestamp:
return (msg, remote_id, timestamp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's still a breaking change, or am I missing something?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an additional keyword argument receive_timestamp: bool = False so that no breaking change occurs

elif remote_id and not receive_timestamp:
return (msg, remote_id)
return msg

Expand Down
40 changes: 33 additions & 7 deletions tests/test_com_aux.py
Expand Up @@ -12,10 +12,7 @@
import pytest

from pykiso import Message
from pykiso.lib.auxiliaries.communication_auxiliary import (
CommunicationAuxiliary,
queue,
)
from pykiso.lib.auxiliaries.communication_auxiliary import CommunicationAuxiliary, queue
from pykiso.test_setup.dynamic_loader import DynamicImportLinker


Expand Down Expand Up @@ -97,17 +94,46 @@ def test_com_aux_receive_messaging_without_contextmanager(caplog, com_aux_inst,
com_aux_inst.delete_instance()


def test_com_aux_receive_with_id_messaging_without_contextmanager(caplog, com_aux_inst, mocker):
msg = {"remote_id": 1, "msg": "test"}
mocker.patch.object(com_aux_inst.channel, "_cc_receive", return_value=msg)

com_aux_inst.create_instance()

payload, remote_id = com_aux_inst.receive_message()

assert payload == msg.get("msg")
assert remote_id== msg.get("remote_id")
com_aux_inst.delete_instance()


def test_com_aux_receive_with_timestamp_messaging_without_contextmanager(caplog, com_aux_inst, mocker):
msg = {"timestamp": 1005, "msg": "test"}
mocker.patch.object(com_aux_inst.channel, "_cc_receive", return_value=msg)

com_aux_inst.create_instance()

payload, remote_id, timestamp = com_aux_inst.receive_message(receive_timestamp=True)

assert payload == msg["msg"]
assert timestamp == msg["timestamp"]
assert remote_id == None
com_aux_inst.delete_instance()


def test_com_aux_receive_full_messaging_with_contextmanager(
caplog, com_aux_inst, mocker
):
msg = {"id": 1, "msg": "test"}
msg = {"remote_id": 1, "msg": "test", "timestamp": 52265}
mocker.patch.object(com_aux_inst.channel, "_cc_receive", return_value=msg)
com_aux_inst.create_instance()

with com_aux_inst.collect_messages():
ret = com_aux_inst.receive_message()
payload, remote_id, timestamp = com_aux_inst.receive_message(receive_timestamp=True)

assert ret == msg["msg"]
assert payload == msg["msg"]
assert remote_id == msg["remote_id"]
assert timestamp == msg["timestamp"]
com_aux_inst.delete_instance()


Expand Down