Skip to content

Commit

Permalink
feat: com aux able to receive timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
TedRio committed Mar 27, 2024
1 parent 0351834 commit b34ecec
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
6 changes: 6 additions & 0 deletions docs/whats_new/version_ongoing.rst
@@ -1,2 +1,8 @@
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.
6 changes: 5 additions & 1 deletion src/pykiso/lib/auxiliaries/communication_auxiliary.py
Expand Up @@ -136,6 +136,7 @@ def receive_message(
self,
blocking: bool = True,
timeout_in_s: float = None,
receive_timestamp: bool = False,
) -> Optional[bytes]:
"""Receive a raw message.
Expand Down Expand Up @@ -167,9 +168,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)
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

0 comments on commit b34ecec

Please sign in to comment.