1 | n/a | """Tests for unix_events.py.""" |
---|
2 | n/a | |
---|
3 | n/a | import collections |
---|
4 | n/a | import errno |
---|
5 | n/a | import io |
---|
6 | n/a | import os |
---|
7 | n/a | import pathlib |
---|
8 | n/a | import signal |
---|
9 | n/a | import socket |
---|
10 | n/a | import stat |
---|
11 | n/a | import sys |
---|
12 | n/a | import tempfile |
---|
13 | n/a | import threading |
---|
14 | n/a | import unittest |
---|
15 | n/a | from unittest import mock |
---|
16 | n/a | |
---|
17 | n/a | if sys.platform == 'win32': |
---|
18 | n/a | raise unittest.SkipTest('UNIX only') |
---|
19 | n/a | |
---|
20 | n/a | |
---|
21 | n/a | import asyncio |
---|
22 | n/a | from asyncio import log |
---|
23 | n/a | from asyncio import test_utils |
---|
24 | n/a | from asyncio import unix_events |
---|
25 | n/a | |
---|
26 | n/a | |
---|
27 | n/a | MOCK_ANY = mock.ANY |
---|
28 | n/a | |
---|
29 | n/a | |
---|
30 | n/a | def close_pipe_transport(transport): |
---|
31 | n/a | # Don't call transport.close() because the event loop and the selector |
---|
32 | n/a | # are mocked |
---|
33 | n/a | if transport._pipe is None: |
---|
34 | n/a | return |
---|
35 | n/a | transport._pipe.close() |
---|
36 | n/a | transport._pipe = None |
---|
37 | n/a | |
---|
38 | n/a | |
---|
39 | n/a | @unittest.skipUnless(signal, 'Signals are not supported') |
---|
40 | n/a | class SelectorEventLoopSignalTests(test_utils.TestCase): |
---|
41 | n/a | |
---|
42 | n/a | def setUp(self): |
---|
43 | n/a | super().setUp() |
---|
44 | n/a | self.loop = asyncio.SelectorEventLoop() |
---|
45 | n/a | self.set_event_loop(self.loop) |
---|
46 | n/a | |
---|
47 | n/a | def test_check_signal(self): |
---|
48 | n/a | self.assertRaises( |
---|
49 | n/a | TypeError, self.loop._check_signal, '1') |
---|
50 | n/a | self.assertRaises( |
---|
51 | n/a | ValueError, self.loop._check_signal, signal.NSIG + 1) |
---|
52 | n/a | |
---|
53 | n/a | def test_handle_signal_no_handler(self): |
---|
54 | n/a | self.loop._handle_signal(signal.NSIG + 1) |
---|
55 | n/a | |
---|
56 | n/a | def test_handle_signal_cancelled_handler(self): |
---|
57 | n/a | h = asyncio.Handle(mock.Mock(), (), |
---|
58 | n/a | loop=mock.Mock()) |
---|
59 | n/a | h.cancel() |
---|
60 | n/a | self.loop._signal_handlers[signal.NSIG + 1] = h |
---|
61 | n/a | self.loop.remove_signal_handler = mock.Mock() |
---|
62 | n/a | self.loop._handle_signal(signal.NSIG + 1) |
---|
63 | n/a | self.loop.remove_signal_handler.assert_called_with(signal.NSIG + 1) |
---|
64 | n/a | |
---|
65 | n/a | @mock.patch('asyncio.unix_events.signal') |
---|
66 | n/a | def test_add_signal_handler_setup_error(self, m_signal): |
---|
67 | n/a | m_signal.NSIG = signal.NSIG |
---|
68 | n/a | m_signal.set_wakeup_fd.side_effect = ValueError |
---|
69 | n/a | |
---|
70 | n/a | self.assertRaises( |
---|
71 | n/a | RuntimeError, |
---|
72 | n/a | self.loop.add_signal_handler, |
---|
73 | n/a | signal.SIGINT, lambda: True) |
---|
74 | n/a | |
---|
75 | n/a | @mock.patch('asyncio.unix_events.signal') |
---|
76 | n/a | def test_add_signal_handler_coroutine_error(self, m_signal): |
---|
77 | n/a | m_signal.NSIG = signal.NSIG |
---|
78 | n/a | |
---|
79 | n/a | @asyncio.coroutine |
---|
80 | n/a | def simple_coroutine(): |
---|
81 | n/a | yield from [] |
---|
82 | n/a | |
---|
83 | n/a | # callback must not be a coroutine function |
---|
84 | n/a | coro_func = simple_coroutine |
---|
85 | n/a | coro_obj = coro_func() |
---|
86 | n/a | self.addCleanup(coro_obj.close) |
---|
87 | n/a | for func in (coro_func, coro_obj): |
---|
88 | n/a | self.assertRaisesRegex( |
---|
89 | n/a | TypeError, 'coroutines cannot be used with add_signal_handler', |
---|
90 | n/a | self.loop.add_signal_handler, |
---|
91 | n/a | signal.SIGINT, func) |
---|
92 | n/a | |
---|
93 | n/a | @mock.patch('asyncio.unix_events.signal') |
---|
94 | n/a | def test_add_signal_handler(self, m_signal): |
---|
95 | n/a | m_signal.NSIG = signal.NSIG |
---|
96 | n/a | |
---|
97 | n/a | cb = lambda: True |
---|
98 | n/a | self.loop.add_signal_handler(signal.SIGHUP, cb) |
---|
99 | n/a | h = self.loop._signal_handlers.get(signal.SIGHUP) |
---|
100 | n/a | self.assertIsInstance(h, asyncio.Handle) |
---|
101 | n/a | self.assertEqual(h._callback, cb) |
---|
102 | n/a | |
---|
103 | n/a | @mock.patch('asyncio.unix_events.signal') |
---|
104 | n/a | def test_add_signal_handler_install_error(self, m_signal): |
---|
105 | n/a | m_signal.NSIG = signal.NSIG |
---|
106 | n/a | |
---|
107 | n/a | def set_wakeup_fd(fd): |
---|
108 | n/a | if fd == -1: |
---|
109 | n/a | raise ValueError() |
---|
110 | n/a | m_signal.set_wakeup_fd = set_wakeup_fd |
---|
111 | n/a | |
---|
112 | n/a | class Err(OSError): |
---|
113 | n/a | errno = errno.EFAULT |
---|
114 | n/a | m_signal.signal.side_effect = Err |
---|
115 | n/a | |
---|
116 | n/a | self.assertRaises( |
---|
117 | n/a | Err, |
---|
118 | n/a | self.loop.add_signal_handler, |
---|
119 | n/a | signal.SIGINT, lambda: True) |
---|
120 | n/a | |
---|
121 | n/a | @mock.patch('asyncio.unix_events.signal') |
---|
122 | n/a | @mock.patch('asyncio.base_events.logger') |
---|
123 | n/a | def test_add_signal_handler_install_error2(self, m_logging, m_signal): |
---|
124 | n/a | m_signal.NSIG = signal.NSIG |
---|
125 | n/a | |
---|
126 | n/a | class Err(OSError): |
---|
127 | n/a | errno = errno.EINVAL |
---|
128 | n/a | m_signal.signal.side_effect = Err |
---|
129 | n/a | |
---|
130 | n/a | self.loop._signal_handlers[signal.SIGHUP] = lambda: True |
---|
131 | n/a | self.assertRaises( |
---|
132 | n/a | RuntimeError, |
---|
133 | n/a | self.loop.add_signal_handler, |
---|
134 | n/a | signal.SIGINT, lambda: True) |
---|
135 | n/a | self.assertFalse(m_logging.info.called) |
---|
136 | n/a | self.assertEqual(1, m_signal.set_wakeup_fd.call_count) |
---|
137 | n/a | |
---|
138 | n/a | @mock.patch('asyncio.unix_events.signal') |
---|
139 | n/a | @mock.patch('asyncio.base_events.logger') |
---|
140 | n/a | def test_add_signal_handler_install_error3(self, m_logging, m_signal): |
---|
141 | n/a | class Err(OSError): |
---|
142 | n/a | errno = errno.EINVAL |
---|
143 | n/a | m_signal.signal.side_effect = Err |
---|
144 | n/a | m_signal.NSIG = signal.NSIG |
---|
145 | n/a | |
---|
146 | n/a | self.assertRaises( |
---|
147 | n/a | RuntimeError, |
---|
148 | n/a | self.loop.add_signal_handler, |
---|
149 | n/a | signal.SIGINT, lambda: True) |
---|
150 | n/a | self.assertFalse(m_logging.info.called) |
---|
151 | n/a | self.assertEqual(2, m_signal.set_wakeup_fd.call_count) |
---|
152 | n/a | |
---|
153 | n/a | @mock.patch('asyncio.unix_events.signal') |
---|
154 | n/a | def test_remove_signal_handler(self, m_signal): |
---|
155 | n/a | m_signal.NSIG = signal.NSIG |
---|
156 | n/a | |
---|
157 | n/a | self.loop.add_signal_handler(signal.SIGHUP, lambda: True) |
---|
158 | n/a | |
---|
159 | n/a | self.assertTrue( |
---|
160 | n/a | self.loop.remove_signal_handler(signal.SIGHUP)) |
---|
161 | n/a | self.assertTrue(m_signal.set_wakeup_fd.called) |
---|
162 | n/a | self.assertTrue(m_signal.signal.called) |
---|
163 | n/a | self.assertEqual( |
---|
164 | n/a | (signal.SIGHUP, m_signal.SIG_DFL), m_signal.signal.call_args[0]) |
---|
165 | n/a | |
---|
166 | n/a | @mock.patch('asyncio.unix_events.signal') |
---|
167 | n/a | def test_remove_signal_handler_2(self, m_signal): |
---|
168 | n/a | m_signal.NSIG = signal.NSIG |
---|
169 | n/a | m_signal.SIGINT = signal.SIGINT |
---|
170 | n/a | |
---|
171 | n/a | self.loop.add_signal_handler(signal.SIGINT, lambda: True) |
---|
172 | n/a | self.loop._signal_handlers[signal.SIGHUP] = object() |
---|
173 | n/a | m_signal.set_wakeup_fd.reset_mock() |
---|
174 | n/a | |
---|
175 | n/a | self.assertTrue( |
---|
176 | n/a | self.loop.remove_signal_handler(signal.SIGINT)) |
---|
177 | n/a | self.assertFalse(m_signal.set_wakeup_fd.called) |
---|
178 | n/a | self.assertTrue(m_signal.signal.called) |
---|
179 | n/a | self.assertEqual( |
---|
180 | n/a | (signal.SIGINT, m_signal.default_int_handler), |
---|
181 | n/a | m_signal.signal.call_args[0]) |
---|
182 | n/a | |
---|
183 | n/a | @mock.patch('asyncio.unix_events.signal') |
---|
184 | n/a | @mock.patch('asyncio.base_events.logger') |
---|
185 | n/a | def test_remove_signal_handler_cleanup_error(self, m_logging, m_signal): |
---|
186 | n/a | m_signal.NSIG = signal.NSIG |
---|
187 | n/a | self.loop.add_signal_handler(signal.SIGHUP, lambda: True) |
---|
188 | n/a | |
---|
189 | n/a | m_signal.set_wakeup_fd.side_effect = ValueError |
---|
190 | n/a | |
---|
191 | n/a | self.loop.remove_signal_handler(signal.SIGHUP) |
---|
192 | n/a | self.assertTrue(m_logging.info) |
---|
193 | n/a | |
---|
194 | n/a | @mock.patch('asyncio.unix_events.signal') |
---|
195 | n/a | def test_remove_signal_handler_error(self, m_signal): |
---|
196 | n/a | m_signal.NSIG = signal.NSIG |
---|
197 | n/a | self.loop.add_signal_handler(signal.SIGHUP, lambda: True) |
---|
198 | n/a | |
---|
199 | n/a | m_signal.signal.side_effect = OSError |
---|
200 | n/a | |
---|
201 | n/a | self.assertRaises( |
---|
202 | n/a | OSError, self.loop.remove_signal_handler, signal.SIGHUP) |
---|
203 | n/a | |
---|
204 | n/a | @mock.patch('asyncio.unix_events.signal') |
---|
205 | n/a | def test_remove_signal_handler_error2(self, m_signal): |
---|
206 | n/a | m_signal.NSIG = signal.NSIG |
---|
207 | n/a | self.loop.add_signal_handler(signal.SIGHUP, lambda: True) |
---|
208 | n/a | |
---|
209 | n/a | class Err(OSError): |
---|
210 | n/a | errno = errno.EINVAL |
---|
211 | n/a | m_signal.signal.side_effect = Err |
---|
212 | n/a | |
---|
213 | n/a | self.assertRaises( |
---|
214 | n/a | RuntimeError, self.loop.remove_signal_handler, signal.SIGHUP) |
---|
215 | n/a | |
---|
216 | n/a | @mock.patch('asyncio.unix_events.signal') |
---|
217 | n/a | def test_close(self, m_signal): |
---|
218 | n/a | m_signal.NSIG = signal.NSIG |
---|
219 | n/a | |
---|
220 | n/a | self.loop.add_signal_handler(signal.SIGHUP, lambda: True) |
---|
221 | n/a | self.loop.add_signal_handler(signal.SIGCHLD, lambda: True) |
---|
222 | n/a | |
---|
223 | n/a | self.assertEqual(len(self.loop._signal_handlers), 2) |
---|
224 | n/a | |
---|
225 | n/a | m_signal.set_wakeup_fd.reset_mock() |
---|
226 | n/a | |
---|
227 | n/a | self.loop.close() |
---|
228 | n/a | |
---|
229 | n/a | self.assertEqual(len(self.loop._signal_handlers), 0) |
---|
230 | n/a | m_signal.set_wakeup_fd.assert_called_once_with(-1) |
---|
231 | n/a | |
---|
232 | n/a | |
---|
233 | n/a | @unittest.skipUnless(hasattr(socket, 'AF_UNIX'), |
---|
234 | n/a | 'UNIX Sockets are not supported') |
---|
235 | n/a | class SelectorEventLoopUnixSocketTests(test_utils.TestCase): |
---|
236 | n/a | |
---|
237 | n/a | def setUp(self): |
---|
238 | n/a | super().setUp() |
---|
239 | n/a | self.loop = asyncio.SelectorEventLoop() |
---|
240 | n/a | self.set_event_loop(self.loop) |
---|
241 | n/a | |
---|
242 | n/a | def test_create_unix_server_existing_path_sock(self): |
---|
243 | n/a | with test_utils.unix_socket_path() as path: |
---|
244 | n/a | sock = socket.socket(socket.AF_UNIX) |
---|
245 | n/a | sock.bind(path) |
---|
246 | n/a | sock.listen(1) |
---|
247 | n/a | sock.close() |
---|
248 | n/a | |
---|
249 | n/a | coro = self.loop.create_unix_server(lambda: None, path) |
---|
250 | n/a | srv = self.loop.run_until_complete(coro) |
---|
251 | n/a | srv.close() |
---|
252 | n/a | self.loop.run_until_complete(srv.wait_closed()) |
---|
253 | n/a | |
---|
254 | n/a | @unittest.skipUnless(hasattr(os, 'fspath'), 'no os.fspath') |
---|
255 | n/a | def test_create_unix_server_pathlib(self): |
---|
256 | n/a | with test_utils.unix_socket_path() as path: |
---|
257 | n/a | path = pathlib.Path(path) |
---|
258 | n/a | srv_coro = self.loop.create_unix_server(lambda: None, path) |
---|
259 | n/a | srv = self.loop.run_until_complete(srv_coro) |
---|
260 | n/a | srv.close() |
---|
261 | n/a | self.loop.run_until_complete(srv.wait_closed()) |
---|
262 | n/a | |
---|
263 | n/a | def test_create_unix_server_existing_path_nonsock(self): |
---|
264 | n/a | with tempfile.NamedTemporaryFile() as file: |
---|
265 | n/a | coro = self.loop.create_unix_server(lambda: None, file.name) |
---|
266 | n/a | with self.assertRaisesRegex(OSError, |
---|
267 | n/a | 'Address.*is already in use'): |
---|
268 | n/a | self.loop.run_until_complete(coro) |
---|
269 | n/a | |
---|
270 | n/a | def test_create_unix_server_ssl_bool(self): |
---|
271 | n/a | coro = self.loop.create_unix_server(lambda: None, path='spam', |
---|
272 | n/a | ssl=True) |
---|
273 | n/a | with self.assertRaisesRegex(TypeError, |
---|
274 | n/a | 'ssl argument must be an SSLContext'): |
---|
275 | n/a | self.loop.run_until_complete(coro) |
---|
276 | n/a | |
---|
277 | n/a | def test_create_unix_server_nopath_nosock(self): |
---|
278 | n/a | coro = self.loop.create_unix_server(lambda: None, path=None) |
---|
279 | n/a | with self.assertRaisesRegex(ValueError, |
---|
280 | n/a | 'path was not specified, and no sock'): |
---|
281 | n/a | self.loop.run_until_complete(coro) |
---|
282 | n/a | |
---|
283 | n/a | def test_create_unix_server_path_inetsock(self): |
---|
284 | n/a | sock = socket.socket() |
---|
285 | n/a | with sock: |
---|
286 | n/a | coro = self.loop.create_unix_server(lambda: None, path=None, |
---|
287 | n/a | sock=sock) |
---|
288 | n/a | with self.assertRaisesRegex(ValueError, |
---|
289 | n/a | 'A UNIX Domain Stream.*was expected'): |
---|
290 | n/a | self.loop.run_until_complete(coro) |
---|
291 | n/a | |
---|
292 | n/a | def test_create_unix_server_path_dgram(self): |
---|
293 | n/a | sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) |
---|
294 | n/a | with sock: |
---|
295 | n/a | coro = self.loop.create_unix_server(lambda: None, path=None, |
---|
296 | n/a | sock=sock) |
---|
297 | n/a | with self.assertRaisesRegex(ValueError, |
---|
298 | n/a | 'A UNIX Domain Stream.*was expected'): |
---|
299 | n/a | self.loop.run_until_complete(coro) |
---|
300 | n/a | |
---|
301 | n/a | @unittest.skipUnless(hasattr(socket, 'SOCK_NONBLOCK'), |
---|
302 | n/a | 'no socket.SOCK_NONBLOCK (linux only)') |
---|
303 | n/a | def test_create_unix_server_path_stream_bittype(self): |
---|
304 | n/a | sock = socket.socket( |
---|
305 | n/a | socket.AF_UNIX, socket.SOCK_STREAM | socket.SOCK_NONBLOCK) |
---|
306 | n/a | with tempfile.NamedTemporaryFile() as file: |
---|
307 | n/a | fn = file.name |
---|
308 | n/a | try: |
---|
309 | n/a | with sock: |
---|
310 | n/a | sock.bind(fn) |
---|
311 | n/a | coro = self.loop.create_unix_server(lambda: None, path=None, |
---|
312 | n/a | sock=sock) |
---|
313 | n/a | srv = self.loop.run_until_complete(coro) |
---|
314 | n/a | srv.close() |
---|
315 | n/a | self.loop.run_until_complete(srv.wait_closed()) |
---|
316 | n/a | finally: |
---|
317 | n/a | os.unlink(fn) |
---|
318 | n/a | |
---|
319 | n/a | def test_create_unix_connection_path_inetsock(self): |
---|
320 | n/a | sock = socket.socket() |
---|
321 | n/a | with sock: |
---|
322 | n/a | coro = self.loop.create_unix_connection(lambda: None, path=None, |
---|
323 | n/a | sock=sock) |
---|
324 | n/a | with self.assertRaisesRegex(ValueError, |
---|
325 | n/a | 'A UNIX Domain Stream.*was expected'): |
---|
326 | n/a | self.loop.run_until_complete(coro) |
---|
327 | n/a | |
---|
328 | n/a | @mock.patch('asyncio.unix_events.socket') |
---|
329 | n/a | def test_create_unix_server_bind_error(self, m_socket): |
---|
330 | n/a | # Ensure that the socket is closed on any bind error |
---|
331 | n/a | sock = mock.Mock() |
---|
332 | n/a | m_socket.socket.return_value = sock |
---|
333 | n/a | |
---|
334 | n/a | sock.bind.side_effect = OSError |
---|
335 | n/a | coro = self.loop.create_unix_server(lambda: None, path="/test") |
---|
336 | n/a | with self.assertRaises(OSError): |
---|
337 | n/a | self.loop.run_until_complete(coro) |
---|
338 | n/a | self.assertTrue(sock.close.called) |
---|
339 | n/a | |
---|
340 | n/a | sock.bind.side_effect = MemoryError |
---|
341 | n/a | coro = self.loop.create_unix_server(lambda: None, path="/test") |
---|
342 | n/a | with self.assertRaises(MemoryError): |
---|
343 | n/a | self.loop.run_until_complete(coro) |
---|
344 | n/a | self.assertTrue(sock.close.called) |
---|
345 | n/a | |
---|
346 | n/a | def test_create_unix_connection_path_sock(self): |
---|
347 | n/a | coro = self.loop.create_unix_connection( |
---|
348 | n/a | lambda: None, os.devnull, sock=object()) |
---|
349 | n/a | with self.assertRaisesRegex(ValueError, 'path and sock can not be'): |
---|
350 | n/a | self.loop.run_until_complete(coro) |
---|
351 | n/a | |
---|
352 | n/a | def test_create_unix_connection_nopath_nosock(self): |
---|
353 | n/a | coro = self.loop.create_unix_connection( |
---|
354 | n/a | lambda: None, None) |
---|
355 | n/a | with self.assertRaisesRegex(ValueError, |
---|
356 | n/a | 'no path and sock were specified'): |
---|
357 | n/a | self.loop.run_until_complete(coro) |
---|
358 | n/a | |
---|
359 | n/a | def test_create_unix_connection_nossl_serverhost(self): |
---|
360 | n/a | coro = self.loop.create_unix_connection( |
---|
361 | n/a | lambda: None, os.devnull, server_hostname='spam') |
---|
362 | n/a | with self.assertRaisesRegex(ValueError, |
---|
363 | n/a | 'server_hostname is only meaningful'): |
---|
364 | n/a | self.loop.run_until_complete(coro) |
---|
365 | n/a | |
---|
366 | n/a | def test_create_unix_connection_ssl_noserverhost(self): |
---|
367 | n/a | coro = self.loop.create_unix_connection( |
---|
368 | n/a | lambda: None, os.devnull, ssl=True) |
---|
369 | n/a | |
---|
370 | n/a | with self.assertRaisesRegex( |
---|
371 | n/a | ValueError, 'you have to pass server_hostname when using ssl'): |
---|
372 | n/a | |
---|
373 | n/a | self.loop.run_until_complete(coro) |
---|
374 | n/a | |
---|
375 | n/a | |
---|
376 | n/a | class UnixReadPipeTransportTests(test_utils.TestCase): |
---|
377 | n/a | |
---|
378 | n/a | def setUp(self): |
---|
379 | n/a | super().setUp() |
---|
380 | n/a | self.loop = self.new_test_loop() |
---|
381 | n/a | self.protocol = test_utils.make_test_protocol(asyncio.Protocol) |
---|
382 | n/a | self.pipe = mock.Mock(spec_set=io.RawIOBase) |
---|
383 | n/a | self.pipe.fileno.return_value = 5 |
---|
384 | n/a | |
---|
385 | n/a | blocking_patcher = mock.patch('asyncio.unix_events._set_nonblocking') |
---|
386 | n/a | blocking_patcher.start() |
---|
387 | n/a | self.addCleanup(blocking_patcher.stop) |
---|
388 | n/a | |
---|
389 | n/a | fstat_patcher = mock.patch('os.fstat') |
---|
390 | n/a | m_fstat = fstat_patcher.start() |
---|
391 | n/a | st = mock.Mock() |
---|
392 | n/a | st.st_mode = stat.S_IFIFO |
---|
393 | n/a | m_fstat.return_value = st |
---|
394 | n/a | self.addCleanup(fstat_patcher.stop) |
---|
395 | n/a | |
---|
396 | n/a | def read_pipe_transport(self, waiter=None): |
---|
397 | n/a | transport = unix_events._UnixReadPipeTransport(self.loop, self.pipe, |
---|
398 | n/a | self.protocol, |
---|
399 | n/a | waiter=waiter) |
---|
400 | n/a | self.addCleanup(close_pipe_transport, transport) |
---|
401 | n/a | return transport |
---|
402 | n/a | |
---|
403 | n/a | def test_ctor(self): |
---|
404 | n/a | waiter = asyncio.Future(loop=self.loop) |
---|
405 | n/a | tr = self.read_pipe_transport(waiter=waiter) |
---|
406 | n/a | self.loop.run_until_complete(waiter) |
---|
407 | n/a | |
---|
408 | n/a | self.protocol.connection_made.assert_called_with(tr) |
---|
409 | n/a | self.loop.assert_reader(5, tr._read_ready) |
---|
410 | n/a | self.assertIsNone(waiter.result()) |
---|
411 | n/a | |
---|
412 | n/a | @mock.patch('os.read') |
---|
413 | n/a | def test__read_ready(self, m_read): |
---|
414 | n/a | tr = self.read_pipe_transport() |
---|
415 | n/a | m_read.return_value = b'data' |
---|
416 | n/a | tr._read_ready() |
---|
417 | n/a | |
---|
418 | n/a | m_read.assert_called_with(5, tr.max_size) |
---|
419 | n/a | self.protocol.data_received.assert_called_with(b'data') |
---|
420 | n/a | |
---|
421 | n/a | @mock.patch('os.read') |
---|
422 | n/a | def test__read_ready_eof(self, m_read): |
---|
423 | n/a | tr = self.read_pipe_transport() |
---|
424 | n/a | m_read.return_value = b'' |
---|
425 | n/a | tr._read_ready() |
---|
426 | n/a | |
---|
427 | n/a | m_read.assert_called_with(5, tr.max_size) |
---|
428 | n/a | self.assertFalse(self.loop.readers) |
---|
429 | n/a | test_utils.run_briefly(self.loop) |
---|
430 | n/a | self.protocol.eof_received.assert_called_with() |
---|
431 | n/a | self.protocol.connection_lost.assert_called_with(None) |
---|
432 | n/a | |
---|
433 | n/a | @mock.patch('os.read') |
---|
434 | n/a | def test__read_ready_blocked(self, m_read): |
---|
435 | n/a | tr = self.read_pipe_transport() |
---|
436 | n/a | m_read.side_effect = BlockingIOError |
---|
437 | n/a | tr._read_ready() |
---|
438 | n/a | |
---|
439 | n/a | m_read.assert_called_with(5, tr.max_size) |
---|
440 | n/a | test_utils.run_briefly(self.loop) |
---|
441 | n/a | self.assertFalse(self.protocol.data_received.called) |
---|
442 | n/a | |
---|
443 | n/a | @mock.patch('asyncio.log.logger.error') |
---|
444 | n/a | @mock.patch('os.read') |
---|
445 | n/a | def test__read_ready_error(self, m_read, m_logexc): |
---|
446 | n/a | tr = self.read_pipe_transport() |
---|
447 | n/a | err = OSError() |
---|
448 | n/a | m_read.side_effect = err |
---|
449 | n/a | tr._close = mock.Mock() |
---|
450 | n/a | tr._read_ready() |
---|
451 | n/a | |
---|
452 | n/a | m_read.assert_called_with(5, tr.max_size) |
---|
453 | n/a | tr._close.assert_called_with(err) |
---|
454 | n/a | m_logexc.assert_called_with( |
---|
455 | n/a | test_utils.MockPattern( |
---|
456 | n/a | 'Fatal read error on pipe transport' |
---|
457 | n/a | '\nprotocol:.*\ntransport:.*'), |
---|
458 | n/a | exc_info=(OSError, MOCK_ANY, MOCK_ANY)) |
---|
459 | n/a | |
---|
460 | n/a | @mock.patch('os.read') |
---|
461 | n/a | def test_pause_reading(self, m_read): |
---|
462 | n/a | tr = self.read_pipe_transport() |
---|
463 | n/a | m = mock.Mock() |
---|
464 | n/a | self.loop.add_reader(5, m) |
---|
465 | n/a | tr.pause_reading() |
---|
466 | n/a | self.assertFalse(self.loop.readers) |
---|
467 | n/a | |
---|
468 | n/a | @mock.patch('os.read') |
---|
469 | n/a | def test_resume_reading(self, m_read): |
---|
470 | n/a | tr = self.read_pipe_transport() |
---|
471 | n/a | tr.resume_reading() |
---|
472 | n/a | self.loop.assert_reader(5, tr._read_ready) |
---|
473 | n/a | |
---|
474 | n/a | @mock.patch('os.read') |
---|
475 | n/a | def test_close(self, m_read): |
---|
476 | n/a | tr = self.read_pipe_transport() |
---|
477 | n/a | tr._close = mock.Mock() |
---|
478 | n/a | tr.close() |
---|
479 | n/a | tr._close.assert_called_with(None) |
---|
480 | n/a | |
---|
481 | n/a | @mock.patch('os.read') |
---|
482 | n/a | def test_close_already_closing(self, m_read): |
---|
483 | n/a | tr = self.read_pipe_transport() |
---|
484 | n/a | tr._closing = True |
---|
485 | n/a | tr._close = mock.Mock() |
---|
486 | n/a | tr.close() |
---|
487 | n/a | self.assertFalse(tr._close.called) |
---|
488 | n/a | |
---|
489 | n/a | @mock.patch('os.read') |
---|
490 | n/a | def test__close(self, m_read): |
---|
491 | n/a | tr = self.read_pipe_transport() |
---|
492 | n/a | err = object() |
---|
493 | n/a | tr._close(err) |
---|
494 | n/a | self.assertTrue(tr.is_closing()) |
---|
495 | n/a | self.assertFalse(self.loop.readers) |
---|
496 | n/a | test_utils.run_briefly(self.loop) |
---|
497 | n/a | self.protocol.connection_lost.assert_called_with(err) |
---|
498 | n/a | |
---|
499 | n/a | def test__call_connection_lost(self): |
---|
500 | n/a | tr = self.read_pipe_transport() |
---|
501 | n/a | self.assertIsNotNone(tr._protocol) |
---|
502 | n/a | self.assertIsNotNone(tr._loop) |
---|
503 | n/a | |
---|
504 | n/a | err = None |
---|
505 | n/a | tr._call_connection_lost(err) |
---|
506 | n/a | self.protocol.connection_lost.assert_called_with(err) |
---|
507 | n/a | self.pipe.close.assert_called_with() |
---|
508 | n/a | |
---|
509 | n/a | self.assertIsNone(tr._protocol) |
---|
510 | n/a | self.assertIsNone(tr._loop) |
---|
511 | n/a | |
---|
512 | n/a | def test__call_connection_lost_with_err(self): |
---|
513 | n/a | tr = self.read_pipe_transport() |
---|
514 | n/a | self.assertIsNotNone(tr._protocol) |
---|
515 | n/a | self.assertIsNotNone(tr._loop) |
---|
516 | n/a | |
---|
517 | n/a | err = OSError() |
---|
518 | n/a | tr._call_connection_lost(err) |
---|
519 | n/a | self.protocol.connection_lost.assert_called_with(err) |
---|
520 | n/a | self.pipe.close.assert_called_with() |
---|
521 | n/a | |
---|
522 | n/a | self.assertIsNone(tr._protocol) |
---|
523 | n/a | self.assertIsNone(tr._loop) |
---|
524 | n/a | |
---|
525 | n/a | |
---|
526 | n/a | class UnixWritePipeTransportTests(test_utils.TestCase): |
---|
527 | n/a | |
---|
528 | n/a | def setUp(self): |
---|
529 | n/a | super().setUp() |
---|
530 | n/a | self.loop = self.new_test_loop() |
---|
531 | n/a | self.protocol = test_utils.make_test_protocol(asyncio.BaseProtocol) |
---|
532 | n/a | self.pipe = mock.Mock(spec_set=io.RawIOBase) |
---|
533 | n/a | self.pipe.fileno.return_value = 5 |
---|
534 | n/a | |
---|
535 | n/a | blocking_patcher = mock.patch('asyncio.unix_events._set_nonblocking') |
---|
536 | n/a | blocking_patcher.start() |
---|
537 | n/a | self.addCleanup(blocking_patcher.stop) |
---|
538 | n/a | |
---|
539 | n/a | fstat_patcher = mock.patch('os.fstat') |
---|
540 | n/a | m_fstat = fstat_patcher.start() |
---|
541 | n/a | st = mock.Mock() |
---|
542 | n/a | st.st_mode = stat.S_IFSOCK |
---|
543 | n/a | m_fstat.return_value = st |
---|
544 | n/a | self.addCleanup(fstat_patcher.stop) |
---|
545 | n/a | |
---|
546 | n/a | def write_pipe_transport(self, waiter=None): |
---|
547 | n/a | transport = unix_events._UnixWritePipeTransport(self.loop, self.pipe, |
---|
548 | n/a | self.protocol, |
---|
549 | n/a | waiter=waiter) |
---|
550 | n/a | self.addCleanup(close_pipe_transport, transport) |
---|
551 | n/a | return transport |
---|
552 | n/a | |
---|
553 | n/a | def test_ctor(self): |
---|
554 | n/a | waiter = asyncio.Future(loop=self.loop) |
---|
555 | n/a | tr = self.write_pipe_transport(waiter=waiter) |
---|
556 | n/a | self.loop.run_until_complete(waiter) |
---|
557 | n/a | |
---|
558 | n/a | self.protocol.connection_made.assert_called_with(tr) |
---|
559 | n/a | self.loop.assert_reader(5, tr._read_ready) |
---|
560 | n/a | self.assertEqual(None, waiter.result()) |
---|
561 | n/a | |
---|
562 | n/a | def test_can_write_eof(self): |
---|
563 | n/a | tr = self.write_pipe_transport() |
---|
564 | n/a | self.assertTrue(tr.can_write_eof()) |
---|
565 | n/a | |
---|
566 | n/a | @mock.patch('os.write') |
---|
567 | n/a | def test_write(self, m_write): |
---|
568 | n/a | tr = self.write_pipe_transport() |
---|
569 | n/a | m_write.return_value = 4 |
---|
570 | n/a | tr.write(b'data') |
---|
571 | n/a | m_write.assert_called_with(5, b'data') |
---|
572 | n/a | self.assertFalse(self.loop.writers) |
---|
573 | n/a | self.assertEqual(bytearray(), tr._buffer) |
---|
574 | n/a | |
---|
575 | n/a | @mock.patch('os.write') |
---|
576 | n/a | def test_write_no_data(self, m_write): |
---|
577 | n/a | tr = self.write_pipe_transport() |
---|
578 | n/a | tr.write(b'') |
---|
579 | n/a | self.assertFalse(m_write.called) |
---|
580 | n/a | self.assertFalse(self.loop.writers) |
---|
581 | n/a | self.assertEqual(bytearray(b''), tr._buffer) |
---|
582 | n/a | |
---|
583 | n/a | @mock.patch('os.write') |
---|
584 | n/a | def test_write_partial(self, m_write): |
---|
585 | n/a | tr = self.write_pipe_transport() |
---|
586 | n/a | m_write.return_value = 2 |
---|
587 | n/a | tr.write(b'data') |
---|
588 | n/a | self.loop.assert_writer(5, tr._write_ready) |
---|
589 | n/a | self.assertEqual(bytearray(b'ta'), tr._buffer) |
---|
590 | n/a | |
---|
591 | n/a | @mock.patch('os.write') |
---|
592 | n/a | def test_write_buffer(self, m_write): |
---|
593 | n/a | tr = self.write_pipe_transport() |
---|
594 | n/a | self.loop.add_writer(5, tr._write_ready) |
---|
595 | n/a | tr._buffer = bytearray(b'previous') |
---|
596 | n/a | tr.write(b'data') |
---|
597 | n/a | self.assertFalse(m_write.called) |
---|
598 | n/a | self.loop.assert_writer(5, tr._write_ready) |
---|
599 | n/a | self.assertEqual(bytearray(b'previousdata'), tr._buffer) |
---|
600 | n/a | |
---|
601 | n/a | @mock.patch('os.write') |
---|
602 | n/a | def test_write_again(self, m_write): |
---|
603 | n/a | tr = self.write_pipe_transport() |
---|
604 | n/a | m_write.side_effect = BlockingIOError() |
---|
605 | n/a | tr.write(b'data') |
---|
606 | n/a | m_write.assert_called_with(5, bytearray(b'data')) |
---|
607 | n/a | self.loop.assert_writer(5, tr._write_ready) |
---|
608 | n/a | self.assertEqual(bytearray(b'data'), tr._buffer) |
---|
609 | n/a | |
---|
610 | n/a | @mock.patch('asyncio.unix_events.logger') |
---|
611 | n/a | @mock.patch('os.write') |
---|
612 | n/a | def test_write_err(self, m_write, m_log): |
---|
613 | n/a | tr = self.write_pipe_transport() |
---|
614 | n/a | err = OSError() |
---|
615 | n/a | m_write.side_effect = err |
---|
616 | n/a | tr._fatal_error = mock.Mock() |
---|
617 | n/a | tr.write(b'data') |
---|
618 | n/a | m_write.assert_called_with(5, b'data') |
---|
619 | n/a | self.assertFalse(self.loop.writers) |
---|
620 | n/a | self.assertEqual(bytearray(), tr._buffer) |
---|
621 | n/a | tr._fatal_error.assert_called_with( |
---|
622 | n/a | err, |
---|
623 | n/a | 'Fatal write error on pipe transport') |
---|
624 | n/a | self.assertEqual(1, tr._conn_lost) |
---|
625 | n/a | |
---|
626 | n/a | tr.write(b'data') |
---|
627 | n/a | self.assertEqual(2, tr._conn_lost) |
---|
628 | n/a | tr.write(b'data') |
---|
629 | n/a | tr.write(b'data') |
---|
630 | n/a | tr.write(b'data') |
---|
631 | n/a | tr.write(b'data') |
---|
632 | n/a | # This is a bit overspecified. :-( |
---|
633 | n/a | m_log.warning.assert_called_with( |
---|
634 | n/a | 'pipe closed by peer or os.write(pipe, data) raised exception.') |
---|
635 | n/a | tr.close() |
---|
636 | n/a | |
---|
637 | n/a | @mock.patch('os.write') |
---|
638 | n/a | def test_write_close(self, m_write): |
---|
639 | n/a | tr = self.write_pipe_transport() |
---|
640 | n/a | tr._read_ready() # pipe was closed by peer |
---|
641 | n/a | |
---|
642 | n/a | tr.write(b'data') |
---|
643 | n/a | self.assertEqual(tr._conn_lost, 1) |
---|
644 | n/a | tr.write(b'data') |
---|
645 | n/a | self.assertEqual(tr._conn_lost, 2) |
---|
646 | n/a | |
---|
647 | n/a | def test__read_ready(self): |
---|
648 | n/a | tr = self.write_pipe_transport() |
---|
649 | n/a | tr._read_ready() |
---|
650 | n/a | self.assertFalse(self.loop.readers) |
---|
651 | n/a | self.assertFalse(self.loop.writers) |
---|
652 | n/a | self.assertTrue(tr.is_closing()) |
---|
653 | n/a | test_utils.run_briefly(self.loop) |
---|
654 | n/a | self.protocol.connection_lost.assert_called_with(None) |
---|
655 | n/a | |
---|
656 | n/a | @mock.patch('os.write') |
---|
657 | n/a | def test__write_ready(self, m_write): |
---|
658 | n/a | tr = self.write_pipe_transport() |
---|
659 | n/a | self.loop.add_writer(5, tr._write_ready) |
---|
660 | n/a | tr._buffer = bytearray(b'data') |
---|
661 | n/a | m_write.return_value = 4 |
---|
662 | n/a | tr._write_ready() |
---|
663 | n/a | self.assertFalse(self.loop.writers) |
---|
664 | n/a | self.assertEqual(bytearray(), tr._buffer) |
---|
665 | n/a | |
---|
666 | n/a | @mock.patch('os.write') |
---|
667 | n/a | def test__write_ready_partial(self, m_write): |
---|
668 | n/a | tr = self.write_pipe_transport() |
---|
669 | n/a | self.loop.add_writer(5, tr._write_ready) |
---|
670 | n/a | tr._buffer = bytearray(b'data') |
---|
671 | n/a | m_write.return_value = 3 |
---|
672 | n/a | tr._write_ready() |
---|
673 | n/a | self.loop.assert_writer(5, tr._write_ready) |
---|
674 | n/a | self.assertEqual(bytearray(b'a'), tr._buffer) |
---|
675 | n/a | |
---|
676 | n/a | @mock.patch('os.write') |
---|
677 | n/a | def test__write_ready_again(self, m_write): |
---|
678 | n/a | tr = self.write_pipe_transport() |
---|
679 | n/a | self.loop.add_writer(5, tr._write_ready) |
---|
680 | n/a | tr._buffer = bytearray(b'data') |
---|
681 | n/a | m_write.side_effect = BlockingIOError() |
---|
682 | n/a | tr._write_ready() |
---|
683 | n/a | m_write.assert_called_with(5, bytearray(b'data')) |
---|
684 | n/a | self.loop.assert_writer(5, tr._write_ready) |
---|
685 | n/a | self.assertEqual(bytearray(b'data'), tr._buffer) |
---|
686 | n/a | |
---|
687 | n/a | @mock.patch('os.write') |
---|
688 | n/a | def test__write_ready_empty(self, m_write): |
---|
689 | n/a | tr = self.write_pipe_transport() |
---|
690 | n/a | self.loop.add_writer(5, tr._write_ready) |
---|
691 | n/a | tr._buffer = bytearray(b'data') |
---|
692 | n/a | m_write.return_value = 0 |
---|
693 | n/a | tr._write_ready() |
---|
694 | n/a | m_write.assert_called_with(5, bytearray(b'data')) |
---|
695 | n/a | self.loop.assert_writer(5, tr._write_ready) |
---|
696 | n/a | self.assertEqual(bytearray(b'data'), tr._buffer) |
---|
697 | n/a | |
---|
698 | n/a | @mock.patch('asyncio.log.logger.error') |
---|
699 | n/a | @mock.patch('os.write') |
---|
700 | n/a | def test__write_ready_err(self, m_write, m_logexc): |
---|
701 | n/a | tr = self.write_pipe_transport() |
---|
702 | n/a | self.loop.add_writer(5, tr._write_ready) |
---|
703 | n/a | tr._buffer = bytearray(b'data') |
---|
704 | n/a | m_write.side_effect = err = OSError() |
---|
705 | n/a | tr._write_ready() |
---|
706 | n/a | self.assertFalse(self.loop.writers) |
---|
707 | n/a | self.assertFalse(self.loop.readers) |
---|
708 | n/a | self.assertEqual(bytearray(), tr._buffer) |
---|
709 | n/a | self.assertTrue(tr.is_closing()) |
---|
710 | n/a | m_logexc.assert_called_with( |
---|
711 | n/a | test_utils.MockPattern( |
---|
712 | n/a | 'Fatal write error on pipe transport' |
---|
713 | n/a | '\nprotocol:.*\ntransport:.*'), |
---|
714 | n/a | exc_info=(OSError, MOCK_ANY, MOCK_ANY)) |
---|
715 | n/a | self.assertEqual(1, tr._conn_lost) |
---|
716 | n/a | test_utils.run_briefly(self.loop) |
---|
717 | n/a | self.protocol.connection_lost.assert_called_with(err) |
---|
718 | n/a | |
---|
719 | n/a | @mock.patch('os.write') |
---|
720 | n/a | def test__write_ready_closing(self, m_write): |
---|
721 | n/a | tr = self.write_pipe_transport() |
---|
722 | n/a | self.loop.add_writer(5, tr._write_ready) |
---|
723 | n/a | tr._closing = True |
---|
724 | n/a | tr._buffer = bytearray(b'data') |
---|
725 | n/a | m_write.return_value = 4 |
---|
726 | n/a | tr._write_ready() |
---|
727 | n/a | self.assertFalse(self.loop.writers) |
---|
728 | n/a | self.assertFalse(self.loop.readers) |
---|
729 | n/a | self.assertEqual(bytearray(), tr._buffer) |
---|
730 | n/a | self.protocol.connection_lost.assert_called_with(None) |
---|
731 | n/a | self.pipe.close.assert_called_with() |
---|
732 | n/a | |
---|
733 | n/a | @mock.patch('os.write') |
---|
734 | n/a | def test_abort(self, m_write): |
---|
735 | n/a | tr = self.write_pipe_transport() |
---|
736 | n/a | self.loop.add_writer(5, tr._write_ready) |
---|
737 | n/a | self.loop.add_reader(5, tr._read_ready) |
---|
738 | n/a | tr._buffer = [b'da', b'ta'] |
---|
739 | n/a | tr.abort() |
---|
740 | n/a | self.assertFalse(m_write.called) |
---|
741 | n/a | self.assertFalse(self.loop.readers) |
---|
742 | n/a | self.assertFalse(self.loop.writers) |
---|
743 | n/a | self.assertEqual([], tr._buffer) |
---|
744 | n/a | self.assertTrue(tr.is_closing()) |
---|
745 | n/a | test_utils.run_briefly(self.loop) |
---|
746 | n/a | self.protocol.connection_lost.assert_called_with(None) |
---|
747 | n/a | |
---|
748 | n/a | def test__call_connection_lost(self): |
---|
749 | n/a | tr = self.write_pipe_transport() |
---|
750 | n/a | self.assertIsNotNone(tr._protocol) |
---|
751 | n/a | self.assertIsNotNone(tr._loop) |
---|
752 | n/a | |
---|
753 | n/a | err = None |
---|
754 | n/a | tr._call_connection_lost(err) |
---|
755 | n/a | self.protocol.connection_lost.assert_called_with(err) |
---|
756 | n/a | self.pipe.close.assert_called_with() |
---|
757 | n/a | |
---|
758 | n/a | self.assertIsNone(tr._protocol) |
---|
759 | n/a | self.assertIsNone(tr._loop) |
---|
760 | n/a | |
---|
761 | n/a | def test__call_connection_lost_with_err(self): |
---|
762 | n/a | tr = self.write_pipe_transport() |
---|
763 | n/a | self.assertIsNotNone(tr._protocol) |
---|
764 | n/a | self.assertIsNotNone(tr._loop) |
---|
765 | n/a | |
---|
766 | n/a | err = OSError() |
---|
767 | n/a | tr._call_connection_lost(err) |
---|
768 | n/a | self.protocol.connection_lost.assert_called_with(err) |
---|
769 | n/a | self.pipe.close.assert_called_with() |
---|
770 | n/a | |
---|
771 | n/a | self.assertIsNone(tr._protocol) |
---|
772 | n/a | self.assertIsNone(tr._loop) |
---|
773 | n/a | |
---|
774 | n/a | def test_close(self): |
---|
775 | n/a | tr = self.write_pipe_transport() |
---|
776 | n/a | tr.write_eof = mock.Mock() |
---|
777 | n/a | tr.close() |
---|
778 | n/a | tr.write_eof.assert_called_with() |
---|
779 | n/a | |
---|
780 | n/a | # closing the transport twice must not fail |
---|
781 | n/a | tr.close() |
---|
782 | n/a | |
---|
783 | n/a | def test_close_closing(self): |
---|
784 | n/a | tr = self.write_pipe_transport() |
---|
785 | n/a | tr.write_eof = mock.Mock() |
---|
786 | n/a | tr._closing = True |
---|
787 | n/a | tr.close() |
---|
788 | n/a | self.assertFalse(tr.write_eof.called) |
---|
789 | n/a | |
---|
790 | n/a | def test_write_eof(self): |
---|
791 | n/a | tr = self.write_pipe_transport() |
---|
792 | n/a | tr.write_eof() |
---|
793 | n/a | self.assertTrue(tr.is_closing()) |
---|
794 | n/a | self.assertFalse(self.loop.readers) |
---|
795 | n/a | test_utils.run_briefly(self.loop) |
---|
796 | n/a | self.protocol.connection_lost.assert_called_with(None) |
---|
797 | n/a | |
---|
798 | n/a | def test_write_eof_pending(self): |
---|
799 | n/a | tr = self.write_pipe_transport() |
---|
800 | n/a | tr._buffer = [b'data'] |
---|
801 | n/a | tr.write_eof() |
---|
802 | n/a | self.assertTrue(tr.is_closing()) |
---|
803 | n/a | self.assertFalse(self.protocol.connection_lost.called) |
---|
804 | n/a | |
---|
805 | n/a | |
---|
806 | n/a | class AbstractChildWatcherTests(unittest.TestCase): |
---|
807 | n/a | |
---|
808 | n/a | def test_not_implemented(self): |
---|
809 | n/a | f = mock.Mock() |
---|
810 | n/a | watcher = asyncio.AbstractChildWatcher() |
---|
811 | n/a | self.assertRaises( |
---|
812 | n/a | NotImplementedError, watcher.add_child_handler, f, f) |
---|
813 | n/a | self.assertRaises( |
---|
814 | n/a | NotImplementedError, watcher.remove_child_handler, f) |
---|
815 | n/a | self.assertRaises( |
---|
816 | n/a | NotImplementedError, watcher.attach_loop, f) |
---|
817 | n/a | self.assertRaises( |
---|
818 | n/a | NotImplementedError, watcher.close) |
---|
819 | n/a | self.assertRaises( |
---|
820 | n/a | NotImplementedError, watcher.__enter__) |
---|
821 | n/a | self.assertRaises( |
---|
822 | n/a | NotImplementedError, watcher.__exit__, f, f, f) |
---|
823 | n/a | |
---|
824 | n/a | |
---|
825 | n/a | class BaseChildWatcherTests(unittest.TestCase): |
---|
826 | n/a | |
---|
827 | n/a | def test_not_implemented(self): |
---|
828 | n/a | f = mock.Mock() |
---|
829 | n/a | watcher = unix_events.BaseChildWatcher() |
---|
830 | n/a | self.assertRaises( |
---|
831 | n/a | NotImplementedError, watcher._do_waitpid, f) |
---|
832 | n/a | |
---|
833 | n/a | |
---|
834 | n/a | WaitPidMocks = collections.namedtuple("WaitPidMocks", |
---|
835 | n/a | ("waitpid", |
---|
836 | n/a | "WIFEXITED", |
---|
837 | n/a | "WIFSIGNALED", |
---|
838 | n/a | "WEXITSTATUS", |
---|
839 | n/a | "WTERMSIG", |
---|
840 | n/a | )) |
---|
841 | n/a | |
---|
842 | n/a | |
---|
843 | n/a | class ChildWatcherTestsMixin: |
---|
844 | n/a | |
---|
845 | n/a | ignore_warnings = mock.patch.object(log.logger, "warning") |
---|
846 | n/a | |
---|
847 | n/a | def setUp(self): |
---|
848 | n/a | super().setUp() |
---|
849 | n/a | self.loop = self.new_test_loop() |
---|
850 | n/a | self.running = False |
---|
851 | n/a | self.zombies = {} |
---|
852 | n/a | |
---|
853 | n/a | with mock.patch.object( |
---|
854 | n/a | self.loop, "add_signal_handler") as self.m_add_signal_handler: |
---|
855 | n/a | self.watcher = self.create_watcher() |
---|
856 | n/a | self.watcher.attach_loop(self.loop) |
---|
857 | n/a | |
---|
858 | n/a | def waitpid(self, pid, flags): |
---|
859 | n/a | if isinstance(self.watcher, asyncio.SafeChildWatcher) or pid != -1: |
---|
860 | n/a | self.assertGreater(pid, 0) |
---|
861 | n/a | try: |
---|
862 | n/a | if pid < 0: |
---|
863 | n/a | return self.zombies.popitem() |
---|
864 | n/a | else: |
---|
865 | n/a | return pid, self.zombies.pop(pid) |
---|
866 | n/a | except KeyError: |
---|
867 | n/a | pass |
---|
868 | n/a | if self.running: |
---|
869 | n/a | return 0, 0 |
---|
870 | n/a | else: |
---|
871 | n/a | raise ChildProcessError() |
---|
872 | n/a | |
---|
873 | n/a | def add_zombie(self, pid, returncode): |
---|
874 | n/a | self.zombies[pid] = returncode + 32768 |
---|
875 | n/a | |
---|
876 | n/a | def WIFEXITED(self, status): |
---|
877 | n/a | return status >= 32768 |
---|
878 | n/a | |
---|
879 | n/a | def WIFSIGNALED(self, status): |
---|
880 | n/a | return 32700 < status < 32768 |
---|
881 | n/a | |
---|
882 | n/a | def WEXITSTATUS(self, status): |
---|
883 | n/a | self.assertTrue(self.WIFEXITED(status)) |
---|
884 | n/a | return status - 32768 |
---|
885 | n/a | |
---|
886 | n/a | def WTERMSIG(self, status): |
---|
887 | n/a | self.assertTrue(self.WIFSIGNALED(status)) |
---|
888 | n/a | return 32768 - status |
---|
889 | n/a | |
---|
890 | n/a | def test_create_watcher(self): |
---|
891 | n/a | self.m_add_signal_handler.assert_called_once_with( |
---|
892 | n/a | signal.SIGCHLD, self.watcher._sig_chld) |
---|
893 | n/a | |
---|
894 | n/a | def waitpid_mocks(func): |
---|
895 | n/a | def wrapped_func(self): |
---|
896 | n/a | def patch(target, wrapper): |
---|
897 | n/a | return mock.patch(target, wraps=wrapper, |
---|
898 | n/a | new_callable=mock.Mock) |
---|
899 | n/a | |
---|
900 | n/a | with patch('os.WTERMSIG', self.WTERMSIG) as m_WTERMSIG, \ |
---|
901 | n/a | patch('os.WEXITSTATUS', self.WEXITSTATUS) as m_WEXITSTATUS, \ |
---|
902 | n/a | patch('os.WIFSIGNALED', self.WIFSIGNALED) as m_WIFSIGNALED, \ |
---|
903 | n/a | patch('os.WIFEXITED', self.WIFEXITED) as m_WIFEXITED, \ |
---|
904 | n/a | patch('os.waitpid', self.waitpid) as m_waitpid: |
---|
905 | n/a | func(self, WaitPidMocks(m_waitpid, |
---|
906 | n/a | m_WIFEXITED, m_WIFSIGNALED, |
---|
907 | n/a | m_WEXITSTATUS, m_WTERMSIG, |
---|
908 | n/a | )) |
---|
909 | n/a | return wrapped_func |
---|
910 | n/a | |
---|
911 | n/a | @waitpid_mocks |
---|
912 | n/a | def test_sigchld(self, m): |
---|
913 | n/a | # register a child |
---|
914 | n/a | callback = mock.Mock() |
---|
915 | n/a | |
---|
916 | n/a | with self.watcher: |
---|
917 | n/a | self.running = True |
---|
918 | n/a | self.watcher.add_child_handler(42, callback, 9, 10, 14) |
---|
919 | n/a | |
---|
920 | n/a | self.assertFalse(callback.called) |
---|
921 | n/a | self.assertFalse(m.WIFEXITED.called) |
---|
922 | n/a | self.assertFalse(m.WIFSIGNALED.called) |
---|
923 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
924 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
925 | n/a | |
---|
926 | n/a | # child is running |
---|
927 | n/a | self.watcher._sig_chld() |
---|
928 | n/a | |
---|
929 | n/a | self.assertFalse(callback.called) |
---|
930 | n/a | self.assertFalse(m.WIFEXITED.called) |
---|
931 | n/a | self.assertFalse(m.WIFSIGNALED.called) |
---|
932 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
933 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
934 | n/a | |
---|
935 | n/a | # child terminates (returncode 12) |
---|
936 | n/a | self.running = False |
---|
937 | n/a | self.add_zombie(42, 12) |
---|
938 | n/a | self.watcher._sig_chld() |
---|
939 | n/a | |
---|
940 | n/a | self.assertTrue(m.WIFEXITED.called) |
---|
941 | n/a | self.assertTrue(m.WEXITSTATUS.called) |
---|
942 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
943 | n/a | callback.assert_called_once_with(42, 12, 9, 10, 14) |
---|
944 | n/a | |
---|
945 | n/a | m.WIFSIGNALED.reset_mock() |
---|
946 | n/a | m.WIFEXITED.reset_mock() |
---|
947 | n/a | m.WEXITSTATUS.reset_mock() |
---|
948 | n/a | callback.reset_mock() |
---|
949 | n/a | |
---|
950 | n/a | # ensure that the child is effectively reaped |
---|
951 | n/a | self.add_zombie(42, 13) |
---|
952 | n/a | with self.ignore_warnings: |
---|
953 | n/a | self.watcher._sig_chld() |
---|
954 | n/a | |
---|
955 | n/a | self.assertFalse(callback.called) |
---|
956 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
957 | n/a | |
---|
958 | n/a | m.WIFSIGNALED.reset_mock() |
---|
959 | n/a | m.WIFEXITED.reset_mock() |
---|
960 | n/a | m.WEXITSTATUS.reset_mock() |
---|
961 | n/a | |
---|
962 | n/a | # sigchld called again |
---|
963 | n/a | self.zombies.clear() |
---|
964 | n/a | self.watcher._sig_chld() |
---|
965 | n/a | |
---|
966 | n/a | self.assertFalse(callback.called) |
---|
967 | n/a | self.assertFalse(m.WIFEXITED.called) |
---|
968 | n/a | self.assertFalse(m.WIFSIGNALED.called) |
---|
969 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
970 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
971 | n/a | |
---|
972 | n/a | @waitpid_mocks |
---|
973 | n/a | def test_sigchld_two_children(self, m): |
---|
974 | n/a | callback1 = mock.Mock() |
---|
975 | n/a | callback2 = mock.Mock() |
---|
976 | n/a | |
---|
977 | n/a | # register child 1 |
---|
978 | n/a | with self.watcher: |
---|
979 | n/a | self.running = True |
---|
980 | n/a | self.watcher.add_child_handler(43, callback1, 7, 8) |
---|
981 | n/a | |
---|
982 | n/a | self.assertFalse(callback1.called) |
---|
983 | n/a | self.assertFalse(callback2.called) |
---|
984 | n/a | self.assertFalse(m.WIFEXITED.called) |
---|
985 | n/a | self.assertFalse(m.WIFSIGNALED.called) |
---|
986 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
987 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
988 | n/a | |
---|
989 | n/a | # register child 2 |
---|
990 | n/a | with self.watcher: |
---|
991 | n/a | self.watcher.add_child_handler(44, callback2, 147, 18) |
---|
992 | n/a | |
---|
993 | n/a | self.assertFalse(callback1.called) |
---|
994 | n/a | self.assertFalse(callback2.called) |
---|
995 | n/a | self.assertFalse(m.WIFEXITED.called) |
---|
996 | n/a | self.assertFalse(m.WIFSIGNALED.called) |
---|
997 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
998 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
999 | n/a | |
---|
1000 | n/a | # children are running |
---|
1001 | n/a | self.watcher._sig_chld() |
---|
1002 | n/a | |
---|
1003 | n/a | self.assertFalse(callback1.called) |
---|
1004 | n/a | self.assertFalse(callback2.called) |
---|
1005 | n/a | self.assertFalse(m.WIFEXITED.called) |
---|
1006 | n/a | self.assertFalse(m.WIFSIGNALED.called) |
---|
1007 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
1008 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
1009 | n/a | |
---|
1010 | n/a | # child 1 terminates (signal 3) |
---|
1011 | n/a | self.add_zombie(43, -3) |
---|
1012 | n/a | self.watcher._sig_chld() |
---|
1013 | n/a | |
---|
1014 | n/a | callback1.assert_called_once_with(43, -3, 7, 8) |
---|
1015 | n/a | self.assertFalse(callback2.called) |
---|
1016 | n/a | self.assertTrue(m.WIFSIGNALED.called) |
---|
1017 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
1018 | n/a | self.assertTrue(m.WTERMSIG.called) |
---|
1019 | n/a | |
---|
1020 | n/a | m.WIFSIGNALED.reset_mock() |
---|
1021 | n/a | m.WIFEXITED.reset_mock() |
---|
1022 | n/a | m.WTERMSIG.reset_mock() |
---|
1023 | n/a | callback1.reset_mock() |
---|
1024 | n/a | |
---|
1025 | n/a | # child 2 still running |
---|
1026 | n/a | self.watcher._sig_chld() |
---|
1027 | n/a | |
---|
1028 | n/a | self.assertFalse(callback1.called) |
---|
1029 | n/a | self.assertFalse(callback2.called) |
---|
1030 | n/a | self.assertFalse(m.WIFEXITED.called) |
---|
1031 | n/a | self.assertFalse(m.WIFSIGNALED.called) |
---|
1032 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
1033 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
1034 | n/a | |
---|
1035 | n/a | # child 2 terminates (code 108) |
---|
1036 | n/a | self.add_zombie(44, 108) |
---|
1037 | n/a | self.running = False |
---|
1038 | n/a | self.watcher._sig_chld() |
---|
1039 | n/a | |
---|
1040 | n/a | callback2.assert_called_once_with(44, 108, 147, 18) |
---|
1041 | n/a | self.assertFalse(callback1.called) |
---|
1042 | n/a | self.assertTrue(m.WIFEXITED.called) |
---|
1043 | n/a | self.assertTrue(m.WEXITSTATUS.called) |
---|
1044 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
1045 | n/a | |
---|
1046 | n/a | m.WIFSIGNALED.reset_mock() |
---|
1047 | n/a | m.WIFEXITED.reset_mock() |
---|
1048 | n/a | m.WEXITSTATUS.reset_mock() |
---|
1049 | n/a | callback2.reset_mock() |
---|
1050 | n/a | |
---|
1051 | n/a | # ensure that the children are effectively reaped |
---|
1052 | n/a | self.add_zombie(43, 14) |
---|
1053 | n/a | self.add_zombie(44, 15) |
---|
1054 | n/a | with self.ignore_warnings: |
---|
1055 | n/a | self.watcher._sig_chld() |
---|
1056 | n/a | |
---|
1057 | n/a | self.assertFalse(callback1.called) |
---|
1058 | n/a | self.assertFalse(callback2.called) |
---|
1059 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
1060 | n/a | |
---|
1061 | n/a | m.WIFSIGNALED.reset_mock() |
---|
1062 | n/a | m.WIFEXITED.reset_mock() |
---|
1063 | n/a | m.WEXITSTATUS.reset_mock() |
---|
1064 | n/a | |
---|
1065 | n/a | # sigchld called again |
---|
1066 | n/a | self.zombies.clear() |
---|
1067 | n/a | self.watcher._sig_chld() |
---|
1068 | n/a | |
---|
1069 | n/a | self.assertFalse(callback1.called) |
---|
1070 | n/a | self.assertFalse(callback2.called) |
---|
1071 | n/a | self.assertFalse(m.WIFEXITED.called) |
---|
1072 | n/a | self.assertFalse(m.WIFSIGNALED.called) |
---|
1073 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
1074 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
1075 | n/a | |
---|
1076 | n/a | @waitpid_mocks |
---|
1077 | n/a | def test_sigchld_two_children_terminating_together(self, m): |
---|
1078 | n/a | callback1 = mock.Mock() |
---|
1079 | n/a | callback2 = mock.Mock() |
---|
1080 | n/a | |
---|
1081 | n/a | # register child 1 |
---|
1082 | n/a | with self.watcher: |
---|
1083 | n/a | self.running = True |
---|
1084 | n/a | self.watcher.add_child_handler(45, callback1, 17, 8) |
---|
1085 | n/a | |
---|
1086 | n/a | self.assertFalse(callback1.called) |
---|
1087 | n/a | self.assertFalse(callback2.called) |
---|
1088 | n/a | self.assertFalse(m.WIFEXITED.called) |
---|
1089 | n/a | self.assertFalse(m.WIFSIGNALED.called) |
---|
1090 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
1091 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
1092 | n/a | |
---|
1093 | n/a | # register child 2 |
---|
1094 | n/a | with self.watcher: |
---|
1095 | n/a | self.watcher.add_child_handler(46, callback2, 1147, 18) |
---|
1096 | n/a | |
---|
1097 | n/a | self.assertFalse(callback1.called) |
---|
1098 | n/a | self.assertFalse(callback2.called) |
---|
1099 | n/a | self.assertFalse(m.WIFEXITED.called) |
---|
1100 | n/a | self.assertFalse(m.WIFSIGNALED.called) |
---|
1101 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
1102 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
1103 | n/a | |
---|
1104 | n/a | # children are running |
---|
1105 | n/a | self.watcher._sig_chld() |
---|
1106 | n/a | |
---|
1107 | n/a | self.assertFalse(callback1.called) |
---|
1108 | n/a | self.assertFalse(callback2.called) |
---|
1109 | n/a | self.assertFalse(m.WIFEXITED.called) |
---|
1110 | n/a | self.assertFalse(m.WIFSIGNALED.called) |
---|
1111 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
1112 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
1113 | n/a | |
---|
1114 | n/a | # child 1 terminates (code 78) |
---|
1115 | n/a | # child 2 terminates (signal 5) |
---|
1116 | n/a | self.add_zombie(45, 78) |
---|
1117 | n/a | self.add_zombie(46, -5) |
---|
1118 | n/a | self.running = False |
---|
1119 | n/a | self.watcher._sig_chld() |
---|
1120 | n/a | |
---|
1121 | n/a | callback1.assert_called_once_with(45, 78, 17, 8) |
---|
1122 | n/a | callback2.assert_called_once_with(46, -5, 1147, 18) |
---|
1123 | n/a | self.assertTrue(m.WIFSIGNALED.called) |
---|
1124 | n/a | self.assertTrue(m.WIFEXITED.called) |
---|
1125 | n/a | self.assertTrue(m.WEXITSTATUS.called) |
---|
1126 | n/a | self.assertTrue(m.WTERMSIG.called) |
---|
1127 | n/a | |
---|
1128 | n/a | m.WIFSIGNALED.reset_mock() |
---|
1129 | n/a | m.WIFEXITED.reset_mock() |
---|
1130 | n/a | m.WTERMSIG.reset_mock() |
---|
1131 | n/a | m.WEXITSTATUS.reset_mock() |
---|
1132 | n/a | callback1.reset_mock() |
---|
1133 | n/a | callback2.reset_mock() |
---|
1134 | n/a | |
---|
1135 | n/a | # ensure that the children are effectively reaped |
---|
1136 | n/a | self.add_zombie(45, 14) |
---|
1137 | n/a | self.add_zombie(46, 15) |
---|
1138 | n/a | with self.ignore_warnings: |
---|
1139 | n/a | self.watcher._sig_chld() |
---|
1140 | n/a | |
---|
1141 | n/a | self.assertFalse(callback1.called) |
---|
1142 | n/a | self.assertFalse(callback2.called) |
---|
1143 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
1144 | n/a | |
---|
1145 | n/a | @waitpid_mocks |
---|
1146 | n/a | def test_sigchld_race_condition(self, m): |
---|
1147 | n/a | # register a child |
---|
1148 | n/a | callback = mock.Mock() |
---|
1149 | n/a | |
---|
1150 | n/a | with self.watcher: |
---|
1151 | n/a | # child terminates before being registered |
---|
1152 | n/a | self.add_zombie(50, 4) |
---|
1153 | n/a | self.watcher._sig_chld() |
---|
1154 | n/a | |
---|
1155 | n/a | self.watcher.add_child_handler(50, callback, 1, 12) |
---|
1156 | n/a | |
---|
1157 | n/a | callback.assert_called_once_with(50, 4, 1, 12) |
---|
1158 | n/a | callback.reset_mock() |
---|
1159 | n/a | |
---|
1160 | n/a | # ensure that the child is effectively reaped |
---|
1161 | n/a | self.add_zombie(50, -1) |
---|
1162 | n/a | with self.ignore_warnings: |
---|
1163 | n/a | self.watcher._sig_chld() |
---|
1164 | n/a | |
---|
1165 | n/a | self.assertFalse(callback.called) |
---|
1166 | n/a | |
---|
1167 | n/a | @waitpid_mocks |
---|
1168 | n/a | def test_sigchld_replace_handler(self, m): |
---|
1169 | n/a | callback1 = mock.Mock() |
---|
1170 | n/a | callback2 = mock.Mock() |
---|
1171 | n/a | |
---|
1172 | n/a | # register a child |
---|
1173 | n/a | with self.watcher: |
---|
1174 | n/a | self.running = True |
---|
1175 | n/a | self.watcher.add_child_handler(51, callback1, 19) |
---|
1176 | n/a | |
---|
1177 | n/a | self.assertFalse(callback1.called) |
---|
1178 | n/a | self.assertFalse(callback2.called) |
---|
1179 | n/a | self.assertFalse(m.WIFEXITED.called) |
---|
1180 | n/a | self.assertFalse(m.WIFSIGNALED.called) |
---|
1181 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
1182 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
1183 | n/a | |
---|
1184 | n/a | # register the same child again |
---|
1185 | n/a | with self.watcher: |
---|
1186 | n/a | self.watcher.add_child_handler(51, callback2, 21) |
---|
1187 | n/a | |
---|
1188 | n/a | self.assertFalse(callback1.called) |
---|
1189 | n/a | self.assertFalse(callback2.called) |
---|
1190 | n/a | self.assertFalse(m.WIFEXITED.called) |
---|
1191 | n/a | self.assertFalse(m.WIFSIGNALED.called) |
---|
1192 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
1193 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
1194 | n/a | |
---|
1195 | n/a | # child terminates (signal 8) |
---|
1196 | n/a | self.running = False |
---|
1197 | n/a | self.add_zombie(51, -8) |
---|
1198 | n/a | self.watcher._sig_chld() |
---|
1199 | n/a | |
---|
1200 | n/a | callback2.assert_called_once_with(51, -8, 21) |
---|
1201 | n/a | self.assertFalse(callback1.called) |
---|
1202 | n/a | self.assertTrue(m.WIFSIGNALED.called) |
---|
1203 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
1204 | n/a | self.assertTrue(m.WTERMSIG.called) |
---|
1205 | n/a | |
---|
1206 | n/a | m.WIFSIGNALED.reset_mock() |
---|
1207 | n/a | m.WIFEXITED.reset_mock() |
---|
1208 | n/a | m.WTERMSIG.reset_mock() |
---|
1209 | n/a | callback2.reset_mock() |
---|
1210 | n/a | |
---|
1211 | n/a | # ensure that the child is effectively reaped |
---|
1212 | n/a | self.add_zombie(51, 13) |
---|
1213 | n/a | with self.ignore_warnings: |
---|
1214 | n/a | self.watcher._sig_chld() |
---|
1215 | n/a | |
---|
1216 | n/a | self.assertFalse(callback1.called) |
---|
1217 | n/a | self.assertFalse(callback2.called) |
---|
1218 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
1219 | n/a | |
---|
1220 | n/a | @waitpid_mocks |
---|
1221 | n/a | def test_sigchld_remove_handler(self, m): |
---|
1222 | n/a | callback = mock.Mock() |
---|
1223 | n/a | |
---|
1224 | n/a | # register a child |
---|
1225 | n/a | with self.watcher: |
---|
1226 | n/a | self.running = True |
---|
1227 | n/a | self.watcher.add_child_handler(52, callback, 1984) |
---|
1228 | n/a | |
---|
1229 | n/a | self.assertFalse(callback.called) |
---|
1230 | n/a | self.assertFalse(m.WIFEXITED.called) |
---|
1231 | n/a | self.assertFalse(m.WIFSIGNALED.called) |
---|
1232 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
1233 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
1234 | n/a | |
---|
1235 | n/a | # unregister the child |
---|
1236 | n/a | self.watcher.remove_child_handler(52) |
---|
1237 | n/a | |
---|
1238 | n/a | self.assertFalse(callback.called) |
---|
1239 | n/a | self.assertFalse(m.WIFEXITED.called) |
---|
1240 | n/a | self.assertFalse(m.WIFSIGNALED.called) |
---|
1241 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
1242 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
1243 | n/a | |
---|
1244 | n/a | # child terminates (code 99) |
---|
1245 | n/a | self.running = False |
---|
1246 | n/a | self.add_zombie(52, 99) |
---|
1247 | n/a | with self.ignore_warnings: |
---|
1248 | n/a | self.watcher._sig_chld() |
---|
1249 | n/a | |
---|
1250 | n/a | self.assertFalse(callback.called) |
---|
1251 | n/a | |
---|
1252 | n/a | @waitpid_mocks |
---|
1253 | n/a | def test_sigchld_unknown_status(self, m): |
---|
1254 | n/a | callback = mock.Mock() |
---|
1255 | n/a | |
---|
1256 | n/a | # register a child |
---|
1257 | n/a | with self.watcher: |
---|
1258 | n/a | self.running = True |
---|
1259 | n/a | self.watcher.add_child_handler(53, callback, -19) |
---|
1260 | n/a | |
---|
1261 | n/a | self.assertFalse(callback.called) |
---|
1262 | n/a | self.assertFalse(m.WIFEXITED.called) |
---|
1263 | n/a | self.assertFalse(m.WIFSIGNALED.called) |
---|
1264 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
1265 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
1266 | n/a | |
---|
1267 | n/a | # terminate with unknown status |
---|
1268 | n/a | self.zombies[53] = 1178 |
---|
1269 | n/a | self.running = False |
---|
1270 | n/a | self.watcher._sig_chld() |
---|
1271 | n/a | |
---|
1272 | n/a | callback.assert_called_once_with(53, 1178, -19) |
---|
1273 | n/a | self.assertTrue(m.WIFEXITED.called) |
---|
1274 | n/a | self.assertTrue(m.WIFSIGNALED.called) |
---|
1275 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
1276 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
1277 | n/a | |
---|
1278 | n/a | callback.reset_mock() |
---|
1279 | n/a | m.WIFEXITED.reset_mock() |
---|
1280 | n/a | m.WIFSIGNALED.reset_mock() |
---|
1281 | n/a | |
---|
1282 | n/a | # ensure that the child is effectively reaped |
---|
1283 | n/a | self.add_zombie(53, 101) |
---|
1284 | n/a | with self.ignore_warnings: |
---|
1285 | n/a | self.watcher._sig_chld() |
---|
1286 | n/a | |
---|
1287 | n/a | self.assertFalse(callback.called) |
---|
1288 | n/a | |
---|
1289 | n/a | @waitpid_mocks |
---|
1290 | n/a | def test_remove_child_handler(self, m): |
---|
1291 | n/a | callback1 = mock.Mock() |
---|
1292 | n/a | callback2 = mock.Mock() |
---|
1293 | n/a | callback3 = mock.Mock() |
---|
1294 | n/a | |
---|
1295 | n/a | # register children |
---|
1296 | n/a | with self.watcher: |
---|
1297 | n/a | self.running = True |
---|
1298 | n/a | self.watcher.add_child_handler(54, callback1, 1) |
---|
1299 | n/a | self.watcher.add_child_handler(55, callback2, 2) |
---|
1300 | n/a | self.watcher.add_child_handler(56, callback3, 3) |
---|
1301 | n/a | |
---|
1302 | n/a | # remove child handler 1 |
---|
1303 | n/a | self.assertTrue(self.watcher.remove_child_handler(54)) |
---|
1304 | n/a | |
---|
1305 | n/a | # remove child handler 2 multiple times |
---|
1306 | n/a | self.assertTrue(self.watcher.remove_child_handler(55)) |
---|
1307 | n/a | self.assertFalse(self.watcher.remove_child_handler(55)) |
---|
1308 | n/a | self.assertFalse(self.watcher.remove_child_handler(55)) |
---|
1309 | n/a | |
---|
1310 | n/a | # all children terminate |
---|
1311 | n/a | self.add_zombie(54, 0) |
---|
1312 | n/a | self.add_zombie(55, 1) |
---|
1313 | n/a | self.add_zombie(56, 2) |
---|
1314 | n/a | self.running = False |
---|
1315 | n/a | with self.ignore_warnings: |
---|
1316 | n/a | self.watcher._sig_chld() |
---|
1317 | n/a | |
---|
1318 | n/a | self.assertFalse(callback1.called) |
---|
1319 | n/a | self.assertFalse(callback2.called) |
---|
1320 | n/a | callback3.assert_called_once_with(56, 2, 3) |
---|
1321 | n/a | |
---|
1322 | n/a | @waitpid_mocks |
---|
1323 | n/a | def test_sigchld_unhandled_exception(self, m): |
---|
1324 | n/a | callback = mock.Mock() |
---|
1325 | n/a | |
---|
1326 | n/a | # register a child |
---|
1327 | n/a | with self.watcher: |
---|
1328 | n/a | self.running = True |
---|
1329 | n/a | self.watcher.add_child_handler(57, callback) |
---|
1330 | n/a | |
---|
1331 | n/a | # raise an exception |
---|
1332 | n/a | m.waitpid.side_effect = ValueError |
---|
1333 | n/a | |
---|
1334 | n/a | with mock.patch.object(log.logger, |
---|
1335 | n/a | 'error') as m_error: |
---|
1336 | n/a | |
---|
1337 | n/a | self.assertEqual(self.watcher._sig_chld(), None) |
---|
1338 | n/a | self.assertTrue(m_error.called) |
---|
1339 | n/a | |
---|
1340 | n/a | @waitpid_mocks |
---|
1341 | n/a | def test_sigchld_child_reaped_elsewhere(self, m): |
---|
1342 | n/a | # register a child |
---|
1343 | n/a | callback = mock.Mock() |
---|
1344 | n/a | |
---|
1345 | n/a | with self.watcher: |
---|
1346 | n/a | self.running = True |
---|
1347 | n/a | self.watcher.add_child_handler(58, callback) |
---|
1348 | n/a | |
---|
1349 | n/a | self.assertFalse(callback.called) |
---|
1350 | n/a | self.assertFalse(m.WIFEXITED.called) |
---|
1351 | n/a | self.assertFalse(m.WIFSIGNALED.called) |
---|
1352 | n/a | self.assertFalse(m.WEXITSTATUS.called) |
---|
1353 | n/a | self.assertFalse(m.WTERMSIG.called) |
---|
1354 | n/a | |
---|
1355 | n/a | # child terminates |
---|
1356 | n/a | self.running = False |
---|
1357 | n/a | self.add_zombie(58, 4) |
---|
1358 | n/a | |
---|
1359 | n/a | # waitpid is called elsewhere |
---|
1360 | n/a | os.waitpid(58, os.WNOHANG) |
---|
1361 | n/a | |
---|
1362 | n/a | m.waitpid.reset_mock() |
---|
1363 | n/a | |
---|
1364 | n/a | # sigchld |
---|
1365 | n/a | with self.ignore_warnings: |
---|
1366 | n/a | self.watcher._sig_chld() |
---|
1367 | n/a | |
---|
1368 | n/a | if isinstance(self.watcher, asyncio.FastChildWatcher): |
---|
1369 | n/a | # here the FastChildWatche enters a deadlock |
---|
1370 | n/a | # (there is no way to prevent it) |
---|
1371 | n/a | self.assertFalse(callback.called) |
---|
1372 | n/a | else: |
---|
1373 | n/a | callback.assert_called_once_with(58, 255) |
---|
1374 | n/a | |
---|
1375 | n/a | @waitpid_mocks |
---|
1376 | n/a | def test_sigchld_unknown_pid_during_registration(self, m): |
---|
1377 | n/a | # register two children |
---|
1378 | n/a | callback1 = mock.Mock() |
---|
1379 | n/a | callback2 = mock.Mock() |
---|
1380 | n/a | |
---|
1381 | n/a | with self.ignore_warnings, self.watcher: |
---|
1382 | n/a | self.running = True |
---|
1383 | n/a | # child 1 terminates |
---|
1384 | n/a | self.add_zombie(591, 7) |
---|
1385 | n/a | # an unknown child terminates |
---|
1386 | n/a | self.add_zombie(593, 17) |
---|
1387 | n/a | |
---|
1388 | n/a | self.watcher._sig_chld() |
---|
1389 | n/a | |
---|
1390 | n/a | self.watcher.add_child_handler(591, callback1) |
---|
1391 | n/a | self.watcher.add_child_handler(592, callback2) |
---|
1392 | n/a | |
---|
1393 | n/a | callback1.assert_called_once_with(591, 7) |
---|
1394 | n/a | self.assertFalse(callback2.called) |
---|
1395 | n/a | |
---|
1396 | n/a | @waitpid_mocks |
---|
1397 | n/a | def test_set_loop(self, m): |
---|
1398 | n/a | # register a child |
---|
1399 | n/a | callback = mock.Mock() |
---|
1400 | n/a | |
---|
1401 | n/a | with self.watcher: |
---|
1402 | n/a | self.running = True |
---|
1403 | n/a | self.watcher.add_child_handler(60, callback) |
---|
1404 | n/a | |
---|
1405 | n/a | # attach a new loop |
---|
1406 | n/a | old_loop = self.loop |
---|
1407 | n/a | self.loop = self.new_test_loop() |
---|
1408 | n/a | patch = mock.patch.object |
---|
1409 | n/a | |
---|
1410 | n/a | with patch(old_loop, "remove_signal_handler") as m_old_remove, \ |
---|
1411 | n/a | patch(self.loop, "add_signal_handler") as m_new_add: |
---|
1412 | n/a | |
---|
1413 | n/a | self.watcher.attach_loop(self.loop) |
---|
1414 | n/a | |
---|
1415 | n/a | m_old_remove.assert_called_once_with( |
---|
1416 | n/a | signal.SIGCHLD) |
---|
1417 | n/a | m_new_add.assert_called_once_with( |
---|
1418 | n/a | signal.SIGCHLD, self.watcher._sig_chld) |
---|
1419 | n/a | |
---|
1420 | n/a | # child terminates |
---|
1421 | n/a | self.running = False |
---|
1422 | n/a | self.add_zombie(60, 9) |
---|
1423 | n/a | self.watcher._sig_chld() |
---|
1424 | n/a | |
---|
1425 | n/a | callback.assert_called_once_with(60, 9) |
---|
1426 | n/a | |
---|
1427 | n/a | @waitpid_mocks |
---|
1428 | n/a | def test_set_loop_race_condition(self, m): |
---|
1429 | n/a | # register 3 children |
---|
1430 | n/a | callback1 = mock.Mock() |
---|
1431 | n/a | callback2 = mock.Mock() |
---|
1432 | n/a | callback3 = mock.Mock() |
---|
1433 | n/a | |
---|
1434 | n/a | with self.watcher: |
---|
1435 | n/a | self.running = True |
---|
1436 | n/a | self.watcher.add_child_handler(61, callback1) |
---|
1437 | n/a | self.watcher.add_child_handler(62, callback2) |
---|
1438 | n/a | self.watcher.add_child_handler(622, callback3) |
---|
1439 | n/a | |
---|
1440 | n/a | # detach the loop |
---|
1441 | n/a | old_loop = self.loop |
---|
1442 | n/a | self.loop = None |
---|
1443 | n/a | |
---|
1444 | n/a | with mock.patch.object( |
---|
1445 | n/a | old_loop, "remove_signal_handler") as m_remove_signal_handler: |
---|
1446 | n/a | |
---|
1447 | n/a | with self.assertWarnsRegex( |
---|
1448 | n/a | RuntimeWarning, 'A loop is being detached'): |
---|
1449 | n/a | self.watcher.attach_loop(None) |
---|
1450 | n/a | |
---|
1451 | n/a | m_remove_signal_handler.assert_called_once_with( |
---|
1452 | n/a | signal.SIGCHLD) |
---|
1453 | n/a | |
---|
1454 | n/a | # child 1 & 2 terminate |
---|
1455 | n/a | self.add_zombie(61, 11) |
---|
1456 | n/a | self.add_zombie(62, -5) |
---|
1457 | n/a | |
---|
1458 | n/a | # SIGCHLD was not caught |
---|
1459 | n/a | self.assertFalse(callback1.called) |
---|
1460 | n/a | self.assertFalse(callback2.called) |
---|
1461 | n/a | self.assertFalse(callback3.called) |
---|
1462 | n/a | |
---|
1463 | n/a | # attach a new loop |
---|
1464 | n/a | self.loop = self.new_test_loop() |
---|
1465 | n/a | |
---|
1466 | n/a | with mock.patch.object( |
---|
1467 | n/a | self.loop, "add_signal_handler") as m_add_signal_handler: |
---|
1468 | n/a | |
---|
1469 | n/a | self.watcher.attach_loop(self.loop) |
---|
1470 | n/a | |
---|
1471 | n/a | m_add_signal_handler.assert_called_once_with( |
---|
1472 | n/a | signal.SIGCHLD, self.watcher._sig_chld) |
---|
1473 | n/a | callback1.assert_called_once_with(61, 11) # race condition! |
---|
1474 | n/a | callback2.assert_called_once_with(62, -5) # race condition! |
---|
1475 | n/a | self.assertFalse(callback3.called) |
---|
1476 | n/a | |
---|
1477 | n/a | callback1.reset_mock() |
---|
1478 | n/a | callback2.reset_mock() |
---|
1479 | n/a | |
---|
1480 | n/a | # child 3 terminates |
---|
1481 | n/a | self.running = False |
---|
1482 | n/a | self.add_zombie(622, 19) |
---|
1483 | n/a | self.watcher._sig_chld() |
---|
1484 | n/a | |
---|
1485 | n/a | self.assertFalse(callback1.called) |
---|
1486 | n/a | self.assertFalse(callback2.called) |
---|
1487 | n/a | callback3.assert_called_once_with(622, 19) |
---|
1488 | n/a | |
---|
1489 | n/a | @waitpid_mocks |
---|
1490 | n/a | def test_close(self, m): |
---|
1491 | n/a | # register two children |
---|
1492 | n/a | callback1 = mock.Mock() |
---|
1493 | n/a | |
---|
1494 | n/a | with self.watcher: |
---|
1495 | n/a | self.running = True |
---|
1496 | n/a | # child 1 terminates |
---|
1497 | n/a | self.add_zombie(63, 9) |
---|
1498 | n/a | # other child terminates |
---|
1499 | n/a | self.add_zombie(65, 18) |
---|
1500 | n/a | self.watcher._sig_chld() |
---|
1501 | n/a | |
---|
1502 | n/a | self.watcher.add_child_handler(63, callback1) |
---|
1503 | n/a | self.watcher.add_child_handler(64, callback1) |
---|
1504 | n/a | |
---|
1505 | n/a | self.assertEqual(len(self.watcher._callbacks), 1) |
---|
1506 | n/a | if isinstance(self.watcher, asyncio.FastChildWatcher): |
---|
1507 | n/a | self.assertEqual(len(self.watcher._zombies), 1) |
---|
1508 | n/a | |
---|
1509 | n/a | with mock.patch.object( |
---|
1510 | n/a | self.loop, |
---|
1511 | n/a | "remove_signal_handler") as m_remove_signal_handler: |
---|
1512 | n/a | |
---|
1513 | n/a | self.watcher.close() |
---|
1514 | n/a | |
---|
1515 | n/a | m_remove_signal_handler.assert_called_once_with( |
---|
1516 | n/a | signal.SIGCHLD) |
---|
1517 | n/a | self.assertFalse(self.watcher._callbacks) |
---|
1518 | n/a | if isinstance(self.watcher, asyncio.FastChildWatcher): |
---|
1519 | n/a | self.assertFalse(self.watcher._zombies) |
---|
1520 | n/a | |
---|
1521 | n/a | @waitpid_mocks |
---|
1522 | n/a | def test_add_child_handler_with_no_loop_attached(self, m): |
---|
1523 | n/a | callback = mock.Mock() |
---|
1524 | n/a | with self.create_watcher() as watcher: |
---|
1525 | n/a | with self.assertRaisesRegex( |
---|
1526 | n/a | RuntimeError, |
---|
1527 | n/a | 'the child watcher does not have a loop attached'): |
---|
1528 | n/a | watcher.add_child_handler(100, callback) |
---|
1529 | n/a | |
---|
1530 | n/a | |
---|
1531 | n/a | class SafeChildWatcherTests (ChildWatcherTestsMixin, test_utils.TestCase): |
---|
1532 | n/a | def create_watcher(self): |
---|
1533 | n/a | return asyncio.SafeChildWatcher() |
---|
1534 | n/a | |
---|
1535 | n/a | |
---|
1536 | n/a | class FastChildWatcherTests (ChildWatcherTestsMixin, test_utils.TestCase): |
---|
1537 | n/a | def create_watcher(self): |
---|
1538 | n/a | return asyncio.FastChildWatcher() |
---|
1539 | n/a | |
---|
1540 | n/a | |
---|
1541 | n/a | class PolicyTests(unittest.TestCase): |
---|
1542 | n/a | |
---|
1543 | n/a | def create_policy(self): |
---|
1544 | n/a | return asyncio.DefaultEventLoopPolicy() |
---|
1545 | n/a | |
---|
1546 | n/a | def test_get_child_watcher(self): |
---|
1547 | n/a | policy = self.create_policy() |
---|
1548 | n/a | self.assertIsNone(policy._watcher) |
---|
1549 | n/a | |
---|
1550 | n/a | watcher = policy.get_child_watcher() |
---|
1551 | n/a | self.assertIsInstance(watcher, asyncio.SafeChildWatcher) |
---|
1552 | n/a | |
---|
1553 | n/a | self.assertIs(policy._watcher, watcher) |
---|
1554 | n/a | |
---|
1555 | n/a | self.assertIs(watcher, policy.get_child_watcher()) |
---|
1556 | n/a | self.assertIsNone(watcher._loop) |
---|
1557 | n/a | |
---|
1558 | n/a | def test_get_child_watcher_after_set(self): |
---|
1559 | n/a | policy = self.create_policy() |
---|
1560 | n/a | watcher = asyncio.FastChildWatcher() |
---|
1561 | n/a | |
---|
1562 | n/a | policy.set_child_watcher(watcher) |
---|
1563 | n/a | self.assertIs(policy._watcher, watcher) |
---|
1564 | n/a | self.assertIs(watcher, policy.get_child_watcher()) |
---|
1565 | n/a | |
---|
1566 | n/a | def test_get_child_watcher_with_mainloop_existing(self): |
---|
1567 | n/a | policy = self.create_policy() |
---|
1568 | n/a | loop = policy.get_event_loop() |
---|
1569 | n/a | |
---|
1570 | n/a | self.assertIsNone(policy._watcher) |
---|
1571 | n/a | watcher = policy.get_child_watcher() |
---|
1572 | n/a | |
---|
1573 | n/a | self.assertIsInstance(watcher, asyncio.SafeChildWatcher) |
---|
1574 | n/a | self.assertIs(watcher._loop, loop) |
---|
1575 | n/a | |
---|
1576 | n/a | loop.close() |
---|
1577 | n/a | |
---|
1578 | n/a | def test_get_child_watcher_thread(self): |
---|
1579 | n/a | |
---|
1580 | n/a | def f(): |
---|
1581 | n/a | policy.set_event_loop(policy.new_event_loop()) |
---|
1582 | n/a | |
---|
1583 | n/a | self.assertIsInstance(policy.get_event_loop(), |
---|
1584 | n/a | asyncio.AbstractEventLoop) |
---|
1585 | n/a | watcher = policy.get_child_watcher() |
---|
1586 | n/a | |
---|
1587 | n/a | self.assertIsInstance(watcher, asyncio.SafeChildWatcher) |
---|
1588 | n/a | self.assertIsNone(watcher._loop) |
---|
1589 | n/a | |
---|
1590 | n/a | policy.get_event_loop().close() |
---|
1591 | n/a | |
---|
1592 | n/a | policy = self.create_policy() |
---|
1593 | n/a | |
---|
1594 | n/a | th = threading.Thread(target=f) |
---|
1595 | n/a | th.start() |
---|
1596 | n/a | th.join() |
---|
1597 | n/a | |
---|
1598 | n/a | def test_child_watcher_replace_mainloop_existing(self): |
---|
1599 | n/a | policy = self.create_policy() |
---|
1600 | n/a | loop = policy.get_event_loop() |
---|
1601 | n/a | |
---|
1602 | n/a | watcher = policy.get_child_watcher() |
---|
1603 | n/a | |
---|
1604 | n/a | self.assertIs(watcher._loop, loop) |
---|
1605 | n/a | |
---|
1606 | n/a | new_loop = policy.new_event_loop() |
---|
1607 | n/a | policy.set_event_loop(new_loop) |
---|
1608 | n/a | |
---|
1609 | n/a | self.assertIs(watcher._loop, new_loop) |
---|
1610 | n/a | |
---|
1611 | n/a | policy.set_event_loop(None) |
---|
1612 | n/a | |
---|
1613 | n/a | self.assertIs(watcher._loop, None) |
---|
1614 | n/a | |
---|
1615 | n/a | loop.close() |
---|
1616 | n/a | new_loop.close() |
---|
1617 | n/a | |
---|
1618 | n/a | |
---|
1619 | n/a | if __name__ == '__main__': |
---|
1620 | n/a | unittest.main() |
---|