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

fix: fix timestamp inconsistencies between Windows and Linux #459

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
13 changes: 12 additions & 1 deletion src/pykiso/lib/connectors/cc_pcan_can/cc_pcan_can.py
Expand Up @@ -34,6 +34,16 @@
except ImportError as e:
raise ImportError(f"{e.name} dependency missing, consider installing pykiso with 'pip install pykiso[can]'")

try:
# use to avoid timestamp inconsistances between pykiso and python can
import uptime

if uptime.boottime() is None:
boottime_epoch = 0

Check warning on line 42 in src/pykiso/lib/connectors/cc_pcan_can/cc_pcan_can.py

View check run for this annotation

Codecov / codecov/patch

src/pykiso/lib/connectors/cc_pcan_can/cc_pcan_can.py#L41-L42

Added lines #L41 - L42 were not covered by tests
else:
boottime_epoch = uptime.boottime().timestamp()

Check warning on line 44 in src/pykiso/lib/connectors/cc_pcan_can/cc_pcan_can.py

View check run for this annotation

Codecov / codecov/patch

src/pykiso/lib/connectors/cc_pcan_can/cc_pcan_can.py#L44

Added line #L44 was not covered by tests
except ImportError:
boottime_epoch = 0
Comment on lines +37 to +46
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
try:
# use to avoid timestamp inconsistances between pykiso and python can
import uptime
if uptime.boottime() is None:
boottime_epoch = 0
else:
boottime_epoch = uptime.boottime().timestamp()
except ImportError:
boottime_epoch = 0
boottime_epoch = 0
try:
# use to avoid timestamp inconsistances between pykiso and python can in linux
import uptime
if not uptime.boottime():
boottime_epoch = uptime.boottime().timestamp()
except:
pass

Copy link
Contributor

Choose a reason for hiding this comment

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

I personally find it more explicit to set it to 0 in all failing cases

Copy link
Contributor

Choose a reason for hiding this comment

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

I also like the fact that we don't catch all errors


from pykiso import CChannel

Expand Down Expand Up @@ -152,6 +162,7 @@
# In case of a multi-threading system, all tasks will be called one after the other.
self.timeout = 1e-6
self.trc_count = 0
self.boottime_epoch = boottime_epoch
self._initialize_trace()

if bus_error_warning_filter:
Expand Down Expand Up @@ -358,7 +369,7 @@
if received_msg is not None:
frame_id = received_msg.arbitration_id
payload = received_msg.data
timestamp = received_msg.timestamp
timestamp = received_msg.timestamp - self.boottime_epoch

log.internal_debug(
"received CAN Message: %s, %s, %s",
Expand Down
19 changes: 8 additions & 11 deletions tests/test_cc_pcan_can.py
Expand Up @@ -22,18 +22,9 @@

from pykiso import Message
from pykiso.lib.connectors.cc_pcan_can import cc_pcan_can
from pykiso.lib.connectors.cc_pcan_can.cc_pcan_can import (
CCPCanCan,
PCANBasic,
can,
)
from pykiso.lib.connectors.cc_pcan_can.cc_pcan_can import CCPCanCan, PCANBasic, can
from pykiso.lib.connectors.cc_pcan_can.trc_handler import TRCReaderCanFD
from pykiso.message import (
MessageAckType,
MessageCommandType,
MessageType,
TlvKnownTags,
)
from pykiso.message import MessageAckType, MessageCommandType, MessageType, TlvKnownTags

tlv_dict_to_send = {
TlvKnownTags.TEST_REPORT: "OK",
Expand Down Expand Up @@ -274,6 +265,12 @@ def test_import():
importlib.reload(cc_pcan_can)


def test_import_uptime():
sys.modules["uptime"] = None
importlib.reload(cc_pcan_can)
assert cc_pcan_can.boottime_epoch == 0


@pytest.mark.parametrize(
"constructor_params, expected_config",
[
Expand Down