-
Notifications
You must be signed in to change notification settings - Fork 638
Test MessageSync #561
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
Merged
Merged
Test MessageSync #561
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1a0442a
This fixes the non-standard usage of __test__=False to exclude the ba…
felixdivo d68e543
Merge branch 'develop' into fix-logformats-base-test-case
felixdivo 0f5a6b8
attempt to fix test_scripts
felixdivo 5550fd1
fix use of deprecated test method
felixdivo f4fef93
revert last commit
felixdivo efe186c
refactor & comment player
felixdivo 4a86610
refactor message equality helper
felixdivo 2033be6
add temporary GC disableing to test_cycle_time()
felixdivo cfc1b2c
add test from MessageSync
felixdivo 76a16f8
exclude AppVeyor from MessageSync tests
felixdivo 405a89f
exclude AppVeyor from MessageSync tests
felixdivo 3894d04
make tests/timings easier for CI tests
felixdivo 519fc93
make tests better suited for CI tests (by excluding them on some plat…
felixdivo ffa8bea
Merge branch 'develop' into test-message-sync
felixdivo b5add93
Merge branch 'develop' into test-message-sync
felixdivo fad98ee
better comments
felixdivo dbf840f
Merge branch 'test-message-sync' of github.com:hardbyte/python-can in…
felixdivo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#!/usr/bin/env python | ||
# coding: utf-8 | ||
|
||
""" | ||
This module tests :class:`can.MessageSync`. | ||
""" | ||
|
||
from __future__ import absolute_import | ||
|
||
from copy import copy | ||
from time import time | ||
import gc | ||
|
||
import unittest | ||
import pytest | ||
|
||
from can import MessageSync, Message | ||
|
||
from .config import IS_CI, IS_APPVEYOR, IS_TRAVIS, IS_OSX | ||
from .message_helper import ComparingMessagesTestCase | ||
from .data.example_data import TEST_MESSAGES_BASE | ||
|
||
|
||
TEST_FEWER_MESSAGES = TEST_MESSAGES_BASE[::2] | ||
|
||
|
||
def inc(value): | ||
"""Makes the test boundaries give some more space when run on the CI server.""" | ||
if IS_CI: | ||
return value * 1.5 | ||
else: | ||
return value | ||
|
||
|
||
@unittest.skipIf(IS_APPVEYOR or (IS_TRAVIS and IS_OSX), | ||
"this environment's timings are too unpredictable") | ||
class TestMessageSync(unittest.TestCase, ComparingMessagesTestCase): | ||
|
||
def __init__(self, *args, **kwargs): | ||
unittest.TestCase.__init__(self, *args, **kwargs) | ||
ComparingMessagesTestCase.__init__(self) | ||
|
||
def setup_method(self, _): | ||
# disabling the garbage collector makes the time readings more reliable | ||
gc.disable() | ||
|
||
def teardown_method(self, _): | ||
# we need to reenable the garbage collector again | ||
gc.enable() | ||
|
||
@pytest.mark.timeout(inc(0.2)) | ||
def test_general(self): | ||
messages = [ | ||
Message(timestamp=50.0), | ||
Message(timestamp=50.0), | ||
Message(timestamp=50.0 + 0.05), | ||
Message(timestamp=50.0 + 0.05 + 0.08), | ||
Message(timestamp=50.0) # back in time | ||
] | ||
sync = MessageSync(messages, gap=0.0) | ||
|
||
start = time() | ||
collected = [] | ||
timings = [] | ||
for message in sync: | ||
collected.append(message) | ||
now = time() | ||
timings.append(now - start) | ||
start = now | ||
|
||
self.assertMessagesEqual(messages, collected) | ||
self.assertEqual(len(timings), len(messages), "programming error in test code") | ||
|
||
self.assertTrue(0.0 <= timings[0] < inc(0.005), str(timings[0])) | ||
self.assertTrue(0.0 <= timings[1] < inc(0.005), str(timings[1])) | ||
self.assertTrue(0.045 <= timings[2] < inc(0.055), str(timings[2])) | ||
self.assertTrue(0.075 <= timings[3] < inc(0.085), str(timings[3])) | ||
self.assertTrue(0.0 <= timings[4] < inc(0.005), str(timings[4])) | ||
|
||
@pytest.mark.timeout(inc(0.1) * len(TEST_FEWER_MESSAGES)) # very conservative | ||
def test_skip(self): | ||
messages = copy(TEST_FEWER_MESSAGES) | ||
sync = MessageSync(messages, skip=0.005, gap=0.0) | ||
|
||
before = time() | ||
collected = list(sync) | ||
after = time() | ||
took = after - before | ||
|
||
# the handling of the messages itself also takes some time: | ||
# ~0.001 s/message on a ThinkPad T560 laptop (Ubuntu 18.04, i5-6200U) | ||
assert 0 < took < inc(len(messages) * (0.005 + 0.003)), "took: {}s".format(took) | ||
|
||
self.assertMessagesEqual(messages, collected) | ||
|
||
|
||
if not IS_APPVEYOR: # this environment's timings are too unpredictable | ||
|
||
@pytest.mark.timeout(inc(0.3)) | ||
@pytest.mark.parametrize("timestamp_1,timestamp_2", [ | ||
(0.0, 0.0), | ||
(0.0, 0.01), | ||
(0.01, 0.0), | ||
]) | ||
def test_gap(timestamp_1, timestamp_2): | ||
"""This method is alone so it can be parameterized.""" | ||
messages = [ | ||
Message(arbitration_id=0x1, timestamp=timestamp_1), | ||
Message(arbitration_id=0x2, timestamp=timestamp_2) | ||
] | ||
sync = MessageSync(messages, gap=0.1) | ||
|
||
gc.disable() | ||
before = time() | ||
collected = list(sync) | ||
after = time() | ||
gc.enable() | ||
took = after - before | ||
|
||
assert 0.1 <= took < inc(0.3) | ||
assert messages == collected | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.