Debian Codegraph @

tests/sync/utils.py

raw
1 import contextlib
2 import threading
3 import time
4 import unittest
5
6 from ..utils import MS
7
8
9 class ThreadTestCase(unittest.TestCase):
10 @contextlib.contextmanager
11 def run_in_thread(self, target, args=(), kwargs=None):
12 """
13 Run ``target`` function without arguments in a thread.
14
15 In order to facilitate writing tests, this helper lets the thread run
16 for 1ms on entry and joins the thread with a 1ms timeout on exit.
17
18 """
19 thread = threading.Thread(target=target, args=args, kwargs=kwargs)
20 thread.start()
21 time.sleep(MS)
22 try:
23 yield
24 finally:
25 thread.join(MS)
26 self.assertFalse(thread.is_alive())