|
6 | 6 | import unittest
|
7 | 7 |
|
8 | 8 | import can
|
| 9 | +from test.config import IS_PYPY |
9 | 10 |
|
10 | 11 | logging.getLogger(__file__).setLevel(logging.WARNING)
|
11 | 12 |
|
12 | 13 |
|
13 | 14 | # make a random bool:
|
14 | 15 | def rbool():
|
15 |
| - return bool(round(random.random())) |
16 |
| - |
17 |
| - |
18 |
| -channel = "vcan0" |
| 16 | + return random.choice([False, True]) |
19 | 17 |
|
20 | 18 |
|
21 | 19 | class ControllerAreaNetworkTestCase(unittest.TestCase):
|
@@ -51,74 +49,51 @@ def tearDown(self):
|
51 | 49 | # Restore the defaults
|
52 | 50 | can.rc = self._can_rc
|
53 | 51 |
|
54 |
| - def producer(self, ready_event, msg_read): |
55 |
| - self.client_bus = can.interface.Bus(channel=channel) |
56 |
| - ready_event.wait() |
57 |
| - for i in range(self.num_messages): |
58 |
| - m = can.Message( |
59 |
| - arbitration_id=self.ids[i], |
60 |
| - is_remote_frame=self.remote_flags[i], |
61 |
| - is_error_frame=self.error_flags[i], |
62 |
| - is_extended_id=self.extended_flags[i], |
63 |
| - data=self.data[i], |
64 |
| - ) |
65 |
| - # logging.debug("writing message: {}".format(m)) |
66 |
| - if msg_read is not None: |
67 |
| - # Don't send until the other thread is ready |
68 |
| - msg_read.wait() |
69 |
| - msg_read.clear() |
70 |
| - |
71 |
| - self.client_bus.send(m) |
| 52 | + def producer(self, channel: str): |
| 53 | + with can.interface.Bus(channel=channel) as client_bus: |
| 54 | + for i in range(self.num_messages): |
| 55 | + m = can.Message( |
| 56 | + arbitration_id=self.ids[i], |
| 57 | + is_remote_frame=self.remote_flags[i], |
| 58 | + is_error_frame=self.error_flags[i], |
| 59 | + is_extended_id=self.extended_flags[i], |
| 60 | + data=self.data[i], |
| 61 | + ) |
| 62 | + client_bus.send(m) |
72 | 63 |
|
73 | 64 | def testProducer(self):
|
74 | 65 | """Verify that we can send arbitrary messages on the bus"""
|
75 | 66 | logging.debug("testing producer alone")
|
76 |
| - ready = threading.Event() |
77 |
| - ready.set() |
78 |
| - self.producer(ready, None) |
79 |
| - |
| 67 | + self.producer(channel="testProducer") |
80 | 68 | logging.debug("producer test complete")
|
81 | 69 |
|
82 | 70 | def testProducerConsumer(self):
|
83 | 71 | logging.debug("testing producer/consumer")
|
84 |
| - ready = threading.Event() |
85 |
| - msg_read = threading.Event() |
86 |
| - |
87 |
| - self.server_bus = can.interface.Bus(channel=channel, interface="virtual") |
88 |
| - |
89 |
| - t = threading.Thread(target=self.producer, args=(ready, msg_read)) |
90 |
| - t.start() |
91 |
| - |
92 |
| - # Ensure there are no messages on the bus |
93 |
| - while True: |
94 |
| - m = self.server_bus.recv(timeout=0.5) |
95 |
| - if m is None: |
96 |
| - print("No messages... lets go") |
97 |
| - break |
98 |
| - else: |
99 |
| - self.fail("received messages before the test has started ...") |
100 |
| - ready.set() |
101 |
| - i = 0 |
102 |
| - while i < self.num_messages: |
103 |
| - msg_read.set() |
104 |
| - msg = self.server_bus.recv(timeout=0.5) |
105 |
| - self.assertIsNotNone(msg, "Didn't receive a message") |
106 |
| - # logging.debug("Received message {} with data: {}".format(i, msg.data)) |
107 |
| - |
108 |
| - self.assertEqual(msg.is_extended_id, self.extended_flags[i]) |
109 |
| - if not msg.is_remote_frame: |
110 |
| - self.assertEqual(msg.data, self.data[i]) |
111 |
| - self.assertEqual(msg.arbitration_id, self.ids[i]) |
112 |
| - |
113 |
| - self.assertEqual(msg.is_error_frame, self.error_flags[i]) |
114 |
| - self.assertEqual(msg.is_remote_frame, self.remote_flags[i]) |
115 |
| - |
116 |
| - i += 1 |
117 |
| - t.join() |
118 |
| - |
119 |
| - with contextlib.suppress(NotImplementedError): |
120 |
| - self.server_bus.flush_tx_buffer() |
121 |
| - self.server_bus.shutdown() |
| 72 | + read_timeout = 2.0 if IS_PYPY else 0.5 |
| 73 | + channel = "testProducerConsumer" |
| 74 | + |
| 75 | + with can.interface.Bus(channel=channel, interface="virtual") as server_bus: |
| 76 | + t = threading.Thread(target=self.producer, args=(channel,)) |
| 77 | + t.start() |
| 78 | + |
| 79 | + i = 0 |
| 80 | + while i < self.num_messages: |
| 81 | + msg = server_bus.recv(timeout=read_timeout) |
| 82 | + self.assertIsNotNone(msg, "Didn't receive a message") |
| 83 | + |
| 84 | + self.assertEqual(msg.is_extended_id, self.extended_flags[i]) |
| 85 | + if not msg.is_remote_frame: |
| 86 | + self.assertEqual(msg.data, self.data[i]) |
| 87 | + self.assertEqual(msg.arbitration_id, self.ids[i]) |
| 88 | + |
| 89 | + self.assertEqual(msg.is_error_frame, self.error_flags[i]) |
| 90 | + self.assertEqual(msg.is_remote_frame, self.remote_flags[i]) |
| 91 | + |
| 92 | + i += 1 |
| 93 | + t.join() |
| 94 | + |
| 95 | + with contextlib.suppress(NotImplementedError): |
| 96 | + server_bus.flush_tx_buffer() |
122 | 97 |
|
123 | 98 |
|
124 | 99 | if __name__ == "__main__":
|
|
0 commit comments