Debian Codegraph @

mypyc/test-data/run-threading.test

raw
1 # Test cases for librt.threading (compile and run)
2
3 [case testLockBasics_librt]
4 from typing import Any
5 from testutil import assertRaises
6 from librt.threading import Lock
7
8 class BadBool:
9 def __bool__(self) -> bool:
10 raise RuntimeError("bad bool")
11
12 def test_lock_basic() -> None:
13 lock = Lock()
14 assert not lock.locked()
15 assert lock.acquire()
16 assert lock.locked()
17 lock.release()
18 assert not lock.locked()
19
20 def test_lock_context_manager() -> None:
21 lock = Lock()
22 with lock as acquired:
23 assert acquired is True
24 assert lock.locked()
25 assert not lock.locked()
26
27 def test_lock_non_blocking() -> None:
28 lock = Lock()
29 assert lock.acquire()
30 assert not lock.acquire(False)
31 lock.release()
32 assert lock.acquire(False)
33 lock.release()
34
35 def test_contention() -> None:
36 import threading
37 lock = Lock()
38 counter = [0]
39 n_threads = 4
40 n_increments = 10000
41
42 def worker() -> None:
43 for _ in range(n_increments):
44 lock.acquire()
45 counter[0] += 1
46 lock.release()
47
48 threads = [threading.Thread(target=worker) for _ in range(n_threads)]
49 for t in threads:
50 t.start()
51 for t in threads:
52 t.join()
53 assert counter[0] == n_threads * n_increments
54
55 def test_cross_thread_release() -> None:
56 # threading.Lock is unowned: a lock acquired on one thread may be
57 # released from another, after which another thread can acquire it.
58 import threading
59 lock = Lock()
60 assert lock.acquire()
61
62 started = threading.Event()
63 released = threading.Event()
64
65 def releaser() -> None:
66 started.set()
67 # Hand the lock off from a different thread than the acquirer.
68 lock.release()
69 released.set()
70
71 t = threading.Thread(target=releaser)
72 t.start()
73 started.wait()
74 # This may block until releaser releases the lock on the other thread.
75 assert lock.acquire()
76 t.join()
77 assert released.is_set()
78 lock.release()
79 assert not lock.locked()
80
81 def test_context_manager_exception() -> None:
82 lock = Lock()
83 try:
84 with lock:
85 assert lock.locked()
86 raise ValueError("test")
87 except ValueError:
88 pass
89 assert not lock.locked()
90
91 def test_acquire_blocking_true() -> None:
92 lock = Lock()
93 assert lock.acquire(True)
94 assert lock.locked()
95 lock.release()
96
97 def test_lock_constructor_errors() -> None:
98 lock_type: Any = Lock
99 make_type: Any = type
100 with assertRaises(TypeError):
101 lock_type(1)
102 with assertRaises(TypeError):
103 lock_type(foo=1)
104 with assertRaises(TypeError):
105 make_type("LockSubclass", (lock_type,), {})
106
107 def test_lock_acquire_argument_errors() -> None:
108 lock: Any = Lock()
109 with assertRaises(TypeError):
110 lock.acquire(True, False)
111 with assertRaises(TypeError):
112 lock.acquire(foo=True)
113
114 def test_lock_acquire_blocking_truthiness() -> None:
115 lock: Any = Lock()
116 assert lock.acquire(blocking=True)
117 assert lock.locked()
118 assert not lock.acquire(blocking=False)
119 lock.release()
120
121 assert lock.acquire(None)
122 assert lock.locked()
123 assert not lock.acquire(None)
124 lock.release()
125
126 assert lock.acquire(1)
127 lock.release()
128 assert lock.acquire(0)
129 lock.release()
130
131 def test_lock_acquire_blocking_bool_error() -> None:
132 lock: Any = Lock()
133 with assertRaises(RuntimeError, "bad bool"):
134 lock.acquire(BadBool())
135 with assertRaises(RuntimeError, "bad bool"):
136 lock.acquire(blocking=BadBool())
137
138 def test_lock_exit_manual_call() -> None:
139 lock: Any = Lock()
140 lock.acquire()
141 assert lock.__exit__(None, None, None) is None
142 assert not lock.locked()
143
144 lock.acquire()
145 assert lock.__exit__() is None
146 assert not lock.locked()
147
148 def test_release_unlocked() -> None:
149 lock = Lock()
150 with assertRaises(RuntimeError):
151 lock.release()
152 # Also after acquire + release
153 lock.acquire()
154 lock.release()
155 with assertRaises(RuntimeError):
156 lock.release()