Debian Codegraph @

src/trio/_tests/test_wait_for_object.py

raw
1 import os
2
3 import pytest
4
5 on_windows = os.name == "nt"
6 # Mark all the tests in this file as being windows-only
7 pytestmark = pytest.mark.skipif(not on_windows, reason="windows only")
8
9 import trio
10
11 from .. import _core, _timeouts
12 from .._core._tests.tutil import slow
13
14 if on_windows:
15 from .._core._windows_cffi import Handle, ffi, kernel32
16 from .._wait_for_object import WaitForMultipleObjects_sync, WaitForSingleObject
17
18
19 def test_WaitForMultipleObjects_sync() -> None:
20 # This does a series of tests where we set/close the handle before
21 # initiating the waiting for it.
22 #
23 # Note that closing the handle (not signaling) will cause the
24 # *initiation* of a wait to return immediately. But closing a handle
25 # that is already being waited on will not stop whatever is waiting
26 # for it.
27
28 # One handle
29 handle1 = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
30 kernel32.SetEvent(handle1)
31 WaitForMultipleObjects_sync(handle1)
32 kernel32.CloseHandle(handle1)
33 print("test_WaitForMultipleObjects_sync one OK")
34
35 # Two handles, signal first
36 handle1 = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
37 handle2 = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
38 kernel32.SetEvent(handle1)
39 WaitForMultipleObjects_sync(handle1, handle2)
40 kernel32.CloseHandle(handle1)
41 kernel32.CloseHandle(handle2)
42 print("test_WaitForMultipleObjects_sync set first OK")
43
44 # Two handles, signal second
45 handle1 = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
46 handle2 = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
47 kernel32.SetEvent(handle2)
48 WaitForMultipleObjects_sync(handle1, handle2)
49 kernel32.CloseHandle(handle1)
50 kernel32.CloseHandle(handle2)
51 print("test_WaitForMultipleObjects_sync set second OK")
52
53 # Two handles, close first
54 handle1 = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
55 handle2 = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
56 kernel32.CloseHandle(handle1)
57 with pytest.raises(OSError, match=r"^\[WinError 6\] The handle is invalid$"):
58 WaitForMultipleObjects_sync(handle1, handle2)
59 kernel32.CloseHandle(handle2)
60 print("test_WaitForMultipleObjects_sync close first OK")
61
62 # Two handles, close second
63 handle1 = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
64 handle2 = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
65 kernel32.CloseHandle(handle2)
66 with pytest.raises(OSError, match=r"^\[WinError 6\] The handle is invalid$"):
67 WaitForMultipleObjects_sync(handle1, handle2)
68 kernel32.CloseHandle(handle1)
69 print("test_WaitForMultipleObjects_sync close second OK")
70
71
72 @slow
73 async def test_WaitForMultipleObjects_sync_slow() -> None:
74 # This does a series of test in which the main thread sync-waits for
75 # handles, while we spawn a thread to set the handles after a short while.
76
77 TIMEOUT = 0.3
78
79 # One handle
80 handle1 = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
81 t0 = _core.current_time()
82 async with _core.open_nursery() as nursery:
83 nursery.start_soon(
84 trio.to_thread.run_sync,
85 WaitForMultipleObjects_sync,
86 handle1,
87 )
88 await _timeouts.sleep(TIMEOUT)
89 # If we would comment the line below, the above thread will be stuck,
90 # and Trio won't exit this scope
91 kernel32.SetEvent(handle1)
92 t1 = _core.current_time()
93 assert TIMEOUT <= (t1 - t0) < 2.0 * TIMEOUT
94 kernel32.CloseHandle(handle1)
95 print("test_WaitForMultipleObjects_sync_slow one OK")
96
97 # Two handles, signal first
98 handle1 = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
99 handle2 = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
100 t0 = _core.current_time()
101 async with _core.open_nursery() as nursery:
102 nursery.start_soon(
103 trio.to_thread.run_sync,
104 WaitForMultipleObjects_sync,
105 handle1,
106 handle2,
107 )
108 await _timeouts.sleep(TIMEOUT)
109 kernel32.SetEvent(handle1)
110 t1 = _core.current_time()
111 assert TIMEOUT <= (t1 - t0) < 2.0 * TIMEOUT
112 kernel32.CloseHandle(handle1)
113 kernel32.CloseHandle(handle2)
114 print("test_WaitForMultipleObjects_sync_slow thread-set first OK")
115
116 # Two handles, signal second
117 handle1 = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
118 handle2 = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
119 t0 = _core.current_time()
120 async with _core.open_nursery() as nursery:
121 nursery.start_soon(
122 trio.to_thread.run_sync,
123 WaitForMultipleObjects_sync,
124 handle1,
125 handle2,
126 )
127 await _timeouts.sleep(TIMEOUT)
128 kernel32.SetEvent(handle2)
129 t1 = _core.current_time()
130 assert TIMEOUT <= (t1 - t0) < 2.0 * TIMEOUT
131 kernel32.CloseHandle(handle1)
132 kernel32.CloseHandle(handle2)
133 print("test_WaitForMultipleObjects_sync_slow thread-set second OK")
134
135
136 async def test_WaitForSingleObject() -> None:
137 # This does a series of test for setting/closing the handle before
138 # initiating the wait.
139
140 # Test already set
141 handle = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
142 kernel32.SetEvent(handle)
143 await WaitForSingleObject(handle) # should return at once
144 kernel32.CloseHandle(handle)
145 print("test_WaitForSingleObject already set OK")
146
147 # Test already set, as int
148 handle = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
149 handle_int = int(ffi.cast("intptr_t", handle))
150 kernel32.SetEvent(handle)
151 await WaitForSingleObject(handle_int) # should return at once
152 kernel32.CloseHandle(handle)
153 print("test_WaitForSingleObject already set OK")
154
155 # Test already closed
156 handle = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
157 kernel32.CloseHandle(handle)
158 with pytest.raises(OSError, match=r"^\[WinError 6\] The handle is invalid$"):
159 await WaitForSingleObject(handle) # should return at once
160 print("test_WaitForSingleObject already closed OK")
161
162 # Not a handle
163 with pytest.raises(TypeError):
164 await WaitForSingleObject("not a handle") # type: ignore[arg-type] # Wrong type
165 # with pytest.raises(OSError):
166 # await WaitForSingleObject(99) # If you're unlucky, it actually IS a handle :(
167 print("test_WaitForSingleObject not a handle OK")
168
169
170 @slow
171 async def test_WaitForSingleObject_slow() -> None:
172 # This does a series of test for setting the handle in another task,
173 # and cancelling the wait task.
174
175 # Set the timeout used in the tests. We test the waiting time against
176 # the timeout with a certain margin.
177 TIMEOUT = 0.3
178
179 async def signal_soon_async(handle: Handle) -> None:
180 await _timeouts.sleep(TIMEOUT)
181 kernel32.SetEvent(handle)
182
183 # Test handle is SET after TIMEOUT in separate coroutine
184
185 handle = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
186 t0 = _core.current_time()
187
188 async with _core.open_nursery() as nursery:
189 nursery.start_soon(WaitForSingleObject, handle)
190 nursery.start_soon(signal_soon_async, handle)
191
192 kernel32.CloseHandle(handle)
193 t1 = _core.current_time()
194 assert TIMEOUT <= (t1 - t0) < 2.0 * TIMEOUT
195 print("test_WaitForSingleObject_slow set from task OK")
196
197 # Test handle is SET after TIMEOUT in separate coroutine, as int
198
199 handle = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
200 handle_int = int(ffi.cast("intptr_t", handle))
201 t0 = _core.current_time()
202
203 async with _core.open_nursery() as nursery:
204 nursery.start_soon(WaitForSingleObject, handle_int)
205 nursery.start_soon(signal_soon_async, handle)
206
207 kernel32.CloseHandle(handle)
208 t1 = _core.current_time()
209 assert TIMEOUT <= (t1 - t0) < 2.0 * TIMEOUT
210 print("test_WaitForSingleObject_slow set from task as int OK")
211
212 # Test handle is CLOSED after 1 sec - NOPE see comment above
213
214 # Test cancellation
215
216 handle = kernel32.CreateEventA(ffi.NULL, True, False, ffi.NULL)
217 t0 = _core.current_time()
218
219 with _timeouts.move_on_after(TIMEOUT):
220 await WaitForSingleObject(handle)
221
222 kernel32.CloseHandle(handle)
223 t1 = _core.current_time()
224 assert TIMEOUT <= (t1 - t0) < 2.0 * TIMEOUT
225 print("test_WaitForSingleObject_slow cancellation OK")