|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +This module tests the functions inside of bridge.py |
| 5 | +""" |
| 6 | + |
| 7 | +import random |
| 8 | +import string |
| 9 | +import sys |
| 10 | +import threading |
| 11 | +import time |
| 12 | +from time import sleep as real_sleep |
| 13 | +import unittest.mock |
| 14 | + |
| 15 | +import can |
| 16 | +import can.bridge |
| 17 | +from can.interfaces import virtual |
| 18 | + |
| 19 | +from .message_helper import ComparingMessagesTestCase |
| 20 | + |
| 21 | + |
| 22 | +class TestBridgeScriptModule(unittest.TestCase, ComparingMessagesTestCase): |
| 23 | + |
| 24 | + TIMEOUT = 3.0 |
| 25 | + |
| 26 | + def __init__(self, *args, **kwargs): |
| 27 | + unittest.TestCase.__init__(self, *args, **kwargs) |
| 28 | + ComparingMessagesTestCase.__init__( |
| 29 | + self, |
| 30 | + allowed_timestamp_delta=None, |
| 31 | + preserves_channel=False, |
| 32 | + ) |
| 33 | + |
| 34 | + def setUp(self) -> None: |
| 35 | + self.stop_event = threading.Event() |
| 36 | + |
| 37 | + self.channel1 = "".join(random.choices(string.ascii_letters, k=8)) |
| 38 | + self.channel2 = "".join(random.choices(string.ascii_letters, k=8)) |
| 39 | + |
| 40 | + self.cli_args = [ |
| 41 | + "--bus1-interface", |
| 42 | + "virtual", |
| 43 | + "--bus1-channel", |
| 44 | + self.channel1, |
| 45 | + "--bus2-interface", |
| 46 | + "virtual", |
| 47 | + "--bus2-channel", |
| 48 | + self.channel2, |
| 49 | + ] |
| 50 | + |
| 51 | + self.testmsg = can.Message( |
| 52 | + arbitration_id=0xC0FFEE, data=[0, 25, 0, 1, 3, 1, 4, 1], is_extended_id=True |
| 53 | + ) |
| 54 | + |
| 55 | + def fake_sleep(self, duration): |
| 56 | + """A fake replacement for time.sleep that checks periodically |
| 57 | + whether self.stop_event is set, and raises KeyboardInterrupt |
| 58 | + if so. |
| 59 | +
|
| 60 | + This allows tests to simulate an interrupt (like Ctrl+C) |
| 61 | + during long sleeps, in a controlled and responsive way. |
| 62 | + """ |
| 63 | + interval = 0.05 # Small interval for responsiveness |
| 64 | + t_wakeup = time.perf_counter() + duration |
| 65 | + while time.perf_counter() < t_wakeup: |
| 66 | + if self.stop_event.is_set(): |
| 67 | + raise KeyboardInterrupt("Simulated interrupt from fake_sleep") |
| 68 | + real_sleep(interval) |
| 69 | + |
| 70 | + def test_bridge(self): |
| 71 | + with ( |
| 72 | + unittest.mock.patch("can.bridge.time.sleep", new=self.fake_sleep), |
| 73 | + unittest.mock.patch("can.bridge.sys.argv", [sys.argv[0], *self.cli_args]), |
| 74 | + ): |
| 75 | + # start script |
| 76 | + thread = threading.Thread(target=can.bridge.main) |
| 77 | + thread.start() |
| 78 | + |
| 79 | + # wait until script instantiates virtual buses |
| 80 | + t0 = time.perf_counter() |
| 81 | + while True: |
| 82 | + with virtual.channels_lock: |
| 83 | + if ( |
| 84 | + self.channel1 in virtual.channels |
| 85 | + and self.channel2 in virtual.channels |
| 86 | + ): |
| 87 | + break |
| 88 | + if time.perf_counter() > t0 + 2.0: |
| 89 | + raise TimeoutError("Bridge script did not create virtual buses") |
| 90 | + real_sleep(0.2) |
| 91 | + |
| 92 | + # create buses with the same channels as in scripts |
| 93 | + with ( |
| 94 | + can.interfaces.virtual.VirtualBus(self.channel1) as bus1, |
| 95 | + can.interfaces.virtual.VirtualBus(self.channel2) as bus2, |
| 96 | + ): |
| 97 | + # send test message to bus1, it should be received on bus2 |
| 98 | + bus1.send(self.testmsg) |
| 99 | + recv_msg = bus2.recv(self.TIMEOUT) |
| 100 | + self.assertMessageEqual(self.testmsg, recv_msg) |
| 101 | + |
| 102 | + # assert that both buses are empty |
| 103 | + self.assertIsNone(bus1.recv(0)) |
| 104 | + self.assertIsNone(bus2.recv(0)) |
| 105 | + |
| 106 | + # send test message to bus2, it should be received on bus1 |
| 107 | + bus2.send(self.testmsg) |
| 108 | + recv_msg = bus1.recv(self.TIMEOUT) |
| 109 | + self.assertMessageEqual(self.testmsg, recv_msg) |
| 110 | + |
| 111 | + # assert that both buses are empty |
| 112 | + self.assertIsNone(bus1.recv(0)) |
| 113 | + self.assertIsNone(bus2.recv(0)) |
| 114 | + |
| 115 | + # stop the bridge script |
| 116 | + self.stop_event.set() |
| 117 | + thread.join() |
| 118 | + |
| 119 | + # assert that the virtual buses were closed |
| 120 | + with virtual.channels_lock: |
| 121 | + self.assertNotIn(self.channel1, virtual.channels) |
| 122 | + self.assertNotIn(self.channel2, virtual.channels) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + unittest.main() |
0 commit comments