File: utils.py

package info (click to toggle)
python-websockets 15.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,948 kB
  • sloc: python: 25,105; javascript: 350; ansic: 148; makefile: 43
file content (26 lines) | stat: -rw-r--r-- 658 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import contextlib
import threading
import time
import unittest

from ..utils import MS


class ThreadTestCase(unittest.TestCase):
    @contextlib.contextmanager
    def run_in_thread(self, target):
        """
        Run ``target`` function without arguments in a thread.

        In order to facilitate writing tests, this helper lets the thread run
        for 1ms on entry and joins the thread with a 1ms timeout on exit.

        """
        thread = threading.Thread(target=target)
        thread.start()
        time.sleep(MS)
        try:
            yield
        finally:
            thread.join(MS)
            self.assertFalse(thread.is_alive())