| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2018-12-18 13:56:17 -08:00
										 |  |  | import signal | 
					
						
							| 
									
										
										
										
											2017-11-28 11:15:26 +01:00
										 |  |  | import socket | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2018-12-18 13:56:17 -08:00
										 |  |  | import time | 
					
						
							| 
									
										
										
										
											2019-01-05 12:44:59 -08:00
										 |  |  | import threading | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2015-01-26 15:04:03 +01:00
										 |  |  | from unittest import mock | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | if sys.platform != 'win32': | 
					
						
							|  |  |  |     raise unittest.SkipTest('Windows only') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 15:19:56 +01:00
										 |  |  | import _overlapped | 
					
						
							| 
									
										
										
										
											2013-10-30 14:44:05 -07:00
										 |  |  | import _winapi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | import asyncio | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  | from asyncio import windows_events | 
					
						
							| 
									
										
										
										
											2017-12-11 10:04:40 -05:00
										 |  |  | from test.test_asyncio import utils as test_utils | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-01 20:34:09 -07:00
										 |  |  | def tearDownModule(): | 
					
						
							| 
									
										
										
										
											2024-12-18 11:35:29 +05:30
										 |  |  |     asyncio._set_event_loop_policy(None) | 
					
						
							| 
									
										
										
										
											2018-06-01 20:34:09 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  | class UpperProto(asyncio.Protocol): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def __init__(self): | 
					
						
							|  |  |  |         self.buf = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def connection_made(self, trans): | 
					
						
							|  |  |  |         self.trans = trans | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def data_received(self, data): | 
					
						
							|  |  |  |         self.buf.append(data) | 
					
						
							|  |  |  |         if b'\n' in data: | 
					
						
							|  |  |  |             self.trans.write(b''.join(self.buf).upper()) | 
					
						
							|  |  |  |             self.trans.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-20 07:33:28 -07:00
										 |  |  | class WindowsEventsTestCase(test_utils.TestCase): | 
					
						
							|  |  |  |     def _unraisablehook(self, unraisable): | 
					
						
							|  |  |  |         # Storing unraisable.object can resurrect an object which is being | 
					
						
							|  |  |  |         # finalized. Storing unraisable.exc_value creates a reference cycle. | 
					
						
							|  |  |  |         self._unraisable = unraisable | 
					
						
							|  |  |  |         print(unraisable) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							|  |  |  |         self._prev_unraisablehook = sys.unraisablehook | 
					
						
							|  |  |  |         self._unraisable = None | 
					
						
							|  |  |  |         sys.unraisablehook = self._unraisablehook | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def tearDown(self): | 
					
						
							|  |  |  |         sys.unraisablehook = self._prev_unraisablehook | 
					
						
							|  |  |  |         self.assertIsNone(self._unraisable) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ProactorLoopCtrlC(WindowsEventsTestCase): | 
					
						
							| 
									
										
										
										
											2019-01-05 12:44:59 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-18 13:56:17 -08:00
										 |  |  |     def test_ctrl_c(self): | 
					
						
							| 
									
										
										
										
											2019-01-05 12:44:59 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def SIGINT_after_delay(): | 
					
						
							| 
									
										
										
										
											2019-06-14 13:02:51 +02:00
										 |  |  |             time.sleep(0.1) | 
					
						
							| 
									
										
										
										
											2019-01-08 01:58:25 -08:00
										 |  |  |             signal.raise_signal(signal.SIGINT) | 
					
						
							| 
									
										
										
										
											2019-01-05 12:44:59 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-14 13:02:51 +02:00
										 |  |  |         thread = threading.Thread(target=SIGINT_after_delay) | 
					
						
							| 
									
										
										
										
											2022-01-22 16:50:10 +05:30
										 |  |  |         loop = asyncio.new_event_loop() | 
					
						
							| 
									
										
										
										
											2019-01-05 12:44:59 -08:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2019-06-14 13:02:51 +02:00
										 |  |  |             # only start the loop once the event loop is running | 
					
						
							|  |  |  |             loop.call_soon(thread.start) | 
					
						
							|  |  |  |             loop.run_forever() | 
					
						
							| 
									
										
										
										
											2019-01-05 12:44:59 -08:00
										 |  |  |             self.fail("should not fall through 'run_forever'") | 
					
						
							|  |  |  |         except KeyboardInterrupt: | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  |         finally: | 
					
						
							| 
									
										
										
										
											2019-06-14 13:02:51 +02:00
										 |  |  |             self.close_loop(loop) | 
					
						
							|  |  |  |         thread.join() | 
					
						
							| 
									
										
										
										
											2018-12-18 13:56:17 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-20 07:33:28 -07:00
										 |  |  | class ProactorMultithreading(WindowsEventsTestCase): | 
					
						
							| 
									
										
										
										
											2019-08-26 12:51:08 +03:00
										 |  |  |     def test_run_from_nonmain_thread(self): | 
					
						
							|  |  |  |         finished = False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         async def coro(): | 
					
						
							|  |  |  |             await asyncio.sleep(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def func(): | 
					
						
							|  |  |  |             nonlocal finished | 
					
						
							|  |  |  |             loop = asyncio.new_event_loop() | 
					
						
							|  |  |  |             loop.run_until_complete(coro()) | 
					
						
							| 
									
										
										
										
											2019-10-23 17:25:29 +02:00
										 |  |  |             # close() must not call signal.set_wakeup_fd() | 
					
						
							|  |  |  |             loop.close() | 
					
						
							| 
									
										
										
										
											2019-08-26 12:51:08 +03:00
										 |  |  |             finished = True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         thread = threading.Thread(target=func) | 
					
						
							|  |  |  |         thread.start() | 
					
						
							|  |  |  |         thread.join() | 
					
						
							|  |  |  |         self.assertTrue(finished) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-20 07:33:28 -07:00
										 |  |  | class ProactorTests(WindowsEventsTestCase): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							| 
									
										
										
										
											2016-11-04 14:29:28 -04:00
										 |  |  |         super().setUp() | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         self.loop = asyncio.ProactorEventLoop() | 
					
						
							| 
									
										
										
										
											2014-06-18 01:36:32 +02:00
										 |  |  |         self.set_event_loop(self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_close(self): | 
					
						
							| 
									
										
										
										
											2017-11-28 11:15:26 +01:00
										 |  |  |         a, b = socket.socketpair() | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         trans = self.loop._make_socket_transport(a, asyncio.Protocol()) | 
					
						
							| 
									
										
										
										
											2017-12-14 20:53:26 -05:00
										 |  |  |         f = asyncio.ensure_future(self.loop.sock_recv(b, 100), loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         trans.close() | 
					
						
							|  |  |  |         self.loop.run_until_complete(f) | 
					
						
							|  |  |  |         self.assertEqual(f.result(), b'') | 
					
						
							| 
									
										
										
										
											2014-01-11 00:16:50 +01:00
										 |  |  |         b.close() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_double_bind(self): | 
					
						
							|  |  |  |         ADDRESS = r'\\.\pipe\test_double_bind-%s' % os.getpid() | 
					
						
							|  |  |  |         server1 = windows_events.PipeServer(ADDRESS) | 
					
						
							|  |  |  |         with self.assertRaises(PermissionError): | 
					
						
							| 
									
										
										
										
											2014-02-26 11:07:42 +01:00
										 |  |  |             windows_events.PipeServer(ADDRESS) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         server1.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_pipe(self): | 
					
						
							|  |  |  |         res = self.loop.run_until_complete(self._test_pipe()) | 
					
						
							|  |  |  |         self.assertEqual(res, 'done') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |     async def _test_pipe(self): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         ADDRESS = r'\\.\pipe\_test_pipe-%s' % os.getpid() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with self.assertRaises(FileNotFoundError): | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |             await self.loop.create_pipe_connection( | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |                 asyncio.Protocol, ADDRESS) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         [server] = await self.loop.start_serving_pipe( | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             UpperProto, ADDRESS) | 
					
						
							|  |  |  |         self.assertIsInstance(server, windows_events.PipeServer) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         clients = [] | 
					
						
							|  |  |  |         for i in range(5): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             stream_reader = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  |             protocol = asyncio.StreamReaderProtocol(stream_reader, | 
					
						
							|  |  |  |                                                     loop=self.loop) | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |             trans, proto = await self.loop.create_pipe_connection( | 
					
						
							| 
									
										
										
										
											2013-11-01 14:20:55 -07:00
										 |  |  |                 lambda: protocol, ADDRESS) | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |             self.assertIsInstance(trans, asyncio.Transport) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self.assertEqual(protocol, proto) | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             clients.append((stream_reader, trans)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for i, (r, w) in enumerate(clients): | 
					
						
							|  |  |  |             w.write('lower-{}\n'.format(i).encode()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for i, (r, w) in enumerate(clients): | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |             response = await r.readline() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self.assertEqual(response, 'LOWER-{}\n'.format(i).encode()) | 
					
						
							|  |  |  |             w.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         server.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with self.assertRaises(FileNotFoundError): | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |             await self.loop.create_pipe_connection( | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |                 asyncio.Protocol, ADDRESS) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return 'done' | 
					
						
							| 
									
										
										
										
											2013-10-20 21:02:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-26 15:04:03 +01:00
										 |  |  |     def test_connect_pipe_cancel(self): | 
					
						
							|  |  |  |         exc = OSError() | 
					
						
							|  |  |  |         exc.winerror = _overlapped.ERROR_PIPE_BUSY | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         with mock.patch.object(_overlapped, 'ConnectPipe', | 
					
						
							|  |  |  |                                side_effect=exc) as connect: | 
					
						
							| 
									
										
										
										
											2015-01-26 15:04:03 +01:00
										 |  |  |             coro = self.loop._proactor.connect_pipe('pipe_address') | 
					
						
							|  |  |  |             task = self.loop.create_task(coro) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # check that it's possible to cancel connect_pipe() | 
					
						
							|  |  |  |             task.cancel() | 
					
						
							|  |  |  |             with self.assertRaises(asyncio.CancelledError): | 
					
						
							|  |  |  |                 self.loop.run_until_complete(task) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 14:44:05 -07:00
										 |  |  |     def test_wait_for_handle(self): | 
					
						
							|  |  |  |         event = _overlapped.CreateEvent(None, True, False, None) | 
					
						
							|  |  |  |         self.addCleanup(_winapi.CloseHandle, event) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-24 12:04:22 +02:00
										 |  |  |         # Wait for unset event with 0.5s timeout; | 
					
						
							| 
									
										
										
										
											2013-10-30 14:44:05 -07:00
										 |  |  |         # result should be False at timeout | 
					
						
							| 
									
										
										
										
											2023-09-29 13:49:30 +02:00
										 |  |  |         timeout = 0.5 | 
					
						
							|  |  |  |         fut = self.loop._proactor.wait_for_handle(event, timeout) | 
					
						
							| 
									
										
										
										
											2013-10-30 14:44:05 -07:00
										 |  |  |         start = self.loop.time() | 
					
						
							| 
									
										
										
										
											2014-12-19 17:10:44 +01:00
										 |  |  |         done = self.loop.run_until_complete(fut) | 
					
						
							| 
									
										
										
										
											2013-10-30 14:44:05 -07:00
										 |  |  |         elapsed = self.loop.time() - start | 
					
						
							| 
									
										
										
										
											2014-12-19 17:10:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(done, False) | 
					
						
							| 
									
										
										
										
											2014-07-25 00:54:53 +02:00
										 |  |  |         self.assertFalse(fut.result()) | 
					
						
							| 
									
										
										
										
											2023-09-29 13:49:30 +02:00
										 |  |  |         self.assertGreaterEqual(elapsed, timeout - test_utils.CLOCK_RES) | 
					
						
							| 
									
										
										
										
											2013-10-30 14:44:05 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         _overlapped.SetEvent(event) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 18:28:43 +02:00
										 |  |  |         # Wait for set event; | 
					
						
							| 
									
										
										
										
											2013-10-30 14:44:05 -07:00
										 |  |  |         # result should be True immediately | 
					
						
							| 
									
										
										
										
											2014-07-25 00:54:53 +02:00
										 |  |  |         fut = self.loop._proactor.wait_for_handle(event, 10) | 
					
						
							| 
									
										
										
										
											2014-12-19 17:10:44 +01:00
										 |  |  |         done = self.loop.run_until_complete(fut) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(done, True) | 
					
						
							| 
									
										
										
										
											2014-07-25 00:54:53 +02:00
										 |  |  |         self.assertTrue(fut.result()) | 
					
						
							| 
									
										
										
										
											2013-10-30 14:44:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 23:13:50 +02:00
										 |  |  |         # asyncio issue #195: cancelling a done _WaitHandleFuture | 
					
						
							|  |  |  |         # must not crash | 
					
						
							| 
									
										
										
										
											2014-07-25 00:54:53 +02:00
										 |  |  |         fut.cancel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_wait_for_handle_cancel(self): | 
					
						
							|  |  |  |         event = _overlapped.CreateEvent(None, True, False, None) | 
					
						
							|  |  |  |         self.addCleanup(_winapi.CloseHandle, event) | 
					
						
							| 
									
										
										
										
											2013-10-30 14:44:05 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Wait for unset event with a cancelled future; | 
					
						
							|  |  |  |         # CancelledError should be raised immediately | 
					
						
							| 
									
										
										
										
											2014-07-25 00:54:53 +02:00
										 |  |  |         fut = self.loop._proactor.wait_for_handle(event, 10) | 
					
						
							|  |  |  |         fut.cancel() | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         with self.assertRaises(asyncio.CancelledError): | 
					
						
							| 
									
										
										
										
											2014-07-25 00:54:53 +02:00
										 |  |  |             self.loop.run_until_complete(fut) | 
					
						
							| 
									
										
										
										
											2013-10-30 14:44:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-09 23:13:50 +02:00
										 |  |  |         # asyncio issue #195: cancelling a _WaitHandleFuture twice | 
					
						
							|  |  |  |         # must not crash | 
					
						
							| 
									
										
										
										
											2014-07-25 00:54:53 +02:00
										 |  |  |         fut = self.loop._proactor.wait_for_handle(event) | 
					
						
							|  |  |  |         fut.cancel() | 
					
						
							|  |  |  |         fut.cancel() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-31 15:57:52 -04:00
										 |  |  |     def test_read_self_pipe_restart(self): | 
					
						
							|  |  |  |         # Regression test for https://bugs.python.org/issue39010 | 
					
						
							|  |  |  |         # Previously, restarting a proactor event loop in certain states | 
					
						
							|  |  |  |         # would lead to spurious ConnectionResetErrors being logged. | 
					
						
							|  |  |  |         self.loop.call_exception_handler = mock.Mock() | 
					
						
							|  |  |  |         # Start an operation in another thread so that the self-pipe is used. | 
					
						
							|  |  |  |         # This is theoretically timing-dependent (the task in the executor | 
					
						
							|  |  |  |         # must complete before our start/stop cycles), but in practice it | 
					
						
							|  |  |  |         # seems to work every time. | 
					
						
							|  |  |  |         f = self.loop.run_in_executor(None, lambda: None) | 
					
						
							|  |  |  |         self.loop.stop() | 
					
						
							|  |  |  |         self.loop.run_forever() | 
					
						
							|  |  |  |         self.loop.stop() | 
					
						
							|  |  |  |         self.loop.run_forever() | 
					
						
							| 
									
										
										
										
											2020-09-03 00:58:50 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Shut everything down cleanly. This is an important part of the | 
					
						
							|  |  |  |         # test - in issue 39010, the error occurred during loop.close(), | 
					
						
							|  |  |  |         # so we want to close the loop during the test instead of leaving | 
					
						
							|  |  |  |         # it for tearDown. | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         # First wait for f to complete to avoid a "future's result was never | 
					
						
							|  |  |  |         # retrieved" error. | 
					
						
							| 
									
										
										
										
											2020-08-31 15:57:52 -04:00
										 |  |  |         self.loop.run_until_complete(f) | 
					
						
							| 
									
										
										
										
											2020-09-03 00:58:50 -04:00
										 |  |  |         # Now shut down the loop itself (self.close_loop also shuts down the | 
					
						
							|  |  |  |         # loop's default executor). | 
					
						
							|  |  |  |         self.close_loop(self.loop) | 
					
						
							| 
									
										
										
										
											2020-08-31 15:57:52 -04:00
										 |  |  |         self.assertFalse(self.loop.call_exception_handler.called) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 12:34:46 +08:00
										 |  |  |     def test_address_argument_type_error(self): | 
					
						
							|  |  |  |         # Regression test for https://github.com/python/cpython/issues/98793 | 
					
						
							|  |  |  |         proactor = self.loop._proactor | 
					
						
							|  |  |  |         sock = socket.socket(type=socket.SOCK_DGRAM) | 
					
						
							|  |  |  |         bad_address = None | 
					
						
							|  |  |  |         with self.assertRaises(TypeError): | 
					
						
							|  |  |  |             proactor.connect(sock, bad_address) | 
					
						
							|  |  |  |         with self.assertRaises(TypeError): | 
					
						
							|  |  |  |             proactor.sendto(sock, b'abc', addr=bad_address) | 
					
						
							|  |  |  |         sock.close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-13 13:24:57 -08:00
										 |  |  |     def test_client_pipe_stat(self): | 
					
						
							|  |  |  |         res = self.loop.run_until_complete(self._test_client_pipe_stat()) | 
					
						
							|  |  |  |         self.assertEqual(res, 'done') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async def _test_client_pipe_stat(self): | 
					
						
							|  |  |  |         # Regression test for https://github.com/python/cpython/issues/100573 | 
					
						
							|  |  |  |         ADDRESS = r'\\.\pipe\test_client_pipe_stat-%s' % os.getpid() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         async def probe(): | 
					
						
							|  |  |  |             # See https://github.com/python/cpython/pull/100959#discussion_r1068533658 | 
					
						
							|  |  |  |             h = _overlapped.ConnectPipe(ADDRESS) | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 _winapi.CloseHandle(_overlapped.ConnectPipe(ADDRESS)) | 
					
						
							|  |  |  |             except OSError as e: | 
					
						
							|  |  |  |                 if e.winerror != _overlapped.ERROR_PIPE_BUSY: | 
					
						
							|  |  |  |                     raise | 
					
						
							|  |  |  |             finally: | 
					
						
							|  |  |  |                 _winapi.CloseHandle(h) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with self.assertRaises(FileNotFoundError): | 
					
						
							|  |  |  |             await probe() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         [server] = await self.loop.start_serving_pipe(asyncio.Protocol, ADDRESS) | 
					
						
							|  |  |  |         self.assertIsInstance(server, windows_events.PipeServer) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         errors = [] | 
					
						
							|  |  |  |         self.loop.set_exception_handler(lambda _, data: errors.append(data)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for i in range(5): | 
					
						
							|  |  |  |             await self.loop.create_task(probe()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(len(errors), 0, errors) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         server.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with self.assertRaises(FileNotFoundError): | 
					
						
							|  |  |  |             await probe() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return "done" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-20 07:33:28 -07:00
										 |  |  |     def test_loop_restart(self): | 
					
						
							|  |  |  |         # We're fishing for the "RuntimeError: <_overlapped.Overlapped object at XXX> | 
					
						
							|  |  |  |         # still has pending operation at deallocation, the process may crash" error | 
					
						
							|  |  |  |         stop = threading.Event() | 
					
						
							|  |  |  |         def threadMain(): | 
					
						
							|  |  |  |             while not stop.is_set(): | 
					
						
							|  |  |  |                 self.loop.call_soon_threadsafe(lambda: None) | 
					
						
							|  |  |  |                 time.sleep(0.01) | 
					
						
							|  |  |  |         thr = threading.Thread(target=threadMain) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # In 10 60-second runs of this test prior to the fix: | 
					
						
							|  |  |  |         # time in seconds until failure: (none), 15.0, 6.4, (none), 7.6, 8.3, 1.7, 22.2, 23.5, 8.3 | 
					
						
							|  |  |  |         # 10 seconds had a 50% failure rate but longer would be more costly | 
					
						
							|  |  |  |         end_time = time.time() + 10 # Run for 10 seconds | 
					
						
							|  |  |  |         self.loop.call_soon(thr.start) | 
					
						
							|  |  |  |         while not self._unraisable: # Stop if we got an unraisable exc | 
					
						
							|  |  |  |             self.loop.stop() | 
					
						
							|  |  |  |             self.loop.run_forever() | 
					
						
							|  |  |  |             if time.time() >= end_time: | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         stop.set() | 
					
						
							|  |  |  |         thr.join() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class WinPolicyTests(WindowsEventsTestCase): | 
					
						
							| 
									
										
										
										
											2018-06-07 20:44:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_selector_win_policy(self): | 
					
						
							|  |  |  |         async def main(): | 
					
						
							|  |  |  |             self.assertIsInstance( | 
					
						
							|  |  |  |                 asyncio.get_running_loop(), | 
					
						
							|  |  |  |                 asyncio.SelectorEventLoop) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         old_policy = asyncio.get_event_loop_policy() | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2024-12-18 11:35:29 +05:30
										 |  |  |             asyncio._set_event_loop_policy( | 
					
						
							| 
									
										
										
										
											2018-06-07 20:44:57 -04:00
										 |  |  |                 asyncio.WindowsSelectorEventLoopPolicy()) | 
					
						
							|  |  |  |             asyncio.run(main()) | 
					
						
							|  |  |  |         finally: | 
					
						
							| 
									
										
										
										
											2024-12-18 11:35:29 +05:30
										 |  |  |             asyncio._set_event_loop_policy(old_policy) | 
					
						
							| 
									
										
										
										
											2018-06-07 20:44:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_proactor_win_policy(self): | 
					
						
							|  |  |  |         async def main(): | 
					
						
							|  |  |  |             self.assertIsInstance( | 
					
						
							|  |  |  |                 asyncio.get_running_loop(), | 
					
						
							|  |  |  |                 asyncio.ProactorEventLoop) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         old_policy = asyncio.get_event_loop_policy() | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2024-12-18 11:35:29 +05:30
										 |  |  |             asyncio._set_event_loop_policy( | 
					
						
							| 
									
										
										
										
											2018-06-07 20:44:57 -04:00
										 |  |  |                 asyncio.WindowsProactorEventLoopPolicy()) | 
					
						
							|  |  |  |             asyncio.run(main()) | 
					
						
							|  |  |  |         finally: | 
					
						
							| 
									
										
										
										
											2024-12-18 11:35:29 +05:30
										 |  |  |             asyncio._set_event_loop_policy(old_policy) | 
					
						
							| 
									
										
										
										
											2018-06-07 20:44:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-20 21:02:53 +02:00
										 |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     unittest.main() |