|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +""" |
| 5 | +This module tests :class:`can.MessageSync`. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import absolute_import |
| 9 | + |
| 10 | +from copy import copy |
| 11 | +from time import time |
| 12 | +import gc |
| 13 | + |
| 14 | +import unittest |
| 15 | +import pytest |
| 16 | + |
| 17 | +from can import MessageSync, Message |
| 18 | + |
| 19 | +from .message_helper import ComparingMessagesTestCase |
| 20 | +from .data.example_data import TEST_MESSAGES_BASE |
| 21 | +TEST_FEWER_MESSAGES = TEST_MESSAGES_BASE[::2] |
| 22 | + |
| 23 | +class TestMessageFiltering(unittest.TestCase, ComparingMessagesTestCase): |
| 24 | + |
| 25 | + def __init__(self, *args, **kwargs): |
| 26 | + unittest.TestCase.__init__(self, *args, **kwargs) |
| 27 | + ComparingMessagesTestCase.__init__(self) |
| 28 | + |
| 29 | + def setup_method(self, _): |
| 30 | + gc.disable() |
| 31 | + |
| 32 | + def teardown_method(self, _): |
| 33 | + gc.enable() |
| 34 | + |
| 35 | + @pytest.mark.timeout(0.2) |
| 36 | + def test_general(self): |
| 37 | + messages = [ |
| 38 | + Message(timestamp=50.0), |
| 39 | + Message(timestamp=50.0), |
| 40 | + Message(timestamp=50.0 + 0.05), |
| 41 | + Message(timestamp=50.0 + 0.05 + 0.08), |
| 42 | + Message(timestamp=50.0) # back in time |
| 43 | + ] |
| 44 | + sync = MessageSync(messages, gap=0.0) |
| 45 | + |
| 46 | + start = time() |
| 47 | + collected = [] |
| 48 | + timings = [] |
| 49 | + for message in sync: |
| 50 | + collected.append(message) |
| 51 | + now = time() |
| 52 | + timings.append(now - start) |
| 53 | + start = now |
| 54 | + |
| 55 | + self.assertMessagesEqual(messages, collected) |
| 56 | + self.assertEqual(len(timings), len(messages), "programming error in test code") |
| 57 | + |
| 58 | + self.assertTrue(0.0 <= timings[0] < 0.005, str(timings[0])) |
| 59 | + self.assertTrue(0.0 <= timings[1] < 0.005, str(timings[1])) |
| 60 | + self.assertTrue(0.045 <= timings[2] < 0.055, str(timings[2])) |
| 61 | + self.assertTrue(0.075 <= timings[3] < 0.085, str(timings[3])) |
| 62 | + self.assertTrue(0.0 <= timings[4] < 0.005, str(timings[4])) |
| 63 | + |
| 64 | + @pytest.mark.timeout(0.1 * len(TEST_FEWER_MESSAGES)) # very conservative |
| 65 | + def test_skip(self): |
| 66 | + messages = copy(TEST_FEWER_MESSAGES) |
| 67 | + sync = MessageSync(messages, skip=0.005, gap=0.0) |
| 68 | + |
| 69 | + before = time() |
| 70 | + collected = list(sync) |
| 71 | + after = time() |
| 72 | + took = after - before |
| 73 | + |
| 74 | + # the handling of the messages itself also take time: ~0.001 s/msg on my laptop |
| 75 | + assert 0 < took < len(messages) * (0.005 + 0.003), "took: {}s".format(took) |
| 76 | + |
| 77 | + self.assertMessagesEqual(messages, collected) |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.timeout(0.3) |
| 81 | +@pytest.mark.parametrize("timestamp_1,timestamp_2", [ |
| 82 | + (0.0, 0.0), |
| 83 | + (0.0, 0.01), |
| 84 | + (0.01, 0.0), |
| 85 | +]) |
| 86 | +def test_gap(timestamp_1, timestamp_2): |
| 87 | + """This method is alone so it can be parameterized.""" |
| 88 | + messages = [ |
| 89 | + Message(arbitration_id=0x1, timestamp=timestamp_1), |
| 90 | + Message(arbitration_id=0x2, timestamp=timestamp_2) |
| 91 | + ] |
| 92 | + sync = MessageSync(messages, gap=0.1) |
| 93 | + |
| 94 | + gc.disable() |
| 95 | + before = time() |
| 96 | + collected = list(sync) |
| 97 | + after = time() |
| 98 | + gc.enable() |
| 99 | + took = after - before |
| 100 | + |
| 101 | + assert 0.1 <= took < 0.3 |
| 102 | + assert messages == collected |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == '__main__': |
| 106 | + unittest.main() |
0 commit comments