Skip to content

Commit fe179c7

Browse files
committed
implement join_threads helper method
1 parent 805f3fb commit fe179c7

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

test/simplecyclic_test.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
"""
66

77
import gc
8+
import sys
89
import time
10+
import traceback
911
import unittest
12+
from threading import Thread
1013
from time import sleep
1114
from typing import List
1215
from unittest.mock import MagicMock
@@ -87,6 +90,8 @@ def test_removing_bus_tasks(self):
8790
# Note calling task.stop will remove the task from the Bus's internal task management list
8891
task.stop()
8992

93+
self.join_threads([task.thread for task in tasks], 5.0)
94+
9095
assert len(bus._periodic_tasks) == 0
9196
bus.shutdown()
9297

@@ -115,8 +120,7 @@ def test_managed_tasks(self):
115120
for task in tasks:
116121
task.stop()
117122

118-
for task in tasks:
119-
assert task.thread.join(5.0) is None, "Task didn't stop before timeout"
123+
self.join_threads([task.thread for task in tasks], 5.0)
120124

121125
bus.shutdown()
122126

@@ -142,9 +146,7 @@ def test_stopping_perodic_tasks(self):
142146

143147
# stop the other half using the bus api
144148
bus.stop_all_periodic_tasks(remove_tasks=False)
145-
146-
for task in tasks:
147-
assert task.thread.join(5.0) is None, "Task didn't stop before timeout"
149+
self.join_threads([task.thread for task in tasks], 5.0)
148150

149151
# Tasks stopped via `stop_all_periodic_tasks` with remove_tasks=False should
150152
# still be associated with the bus (e.g. for restarting)
@@ -161,7 +163,7 @@ def test_restart_perodic_tasks(self):
161163
is_extended_id=False, arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7]
162164
)
163165

164-
def _read_all_messages(_bus: can.interfaces.virtual.VirtualBus) -> None:
166+
def _read_all_messages(_bus: "can.interfaces.virtual.VirtualBus") -> None:
165167
sleep(safe_timeout)
166168
while not _bus.queue.empty():
167169
_bus.recv(timeout=period)
@@ -207,9 +209,8 @@ def _read_all_messages(_bus: can.interfaces.virtual.VirtualBus) -> None:
207209

208210
# Stop all tasks and wait for the thread to exit
209211
bus.stop_all_periodic_tasks()
210-
if isinstance(task, can.broadcastmanager.ThreadBasedCyclicSendTask):
211-
# Avoids issues where the thread is still running when the bus is shutdown
212-
task.thread.join(safe_timeout)
212+
# Avoids issues where the thread is still running when the bus is shutdown
213+
self.join_threads([task.thread], 5.0)
213214

214215
@unittest.skipIf(IS_CI, "fails randomly when run on CI server")
215216
def test_thread_based_cyclic_send_task(self):
@@ -288,6 +289,27 @@ def increment_first_byte(msg: can.Message) -> None:
288289
self.assertEqual(b"\x06\x00\x00\x00\x00\x00\x00\x00", bytes(msg_list[5].data))
289290
self.assertEqual(b"\x07\x00\x00\x00\x00\x00\x00\x00", bytes(msg_list[6].data))
290291

292+
@staticmethod
293+
def join_threads(threads: List[Thread], timeout: float) -> None:
294+
stuck_threads: List[Thread] = []
295+
t0 = time.perf_counter()
296+
for thread in threads:
297+
time_left = timeout - (time.perf_counter() - t0)
298+
if time_left > 0.0:
299+
thread.join(time_left)
300+
if thread.is_alive():
301+
if platform.python_implementation() == "CPython":
302+
# print thread frame to help with debugging
303+
frame = sys._current_frames()[thread.ident]
304+
traceback.print_stack(frame, file=sys.stderr)
305+
stuck_threads.append(thread)
306+
if stuck_threads:
307+
err_message = (
308+
f"Threads did not stop within {timeout:.1f} seconds: "
309+
f"[{', '.join([str(t) for t in stuck_threads])}]"
310+
)
311+
raise RuntimeError(err_message)
312+
291313

292314
if __name__ == "__main__":
293315
unittest.main()

0 commit comments

Comments
 (0)