| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | """Tests for streams.py.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import gc | 
					
						
							| 
									
										
										
										
											2015-10-19 11:49:30 -07:00
										 |  |  | import queue | 
					
						
							| 
									
										
										
										
											2017-11-15 17:14:28 -05:00
										 |  |  | import pickle | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  | import socket | 
					
						
							| 
									
										
										
										
											2015-10-19 11:49:30 -07:00
										 |  |  | import threading | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2014-02-26 10:25:02 +01:00
										 |  |  | from unittest import mock | 
					
						
							| 
									
										
										
										
											2013-10-17 14:23:17 -07:00
										 |  |  | try: | 
					
						
							|  |  |  |     import ssl | 
					
						
							|  |  |  | except ImportError: | 
					
						
							|  |  |  |     ssl = None | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  | import asyncio | 
					
						
							| 
									
										
										
										
											2017-12-11 10:04:40 -05:00
										 |  |  | from test.test_asyncio import utils as test_utils | 
					
						
							| 
									
										
										
										
											2025-03-13 10:55:23 +01:00
										 |  |  | from test.support import socket_helper | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-25 00:30:30 +02:00
										 |  |  | class StreamTests(test_utils.TestCase): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     DATA = b'line1\nline2\nline3\n' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							| 
									
										
										
										
											2016-11-04 14:29:28 -04:00
										 |  |  |         super().setUp() | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         self.loop = asyncio.new_event_loop() | 
					
						
							| 
									
										
										
										
											2014-06-18 01:36:32 +02:00
										 |  |  |         self.set_event_loop(self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def tearDown(self): | 
					
						
							|  |  |  |         # just in case if we have transport close callbacks | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-04 01:47:07 +01:00
										 |  |  |         # set_event_loop() takes care of closing self.loop in a safe way | 
					
						
							| 
									
										
										
										
											2014-06-18 01:36:32 +02:00
										 |  |  |         super().tearDown() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |     def _basetest_open_connection(self, open_connection_fut): | 
					
						
							| 
									
										
										
										
											2018-09-12 11:43:04 -07:00
										 |  |  |         messages = [] | 
					
						
							|  |  |  |         self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) | 
					
						
							| 
									
										
										
										
											2020-11-26 09:36:37 +02:00
										 |  |  |         reader, writer = self.loop.run_until_complete(open_connection_fut) | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |         writer.write(b'GET / HTTP/1.0\r\n\r\n') | 
					
						
							|  |  |  |         f = reader.readline() | 
					
						
							|  |  |  |         data = self.loop.run_until_complete(f) | 
					
						
							|  |  |  |         self.assertEqual(data, b'HTTP/1.0 200 OK\r\n') | 
					
						
							|  |  |  |         f = reader.read() | 
					
						
							|  |  |  |         data = self.loop.run_until_complete(f) | 
					
						
							| 
									
										
										
										
											2025-01-20 13:32:39 +02:00
										 |  |  |         self.assertEndsWith(data, b'\r\n\r\nTest message') | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |         writer.close() | 
					
						
							| 
									
										
										
										
											2018-09-12 11:43:04 -07:00
										 |  |  |         self.assertEqual(messages, []) | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def test_open_connection(self): | 
					
						
							|  |  |  |         with test_utils.run_test_server() as httpd: | 
					
						
							| 
									
										
										
										
											2020-11-26 09:36:37 +02:00
										 |  |  |             conn_fut = asyncio.open_connection(*httpd.address) | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |             self._basetest_open_connection(conn_fut) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 10:06:29 +03:00
										 |  |  |     @socket_helper.skip_unless_bind_unix_socket | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |     def test_open_unix_connection(self): | 
					
						
							|  |  |  |         with test_utils.run_test_unix_server() as httpd: | 
					
						
							| 
									
										
										
										
											2020-11-26 09:36:37 +02:00
										 |  |  |             conn_fut = asyncio.open_unix_connection(httpd.address) | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |             self._basetest_open_connection(conn_fut) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _basetest_open_connection_no_loop_ssl(self, open_connection_fut): | 
					
						
							| 
									
										
										
										
											2018-09-12 11:43:04 -07:00
										 |  |  |         messages = [] | 
					
						
							|  |  |  |         self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2020-11-26 09:36:37 +02:00
										 |  |  |             reader, writer = self.loop.run_until_complete(open_connection_fut) | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |         finally: | 
					
						
							| 
									
										
										
										
											2025-04-12 12:03:52 +05:30
										 |  |  |             asyncio.set_event_loop(None) | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |         writer.write(b'GET / HTTP/1.0\r\n\r\n') | 
					
						
							|  |  |  |         f = reader.read() | 
					
						
							|  |  |  |         data = self.loop.run_until_complete(f) | 
					
						
							| 
									
										
										
										
											2025-01-20 13:32:39 +02:00
										 |  |  |         self.assertEndsWith(data, b'\r\n\r\nTest message') | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         writer.close() | 
					
						
							| 
									
										
										
										
											2018-09-12 11:43:04 -07:00
										 |  |  |         self.assertEqual(messages, []) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @unittest.skipIf(ssl is None, 'No ssl module') | 
					
						
							|  |  |  |     def test_open_connection_no_loop_ssl(self): | 
					
						
							|  |  |  |         with test_utils.run_test_server(use_ssl=True) as httpd: | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |             conn_fut = asyncio.open_connection( | 
					
						
							|  |  |  |                 *httpd.address, | 
					
						
							| 
									
										
										
										
											2020-11-26 09:36:37 +02:00
										 |  |  |                 ssl=test_utils.dummy_ssl_context()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |             self._basetest_open_connection_no_loop_ssl(conn_fut) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 10:06:29 +03:00
										 |  |  |     @socket_helper.skip_unless_bind_unix_socket | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |     @unittest.skipIf(ssl is None, 'No ssl module') | 
					
						
							|  |  |  |     def test_open_unix_connection_no_loop_ssl(self): | 
					
						
							|  |  |  |         with test_utils.run_test_unix_server(use_ssl=True) as httpd: | 
					
						
							|  |  |  |             conn_fut = asyncio.open_unix_connection( | 
					
						
							|  |  |  |                 httpd.address, | 
					
						
							|  |  |  |                 ssl=test_utils.dummy_ssl_context(), | 
					
						
							|  |  |  |                 server_hostname='', | 
					
						
							| 
									
										
										
										
											2020-11-26 09:36:37 +02:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             self._basetest_open_connection_no_loop_ssl(conn_fut) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _basetest_open_connection_error(self, open_connection_fut): | 
					
						
							| 
									
										
										
										
											2018-09-12 11:43:04 -07:00
										 |  |  |         messages = [] | 
					
						
							|  |  |  |         self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) | 
					
						
							| 
									
										
										
										
											2020-11-26 09:36:37 +02:00
										 |  |  |         reader, writer = self.loop.run_until_complete(open_connection_fut) | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |         writer._protocol.connection_lost(ZeroDivisionError()) | 
					
						
							|  |  |  |         f = reader.read() | 
					
						
							|  |  |  |         with self.assertRaises(ZeroDivisionError): | 
					
						
							|  |  |  |             self.loop.run_until_complete(f) | 
					
						
							|  |  |  |         writer.close() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							| 
									
										
										
										
											2018-09-12 11:43:04 -07:00
										 |  |  |         self.assertEqual(messages, []) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_open_connection_error(self): | 
					
						
							|  |  |  |         with test_utils.run_test_server() as httpd: | 
					
						
							| 
									
										
										
										
											2020-11-26 09:36:37 +02:00
										 |  |  |             conn_fut = asyncio.open_connection(*httpd.address) | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |             self._basetest_open_connection_error(conn_fut) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 10:06:29 +03:00
										 |  |  |     @socket_helper.skip_unless_bind_unix_socket | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |     def test_open_unix_connection_error(self): | 
					
						
							|  |  |  |         with test_utils.run_test_unix_server() as httpd: | 
					
						
							| 
									
										
										
										
											2020-11-26 09:36:37 +02:00
										 |  |  |             conn_fut = asyncio.open_unix_connection(httpd.address) | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |             self._basetest_open_connection_error(conn_fut) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_feed_empty_data(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.feed_data(b'') | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |     def test_feed_nonempty_data(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.feed_data(self.DATA) | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         self.assertEqual(self.DATA, stream._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_read_zero(self): | 
					
						
							|  |  |  |         # Read zero bytes. | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_data(self.DATA) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         data = self.loop.run_until_complete(stream.read(0)) | 
					
						
							|  |  |  |         self.assertEqual(b'', data) | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         self.assertEqual(self.DATA, stream._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_read(self): | 
					
						
							|  |  |  |         # Read bytes. | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         read_task = self.loop.create_task(stream.read(30)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def cb(): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             stream.feed_data(self.DATA) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.loop.call_soon(cb) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         data = self.loop.run_until_complete(read_task) | 
					
						
							|  |  |  |         self.assertEqual(self.DATA, data) | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_read_line_breaks(self): | 
					
						
							|  |  |  |         # Read bytes without line breaks. | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_data(b'line1') | 
					
						
							|  |  |  |         stream.feed_data(b'line2') | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         data = self.loop.run_until_complete(stream.read(5)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(b'line1', data) | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         self.assertEqual(b'line2', stream._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_read_eof(self): | 
					
						
							|  |  |  |         # Read bytes, stop at eof. | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         read_task = self.loop.create_task(stream.read(1024)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def cb(): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             stream.feed_eof() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.loop.call_soon(cb) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         data = self.loop.run_until_complete(read_task) | 
					
						
							|  |  |  |         self.assertEqual(b'', data) | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_read_until_eof(self): | 
					
						
							|  |  |  |         # Read all bytes until eof. | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         read_task = self.loop.create_task(stream.read(-1)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def cb(): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             stream.feed_data(b'chunk1\n') | 
					
						
							|  |  |  |             stream.feed_data(b'chunk2') | 
					
						
							|  |  |  |             stream.feed_eof() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.loop.call_soon(cb) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         data = self.loop.run_until_complete(read_task) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(b'chunk1\nchunk2', data) | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_read_exception(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_data(b'line\n') | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         data = self.loop.run_until_complete(stream.read(2)) | 
					
						
							|  |  |  |         self.assertEqual(b'li', data) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.set_exception(ValueError()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertRaises( | 
					
						
							|  |  |  |             ValueError, self.loop.run_until_complete, stream.read(2)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |     def test_invalid_limit(self): | 
					
						
							|  |  |  |         with self.assertRaisesRegex(ValueError, 'imit'): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             asyncio.StreamReader(limit=0, loop=self.loop) | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         with self.assertRaisesRegex(ValueError, 'imit'): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             asyncio.StreamReader(limit=-1, loop=self.loop) | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_read_limit(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(limit=3, loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_data(b'chunk') | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |         data = self.loop.run_until_complete(stream.read(5)) | 
					
						
							|  |  |  |         self.assertEqual(b'chunk', data) | 
					
						
							|  |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def test_readline(self): | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         # Read one line. 'readline' will need to wait for the data | 
					
						
							|  |  |  |         # to come from 'cb' | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_data(b'chunk1 ') | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         read_task = self.loop.create_task(stream.readline()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def cb(): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             stream.feed_data(b'chunk2 ') | 
					
						
							|  |  |  |             stream.feed_data(b'chunk3 ') | 
					
						
							|  |  |  |             stream.feed_data(b'\n chunk4') | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.loop.call_soon(cb) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         line = self.loop.run_until_complete(read_task) | 
					
						
							|  |  |  |         self.assertEqual(b'chunk1 chunk2 chunk3 \n', line) | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         self.assertEqual(b' chunk4', stream._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_readline_limit_with_existing_data(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         # Read one line. The data is in StreamReader's buffer | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         # before the event loop is run. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(limit=3, loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_data(b'li') | 
					
						
							|  |  |  |         stream.feed_data(b'ne1\nline2\n') | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.assertRaises( | 
					
						
							|  |  |  |             ValueError, self.loop.run_until_complete, stream.readline()) | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         # The buffer should contain the remaining data after exception | 
					
						
							|  |  |  |         self.assertEqual(b'line2\n', stream._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(limit=3, loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_data(b'li') | 
					
						
							|  |  |  |         stream.feed_data(b'ne1') | 
					
						
							|  |  |  |         stream.feed_data(b'li') | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.assertRaises( | 
					
						
							|  |  |  |             ValueError, self.loop.run_until_complete, stream.readline()) | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         # No b'\n' at the end. The 'limit' is set to 3. So before | 
					
						
							|  |  |  |         # waiting for the new data in buffer, 'readline' will consume | 
					
						
							|  |  |  |         # the entire buffer, and since the length of the consumed data | 
					
						
							| 
									
										
										
										
											2014-02-18 22:27:48 -05:00
										 |  |  |         # is more than 3, it will raise a ValueError. The buffer is | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         # expected to be empty now. | 
					
						
							|  |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 00:14:30 -05:00
										 |  |  |     def test_at_eof(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2014-02-06 00:14:30 -05:00
										 |  |  |         self.assertFalse(stream.at_eof()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.feed_data(b'some data\n') | 
					
						
							| 
									
										
										
										
											2014-02-06 00:14:30 -05:00
										 |  |  |         self.assertFalse(stream.at_eof()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.loop.run_until_complete(stream.readline()) | 
					
						
							|  |  |  |         self.assertFalse(stream.at_eof()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.feed_data(b'some data\n') | 
					
						
							|  |  |  |         stream.feed_eof() | 
					
						
							| 
									
										
										
										
											2014-02-06 00:14:30 -05:00
										 |  |  |         self.loop.run_until_complete(stream.readline()) | 
					
						
							|  |  |  |         self.assertTrue(stream.at_eof()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def test_readline_limit(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         # Read one line. StreamReaders are fed with data after | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         # their 'readline' methods are called. | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(limit=7, loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         def cb(): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             stream.feed_data(b'chunk1') | 
					
						
							|  |  |  |             stream.feed_data(b'chunk2') | 
					
						
							|  |  |  |             stream.feed_data(b'chunk3\n') | 
					
						
							|  |  |  |             stream.feed_eof() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.loop.call_soon(cb) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertRaises( | 
					
						
							|  |  |  |             ValueError, self.loop.run_until_complete, stream.readline()) | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         # The buffer had just one line of data, and after raising | 
					
						
							|  |  |  |         # a ValueError it should be empty. | 
					
						
							|  |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(limit=7, loop=self.loop) | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         def cb(): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             stream.feed_data(b'chunk1') | 
					
						
							|  |  |  |             stream.feed_data(b'chunk2\n') | 
					
						
							|  |  |  |             stream.feed_data(b'chunk3\n') | 
					
						
							|  |  |  |             stream.feed_eof() | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         self.loop.call_soon(cb) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertRaises( | 
					
						
							|  |  |  |             ValueError, self.loop.run_until_complete, stream.readline()) | 
					
						
							|  |  |  |         self.assertEqual(b'chunk3\n', stream._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |         # check strictness of the limit | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(limit=7, loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_data(b'1234567\n') | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |         line = self.loop.run_until_complete(stream.readline()) | 
					
						
							|  |  |  |         self.assertEqual(b'1234567\n', line) | 
					
						
							|  |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.feed_data(b'12345678\n') | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |         with self.assertRaises(ValueError) as cm: | 
					
						
							|  |  |  |             self.loop.run_until_complete(stream.readline()) | 
					
						
							|  |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.feed_data(b'12345678') | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |         with self.assertRaises(ValueError) as cm: | 
					
						
							|  |  |  |             self.loop.run_until_complete(stream.readline()) | 
					
						
							|  |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |     def test_readline_nolimit_nowait(self): | 
					
						
							|  |  |  |         # All needed data for the first 'readline' call will be | 
					
						
							|  |  |  |         # in the buffer. | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_data(self.DATA[:6]) | 
					
						
							|  |  |  |         stream.feed_data(self.DATA[6:]) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         line = self.loop.run_until_complete(stream.readline()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(b'line1\n', line) | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         self.assertEqual(b'line2\nline3\n', stream._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_readline_eof(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_data(b'some data') | 
					
						
							|  |  |  |         stream.feed_eof() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         line = self.loop.run_until_complete(stream.readline()) | 
					
						
							|  |  |  |         self.assertEqual(b'some data', line) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_readline_empty_eof(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_eof() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         line = self.loop.run_until_complete(stream.readline()) | 
					
						
							|  |  |  |         self.assertEqual(b'', line) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_readline_read_byte_count(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_data(self.DATA) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.loop.run_until_complete(stream.readline()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         data = self.loop.run_until_complete(stream.read(7)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(b'line2\nl', data) | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         self.assertEqual(b'ine3\n', stream._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_readline_exception(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_data(b'line\n') | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         data = self.loop.run_until_complete(stream.readline()) | 
					
						
							|  |  |  |         self.assertEqual(b'line\n', data) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.set_exception(ValueError()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertRaises( | 
					
						
							|  |  |  |             ValueError, self.loop.run_until_complete, stream.readline()) | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |     def test_readuntil_separator(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |         with self.assertRaisesRegex(ValueError, 'Separator should be'): | 
					
						
							|  |  |  |             self.loop.run_until_complete(stream.readuntil(separator=b'')) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:58:02 +02:00
										 |  |  |         with self.assertRaisesRegex(ValueError, 'Separator should be'): | 
					
						
							| 
									
										
										
										
											2024-04-11 16:41:55 +02:00
										 |  |  |             self.loop.run_until_complete(stream.readuntil(separator=(b'',))) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:58:02 +02:00
										 |  |  |         with self.assertRaisesRegex(ValueError, 'Separator should contain'): | 
					
						
							| 
									
										
										
										
											2024-04-11 16:41:55 +02:00
										 |  |  |             self.loop.run_until_complete(stream.readuntil(separator=())) | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_readuntil_multi_chunks(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.feed_data(b'lineAAA') | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |         data = self.loop.run_until_complete(stream.readuntil(separator=b'AAA')) | 
					
						
							|  |  |  |         self.assertEqual(b'lineAAA', data) | 
					
						
							|  |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.feed_data(b'lineAAA') | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |         data = self.loop.run_until_complete(stream.readuntil(b'AAA')) | 
					
						
							|  |  |  |         self.assertEqual(b'lineAAA', data) | 
					
						
							|  |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.feed_data(b'lineAAAxxx') | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |         data = self.loop.run_until_complete(stream.readuntil(b'AAA')) | 
					
						
							|  |  |  |         self.assertEqual(b'lineAAA', data) | 
					
						
							|  |  |  |         self.assertEqual(b'xxx', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_readuntil_multi_chunks_1(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.feed_data(b'QWEaa') | 
					
						
							|  |  |  |         stream.feed_data(b'XYaa') | 
					
						
							|  |  |  |         stream.feed_data(b'a') | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |         data = self.loop.run_until_complete(stream.readuntil(b'aaa')) | 
					
						
							|  |  |  |         self.assertEqual(b'QWEaaXYaaa', data) | 
					
						
							|  |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.feed_data(b'QWEaa') | 
					
						
							|  |  |  |         stream.feed_data(b'XYa') | 
					
						
							|  |  |  |         stream.feed_data(b'aa') | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |         data = self.loop.run_until_complete(stream.readuntil(b'aaa')) | 
					
						
							|  |  |  |         self.assertEqual(b'QWEaaXYaaa', data) | 
					
						
							|  |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.feed_data(b'aaa') | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |         data = self.loop.run_until_complete(stream.readuntil(b'aaa')) | 
					
						
							|  |  |  |         self.assertEqual(b'aaa', data) | 
					
						
							|  |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.feed_data(b'Xaaa') | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |         data = self.loop.run_until_complete(stream.readuntil(b'aaa')) | 
					
						
							|  |  |  |         self.assertEqual(b'Xaaa', data) | 
					
						
							|  |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.feed_data(b'XXX') | 
					
						
							|  |  |  |         stream.feed_data(b'a') | 
					
						
							|  |  |  |         stream.feed_data(b'a') | 
					
						
							|  |  |  |         stream.feed_data(b'a') | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |         data = self.loop.run_until_complete(stream.readuntil(b'aaa')) | 
					
						
							|  |  |  |         self.assertEqual(b'XXXaaa', data) | 
					
						
							|  |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_readuntil_eof(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2020-11-28 07:27:28 -07:00
										 |  |  |         data = b'some dataAA' | 
					
						
							|  |  |  |         stream.feed_data(data) | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.feed_eof() | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-28 07:27:28 -07:00
										 |  |  |         with self.assertRaisesRegex(asyncio.IncompleteReadError, | 
					
						
							|  |  |  |                                     'undefined expected bytes') as cm: | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |             self.loop.run_until_complete(stream.readuntil(b'AAA')) | 
					
						
							| 
									
										
										
										
											2020-11-28 07:27:28 -07:00
										 |  |  |         self.assertEqual(cm.exception.partial, data) | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |         self.assertIsNone(cm.exception.expected) | 
					
						
							|  |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_readuntil_limit_found_sep(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop, limit=3) | 
					
						
							|  |  |  |         stream.feed_data(b'some dataAA') | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |         with self.assertRaisesRegex(asyncio.LimitOverrunError, | 
					
						
							|  |  |  |                                     'not found') as cm: | 
					
						
							|  |  |  |             self.loop.run_until_complete(stream.readuntil(b'AAA')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(b'some dataAA', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.feed_data(b'A') | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |         with self.assertRaisesRegex(asyncio.LimitOverrunError, | 
					
						
							|  |  |  |                                     'is found') as cm: | 
					
						
							|  |  |  |             self.loop.run_until_complete(stream.readuntil(b'AAA')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(b'some dataAAA', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:58:02 +02:00
										 |  |  |     def test_readuntil_multi_separator(self): | 
					
						
							|  |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Simple case | 
					
						
							|  |  |  |         stream.feed_data(b'line 1\nline 2\r') | 
					
						
							| 
									
										
										
										
											2024-04-11 16:41:55 +02:00
										 |  |  |         data = self.loop.run_until_complete(stream.readuntil((b'\r', b'\n'))) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:58:02 +02:00
										 |  |  |         self.assertEqual(b'line 1\n', data) | 
					
						
							| 
									
										
										
										
											2024-04-11 16:41:55 +02:00
										 |  |  |         data = self.loop.run_until_complete(stream.readuntil((b'\r', b'\n'))) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:58:02 +02:00
										 |  |  |         self.assertEqual(b'line 2\r', data) | 
					
						
							|  |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # First end position matches, even if that's a longer match | 
					
						
							|  |  |  |         stream.feed_data(b'ABCDEFG') | 
					
						
							| 
									
										
										
										
											2024-04-11 16:41:55 +02:00
										 |  |  |         data = self.loop.run_until_complete(stream.readuntil((b'DEF', b'BCDE'))) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:58:02 +02:00
										 |  |  |         self.assertEqual(b'ABCDE', data) | 
					
						
							|  |  |  |         self.assertEqual(b'FG', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_readuntil_multi_separator_limit(self): | 
					
						
							|  |  |  |         stream = asyncio.StreamReader(loop=self.loop, limit=3) | 
					
						
							|  |  |  |         stream.feed_data(b'some dataA') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with self.assertRaisesRegex(asyncio.LimitOverrunError, | 
					
						
							|  |  |  |                                     'is found') as cm: | 
					
						
							| 
									
										
										
										
											2024-04-11 16:41:55 +02:00
										 |  |  |             self.loop.run_until_complete(stream.readuntil((b'A', b'ome dataA'))) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:58:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(b'some dataA', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_readuntil_multi_separator_negative_offset(self): | 
					
						
							|  |  |  |         # If the buffer is big enough for the smallest separator (but does | 
					
						
							|  |  |  |         # not contain it) but too small for the largest, `offset` must not | 
					
						
							|  |  |  |         # become negative. | 
					
						
							|  |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_data(b'data') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-11 16:41:55 +02:00
										 |  |  |         readuntil_task = self.loop.create_task(stream.readuntil((b'A', b'long sep'))) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:58:02 +02:00
										 |  |  |         self.loop.call_soon(stream.feed_data, b'Z') | 
					
						
							|  |  |  |         self.loop.call_soon(stream.feed_data, b'Aaaa') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         data = self.loop.run_until_complete(readuntil_task) | 
					
						
							|  |  |  |         self.assertEqual(b'dataZA', data) | 
					
						
							|  |  |  |         self.assertEqual(b'aaa', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-11 16:41:55 +02:00
										 |  |  |     def test_readuntil_bytearray(self): | 
					
						
							|  |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_data(b'some data\r\n') | 
					
						
							|  |  |  |         data = self.loop.run_until_complete(stream.readuntil(bytearray(b'\r\n'))) | 
					
						
							|  |  |  |         self.assertEqual(b'some data\r\n', data) | 
					
						
							|  |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def test_readexactly_zero_or_less(self): | 
					
						
							|  |  |  |         # Read exact number of bytes (zero or less). | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_data(self.DATA) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         data = self.loop.run_until_complete(stream.readexactly(0)) | 
					
						
							|  |  |  |         self.assertEqual(b'', data) | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         self.assertEqual(self.DATA, stream._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 19:40:03 -05:00
										 |  |  |         with self.assertRaisesRegex(ValueError, 'less than zero'): | 
					
						
							| 
									
										
										
										
											2015-12-11 11:32:59 -05:00
										 |  |  |             self.loop.run_until_complete(stream.readexactly(-1)) | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         self.assertEqual(self.DATA, stream._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_readexactly(self): | 
					
						
							|  |  |  |         # Read exact number of bytes. | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         n = 2 * len(self.DATA) | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         read_task = self.loop.create_task(stream.readexactly(n)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def cb(): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             stream.feed_data(self.DATA) | 
					
						
							|  |  |  |             stream.feed_data(self.DATA) | 
					
						
							|  |  |  |             stream.feed_data(self.DATA) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.loop.call_soon(cb) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         data = self.loop.run_until_complete(read_task) | 
					
						
							|  |  |  |         self.assertEqual(self.DATA + self.DATA, data) | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         self.assertEqual(self.DATA, stream._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |     def test_readexactly_limit(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(limit=3, loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_data(b'chunk') | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  |         data = self.loop.run_until_complete(stream.readexactly(5)) | 
					
						
							|  |  |  |         self.assertEqual(b'chunk', data) | 
					
						
							|  |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def test_readexactly_eof(self): | 
					
						
							|  |  |  |         # Read exact number of bytes (eof). | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         n = 2 * len(self.DATA) | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         read_task = self.loop.create_task(stream.readexactly(n)) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def cb(): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             stream.feed_data(self.DATA) | 
					
						
							|  |  |  |             stream.feed_eof() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.loop.call_soon(cb) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  |         with self.assertRaises(asyncio.IncompleteReadError) as cm: | 
					
						
							|  |  |  |             self.loop.run_until_complete(read_task) | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.partial, self.DATA) | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.expected, n) | 
					
						
							|  |  |  |         self.assertEqual(str(cm.exception), | 
					
						
							|  |  |  |                          '18 bytes read on a total of 36 expected bytes') | 
					
						
							| 
									
										
										
										
											2014-02-05 18:11:13 -05:00
										 |  |  |         self.assertEqual(b'', stream._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_readexactly_exception(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_data(b'line\n') | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         data = self.loop.run_until_complete(stream.readexactly(2)) | 
					
						
							|  |  |  |         self.assertEqual(b'li', data) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.set_exception(ValueError()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertRaises( | 
					
						
							|  |  |  |             ValueError, self.loop.run_until_complete, stream.readexactly(2)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_exception(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertIsNone(stream.exception()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         exc = ValueError() | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.set_exception(exc) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self.assertIs(stream.exception(), exc) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_exception_waiter(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-16 17:52:10 +03:00
										 |  |  |         async def set_err(): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             stream.set_exception(ValueError()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         t1 = self.loop.create_task(stream.readline()) | 
					
						
							|  |  |  |         t2 = self.loop.create_task(set_err()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-02 13:53:06 -04:00
										 |  |  |         self.loop.run_until_complete(asyncio.wait([t1, t2])) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.assertRaises(ValueError, t1.result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_exception_cancel(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:07:37 +03:00
										 |  |  |         t = self.loop.create_task(stream.readline()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         t.cancel() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         # The following line fails if set_exception() isn't careful. | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.set_exception(RuntimeError('message')) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							| 
									
										
										
										
											2013-10-18 15:17:11 -07:00
										 |  |  |         self.assertIs(stream._waiter, None) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |     def test_start_server(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         class MyServer: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             def __init__(self, loop): | 
					
						
							|  |  |  |                 self.server = None | 
					
						
							|  |  |  |                 self.loop = loop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             async def handle_client(self, client_reader, client_writer): | 
					
						
							|  |  |  |                 data = await client_reader.readline() | 
					
						
							|  |  |  |                 client_writer.write(data) | 
					
						
							|  |  |  |                 await client_writer.drain() | 
					
						
							|  |  |  |                 client_writer.close() | 
					
						
							|  |  |  |                 await client_writer.wait_closed() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             def start(self): | 
					
						
							|  |  |  |                 sock = socket.create_server(('127.0.0.1', 0)) | 
					
						
							|  |  |  |                 self.server = self.loop.run_until_complete( | 
					
						
							|  |  |  |                     asyncio.start_server(self.handle_client, | 
					
						
							|  |  |  |                                          sock=sock)) | 
					
						
							|  |  |  |                 return sock.getsockname() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             def handle_client_callback(self, client_reader, client_writer): | 
					
						
							|  |  |  |                 self.loop.create_task(self.handle_client(client_reader, | 
					
						
							|  |  |  |                                                          client_writer)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             def start_callback(self): | 
					
						
							|  |  |  |                 sock = socket.create_server(('127.0.0.1', 0)) | 
					
						
							|  |  |  |                 addr = sock.getsockname() | 
					
						
							|  |  |  |                 sock.close() | 
					
						
							|  |  |  |                 self.server = self.loop.run_until_complete( | 
					
						
							|  |  |  |                     asyncio.start_server(self.handle_client_callback, | 
					
						
							|  |  |  |                                          host=addr[0], port=addr[1])) | 
					
						
							|  |  |  |                 return addr | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             def stop(self): | 
					
						
							|  |  |  |                 if self.server is not None: | 
					
						
							|  |  |  |                     self.server.close() | 
					
						
							|  |  |  |                     self.loop.run_until_complete(self.server.wait_closed()) | 
					
						
							|  |  |  |                     self.server = None | 
					
						
							| 
									
										
										
										
											2013-11-19 11:43:38 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def client(addr): | 
					
						
							| 
									
										
										
										
											2020-11-26 09:36:37 +02:00
										 |  |  |             reader, writer = await asyncio.open_connection(*addr) | 
					
						
							| 
									
										
										
										
											2013-11-19 11:43:38 -08:00
										 |  |  |             # send a line | 
					
						
							|  |  |  |             writer.write(b"hello world!\n") | 
					
						
							|  |  |  |             # read it back | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |             msgback = await reader.readline() | 
					
						
							| 
									
										
										
										
											2013-11-19 11:43:38 -08:00
										 |  |  |             writer.close() | 
					
						
							| 
									
										
										
										
											2018-11-08 13:21:47 +01:00
										 |  |  |             await writer.wait_closed() | 
					
						
							| 
									
										
										
										
											2013-11-19 11:43:38 -08:00
										 |  |  |             return msgback | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |         messages = [] | 
					
						
							|  |  |  |         self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) | 
					
						
							| 
									
										
										
										
											2013-11-19 11:43:38 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |         # test the server variant with a coroutine as client handler | 
					
						
							|  |  |  |         server = MyServer(self.loop) | 
					
						
							|  |  |  |         addr = server.start() | 
					
						
							|  |  |  |         msg = self.loop.run_until_complete(self.loop.create_task(client(addr))) | 
					
						
							|  |  |  |         server.stop() | 
					
						
							|  |  |  |         self.assertEqual(msg, b"hello world!\n") | 
					
						
							| 
									
										
										
										
											2013-11-19 11:43:38 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |         # test the server variant with a callback as client handler | 
					
						
							|  |  |  |         server = MyServer(self.loop) | 
					
						
							|  |  |  |         addr = server.start_callback() | 
					
						
							|  |  |  |         msg = self.loop.run_until_complete(self.loop.create_task(client(addr))) | 
					
						
							|  |  |  |         server.stop() | 
					
						
							|  |  |  |         self.assertEqual(msg, b"hello world!\n") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(messages, []) | 
					
						
							| 
									
										
										
										
											2018-09-12 11:43:04 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 10:06:29 +03:00
										 |  |  |     @socket_helper.skip_unless_bind_unix_socket | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |     def test_start_unix_server(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         class MyServer: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             def __init__(self, loop, path): | 
					
						
							|  |  |  |                 self.server = None | 
					
						
							|  |  |  |                 self.loop = loop | 
					
						
							|  |  |  |                 self.path = path | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             async def handle_client(self, client_reader, client_writer): | 
					
						
							|  |  |  |                 data = await client_reader.readline() | 
					
						
							|  |  |  |                 client_writer.write(data) | 
					
						
							|  |  |  |                 await client_writer.drain() | 
					
						
							|  |  |  |                 client_writer.close() | 
					
						
							|  |  |  |                 await client_writer.wait_closed() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             def start(self): | 
					
						
							|  |  |  |                 self.server = self.loop.run_until_complete( | 
					
						
							|  |  |  |                     asyncio.start_unix_server(self.handle_client, | 
					
						
							|  |  |  |                                               path=self.path)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             def handle_client_callback(self, client_reader, client_writer): | 
					
						
							|  |  |  |                 self.loop.create_task(self.handle_client(client_reader, | 
					
						
							|  |  |  |                                                          client_writer)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             def start_callback(self): | 
					
						
							|  |  |  |                 start = asyncio.start_unix_server(self.handle_client_callback, | 
					
						
							|  |  |  |                                                   path=self.path) | 
					
						
							|  |  |  |                 self.server = self.loop.run_until_complete(start) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             def stop(self): | 
					
						
							|  |  |  |                 if self.server is not None: | 
					
						
							|  |  |  |                     self.server.close() | 
					
						
							|  |  |  |                     self.loop.run_until_complete(self.server.wait_closed()) | 
					
						
							|  |  |  |                     self.server = None | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def client(path): | 
					
						
							| 
									
										
										
										
											2020-11-26 09:36:37 +02:00
										 |  |  |             reader, writer = await asyncio.open_unix_connection(path) | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |             # send a line | 
					
						
							|  |  |  |             writer.write(b"hello world!\n") | 
					
						
							|  |  |  |             # read it back | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |             msgback = await reader.readline() | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |             writer.close() | 
					
						
							| 
									
										
										
										
											2018-11-08 13:21:47 +01:00
										 |  |  |             await writer.wait_closed() | 
					
						
							| 
									
										
										
										
											2014-02-18 12:15:06 -05:00
										 |  |  |             return msgback | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |         messages = [] | 
					
						
							|  |  |  |         self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # test the server variant with a coroutine as client handler | 
					
						
							|  |  |  |         with test_utils.unix_socket_path() as path: | 
					
						
							|  |  |  |             server = MyServer(self.loop, path) | 
					
						
							|  |  |  |             server.start() | 
					
						
							|  |  |  |             msg = self.loop.run_until_complete( | 
					
						
							|  |  |  |                 self.loop.create_task(client(path))) | 
					
						
							|  |  |  |             server.stop() | 
					
						
							|  |  |  |             self.assertEqual(msg, b"hello world!\n") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # test the server variant with a callback as client handler | 
					
						
							|  |  |  |         with test_utils.unix_socket_path() as path: | 
					
						
							|  |  |  |             server = MyServer(self.loop, path) | 
					
						
							|  |  |  |             server.start_callback() | 
					
						
							|  |  |  |             msg = self.loop.run_until_complete( | 
					
						
							|  |  |  |                 self.loop.create_task(client(path))) | 
					
						
							|  |  |  |             server.stop() | 
					
						
							|  |  |  |             self.assertEqual(msg, b"hello world!\n") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(messages, []) | 
					
						
							| 
									
										
										
										
											2018-09-12 11:43:04 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 15:23:14 +03:00
										 |  |  |     @unittest.skipIf(ssl is None, 'No ssl module') | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |     def test_start_tls(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         class MyServer: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             def __init__(self, loop): | 
					
						
							|  |  |  |                 self.server = None | 
					
						
							|  |  |  |                 self.loop = loop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             async def handle_client(self, client_reader, client_writer): | 
					
						
							|  |  |  |                 data1 = await client_reader.readline() | 
					
						
							|  |  |  |                 client_writer.write(data1) | 
					
						
							|  |  |  |                 await client_writer.drain() | 
					
						
							|  |  |  |                 assert client_writer.get_extra_info('sslcontext') is None | 
					
						
							|  |  |  |                 await client_writer.start_tls( | 
					
						
							|  |  |  |                     test_utils.simple_server_sslcontext()) | 
					
						
							|  |  |  |                 assert client_writer.get_extra_info('sslcontext') is not None | 
					
						
							|  |  |  |                 data2 = await client_reader.readline() | 
					
						
							|  |  |  |                 client_writer.write(data2) | 
					
						
							|  |  |  |                 await client_writer.drain() | 
					
						
							|  |  |  |                 client_writer.close() | 
					
						
							|  |  |  |                 await client_writer.wait_closed() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             def start(self): | 
					
						
							|  |  |  |                 sock = socket.create_server(('127.0.0.1', 0)) | 
					
						
							|  |  |  |                 self.server = self.loop.run_until_complete( | 
					
						
							|  |  |  |                     asyncio.start_server(self.handle_client, | 
					
						
							|  |  |  |                                          sock=sock)) | 
					
						
							|  |  |  |                 return sock.getsockname() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             def stop(self): | 
					
						
							|  |  |  |                 if self.server is not None: | 
					
						
							|  |  |  |                     self.server.close() | 
					
						
							|  |  |  |                     self.loop.run_until_complete(self.server.wait_closed()) | 
					
						
							|  |  |  |                     self.server = None | 
					
						
							| 
									
										
										
										
											2022-04-15 15:23:14 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         async def client(addr): | 
					
						
							|  |  |  |             reader, writer = await asyncio.open_connection(*addr) | 
					
						
							|  |  |  |             writer.write(b"hello world 1!\n") | 
					
						
							|  |  |  |             await writer.drain() | 
					
						
							|  |  |  |             msgback1 = await reader.readline() | 
					
						
							|  |  |  |             assert writer.get_extra_info('sslcontext') is None | 
					
						
							|  |  |  |             await writer.start_tls(test_utils.simple_client_sslcontext()) | 
					
						
							|  |  |  |             assert writer.get_extra_info('sslcontext') is not None | 
					
						
							|  |  |  |             writer.write(b"hello world 2!\n") | 
					
						
							|  |  |  |             await writer.drain() | 
					
						
							|  |  |  |             msgback2 = await reader.readline() | 
					
						
							|  |  |  |             writer.close() | 
					
						
							|  |  |  |             await writer.wait_closed() | 
					
						
							|  |  |  |             return msgback1, msgback2 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |         messages = [] | 
					
						
							|  |  |  |         self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) | 
					
						
							| 
									
										
										
										
											2022-10-04 21:56:47 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |         server = MyServer(self.loop) | 
					
						
							|  |  |  |         addr = server.start() | 
					
						
							|  |  |  |         msg1, msg2 = self.loop.run_until_complete(client(addr)) | 
					
						
							|  |  |  |         server.stop() | 
					
						
							| 
									
										
										
										
											2022-10-04 21:56:47 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |         self.assertEqual(messages, []) | 
					
						
							|  |  |  |         self.assertEqual(msg1, b"hello world 1!\n") | 
					
						
							|  |  |  |         self.assertEqual(msg2, b"hello world 2!\n") | 
					
						
							| 
									
										
										
										
											2022-10-04 21:56:47 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-25 13:40:44 +03:00
										 |  |  |     def test_streamreader_constructor_without_loop(self): | 
					
						
							| 
									
										
										
										
											2022-12-06 19:42:12 +02:00
										 |  |  |         with self.assertRaisesRegex(RuntimeError, 'no current event loop'): | 
					
						
							|  |  |  |             asyncio.StreamReader() | 
					
						
							| 
									
										
										
										
											2015-01-09 21:32:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-25 13:40:44 +03:00
										 |  |  |     def test_streamreader_constructor_use_running_loop(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         # asyncio issue #184: Ensure that StreamReaderProtocol constructor | 
					
						
							| 
									
										
										
										
											2015-01-09 21:32:05 +01:00
										 |  |  |         # retrieves the current loop if the loop parameter is not set | 
					
						
							| 
									
										
										
										
											2021-04-25 13:40:44 +03:00
										 |  |  |         async def test(): | 
					
						
							|  |  |  |             return asyncio.StreamReader() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         reader = self.loop.run_until_complete(test()) | 
					
						
							| 
									
										
										
										
											2015-01-09 21:32:05 +01:00
										 |  |  |         self.assertIs(reader._loop, self.loop) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-25 13:40:44 +03:00
										 |  |  |     def test_streamreader_constructor_use_global_loop(self): | 
					
						
							|  |  |  |         # asyncio issue #184: Ensure that StreamReaderProtocol constructor | 
					
						
							|  |  |  |         # retrieves the current loop if the loop parameter is not set | 
					
						
							| 
									
										
										
										
											2022-12-06 19:42:12 +02:00
										 |  |  |         # Deprecated in 3.10, undeprecated in 3.12 | 
					
						
							| 
									
										
										
										
											2025-04-12 12:03:52 +05:30
										 |  |  |         self.addCleanup(asyncio.set_event_loop, None) | 
					
						
							|  |  |  |         asyncio.set_event_loop(self.loop) | 
					
						
							| 
									
										
										
										
											2022-12-06 19:42:12 +02:00
										 |  |  |         reader = asyncio.StreamReader() | 
					
						
							| 
									
										
										
										
											2021-04-25 13:40:44 +03:00
										 |  |  |         self.assertIs(reader._loop, self.loop) | 
					
						
							| 
									
										
										
										
											2015-01-09 21:32:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-25 13:40:44 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_streamreaderprotocol_constructor_without_loop(self): | 
					
						
							|  |  |  |         reader = mock.Mock() | 
					
						
							| 
									
										
										
										
											2022-12-06 19:42:12 +02:00
										 |  |  |         with self.assertRaisesRegex(RuntimeError, 'no current event loop'): | 
					
						
							|  |  |  |             asyncio.StreamReaderProtocol(reader) | 
					
						
							| 
									
										
										
										
											2021-04-25 13:40:44 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_streamreaderprotocol_constructor_use_running_loop(self): | 
					
						
							|  |  |  |         # asyncio issue #184: Ensure that StreamReaderProtocol constructor | 
					
						
							|  |  |  |         # retrieves the current loop if the loop parameter is not set | 
					
						
							|  |  |  |         reader = mock.Mock() | 
					
						
							|  |  |  |         async def test(): | 
					
						
							|  |  |  |             return asyncio.StreamReaderProtocol(reader) | 
					
						
							|  |  |  |         protocol = self.loop.run_until_complete(test()) | 
					
						
							|  |  |  |         self.assertIs(protocol._loop, self.loop) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_streamreaderprotocol_constructor_use_global_loop(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         # asyncio issue #184: Ensure that StreamReaderProtocol constructor | 
					
						
							| 
									
										
										
										
											2015-01-09 21:32:05 +01:00
										 |  |  |         # retrieves the current loop if the loop parameter is not set | 
					
						
							| 
									
										
										
										
											2022-12-06 19:42:12 +02:00
										 |  |  |         # Deprecated in 3.10, undeprecated in 3.12 | 
					
						
							| 
									
										
										
										
											2025-04-12 12:03:52 +05:30
										 |  |  |         self.addCleanup(asyncio.set_event_loop, None) | 
					
						
							|  |  |  |         asyncio.set_event_loop(self.loop) | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         reader = mock.Mock() | 
					
						
							| 
									
										
										
										
											2022-12-06 19:42:12 +02:00
										 |  |  |         protocol = asyncio.StreamReaderProtocol(reader) | 
					
						
							| 
									
										
										
										
											2015-01-09 21:32:05 +01:00
										 |  |  |         self.assertIs(protocol._loop, self.loop) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-30 00:01:11 +05:30
										 |  |  |     def test_multiple_drain(self): | 
					
						
							|  |  |  |         # See https://github.com/python/cpython/issues/74116 | 
					
						
							|  |  |  |         drained = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         async def drainer(stream): | 
					
						
							|  |  |  |             nonlocal drained | 
					
						
							|  |  |  |             await stream._drain_helper() | 
					
						
							|  |  |  |             drained += 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         async def main(): | 
					
						
							|  |  |  |             loop = asyncio.get_running_loop() | 
					
						
							|  |  |  |             stream = asyncio.streams.FlowControlMixin(loop) | 
					
						
							|  |  |  |             stream.pause_writing() | 
					
						
							|  |  |  |             loop.call_later(0.1, stream.resume_writing) | 
					
						
							|  |  |  |             await asyncio.gather(*[drainer(stream) for _ in range(10)]) | 
					
						
							|  |  |  |             self.assertEqual(drained, 10) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.loop.run_until_complete(main()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |     def test_drain_raises(self): | 
					
						
							| 
									
										
										
										
											2015-10-19 11:49:30 -07:00
										 |  |  |         # See http://bugs.python.org/issue25441 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # This test should not use asyncio for the mock server; the | 
					
						
							|  |  |  |         # whole point of the test is to test for a bug in drain() | 
					
						
							|  |  |  |         # where it never gives up the event loop but the socket is | 
					
						
							|  |  |  |         # closed on the  server side. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-14 19:09:44 +03:00
										 |  |  |         messages = [] | 
					
						
							|  |  |  |         self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) | 
					
						
							| 
									
										
										
										
											2015-10-19 11:49:30 -07:00
										 |  |  |         q = queue.Queue() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def server(): | 
					
						
							|  |  |  |             # Runs in a separate thread. | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             with socket.create_server(('localhost', 0)) as sock: | 
					
						
							| 
									
										
										
										
											2015-12-16 19:51:09 -05:00
										 |  |  |                 addr = sock.getsockname() | 
					
						
							|  |  |  |                 q.put(addr) | 
					
						
							|  |  |  |                 clt, _ = sock.accept() | 
					
						
							|  |  |  |                 clt.close() | 
					
						
							| 
									
										
										
										
											2015-10-19 12:00:04 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         async def client(host, port): | 
					
						
							| 
									
										
										
										
											2020-11-26 09:36:37 +02:00
										 |  |  |             reader, writer = await asyncio.open_connection(host, port) | 
					
						
							| 
									
										
										
										
											2016-01-11 12:28:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-19 11:49:30 -07:00
										 |  |  |             while True: | 
					
						
							|  |  |  |                 writer.write(b"foo\n") | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |                 await writer.drain() | 
					
						
							| 
									
										
										
										
											2015-10-19 11:49:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Start the server thread and wait for it to be listening. | 
					
						
							|  |  |  |         thread = threading.Thread(target=server) | 
					
						
							| 
									
										
										
										
											2021-04-12 13:12:36 +02:00
										 |  |  |         thread.daemon = True | 
					
						
							| 
									
										
										
										
											2015-10-19 11:49:30 -07:00
										 |  |  |         thread.start() | 
					
						
							|  |  |  |         addr = q.get() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Should not be stuck in an infinite loop. | 
					
						
							| 
									
										
										
										
											2018-09-25 08:27:08 -07:00
										 |  |  |         with self.assertRaises((ConnectionResetError, ConnectionAbortedError, | 
					
						
							|  |  |  |                                 BrokenPipeError)): | 
					
						
							| 
									
										
										
										
											2015-10-19 11:49:30 -07:00
										 |  |  |             self.loop.run_until_complete(client(*addr)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Clean up the thread.  (Only on success; on failure, it may | 
					
						
							|  |  |  |         # be stuck in accept().) | 
					
						
							|  |  |  |         thread.join() | 
					
						
							| 
									
										
										
										
											2019-05-14 19:09:44 +03:00
										 |  |  |         self.assertEqual([], messages) | 
					
						
							| 
									
										
										
										
											2015-10-19 11:49:30 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 18:36:00 +03:00
										 |  |  |     def test___repr__(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  |         self.assertEqual("<StreamReader>", repr(stream)) | 
					
						
							| 
									
										
										
										
											2015-09-29 18:36:00 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test___repr__nondefault_limit(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop, limit=123) | 
					
						
							|  |  |  |         self.assertEqual("<StreamReader limit=123>", repr(stream)) | 
					
						
							| 
									
										
										
										
											2015-09-29 18:36:00 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test___repr__eof(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_eof() | 
					
						
							|  |  |  |         self.assertEqual("<StreamReader eof>", repr(stream)) | 
					
						
							| 
									
										
										
										
											2015-09-29 18:36:00 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test___repr__data(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  |         stream.feed_data(b'data') | 
					
						
							|  |  |  |         self.assertEqual("<StreamReader 4 bytes>", repr(stream)) | 
					
						
							| 
									
										
										
										
											2015-09-29 18:36:00 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test___repr__exception(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2015-09-29 18:36:00 +03:00
										 |  |  |         exc = RuntimeError() | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream.set_exception(exc) | 
					
						
							|  |  |  |         self.assertEqual("<StreamReader exception=RuntimeError()>", | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |                          repr(stream)) | 
					
						
							| 
									
										
										
										
											2015-09-29 18:36:00 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test___repr__waiter(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							|  |  |  |         stream._waiter = asyncio.Future(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2015-09-29 18:36:00 +03:00
										 |  |  |         self.assertRegex( | 
					
						
							|  |  |  |             repr(stream), | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             r"<StreamReader waiter=<Future pending[\S ]*>>") | 
					
						
							| 
									
										
										
										
											2015-09-29 18:36:00 +03:00
										 |  |  |         stream._waiter.set_result(None) | 
					
						
							|  |  |  |         self.loop.run_until_complete(stream._waiter) | 
					
						
							|  |  |  |         stream._waiter = None | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         self.assertEqual("<StreamReader>", repr(stream)) | 
					
						
							| 
									
										
										
										
											2015-09-29 18:36:00 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test___repr__transport(self): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         stream = asyncio.StreamReader(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2015-09-29 18:36:00 +03:00
										 |  |  |         stream._transport = mock.Mock() | 
					
						
							|  |  |  |         stream._transport.__repr__ = mock.Mock() | 
					
						
							|  |  |  |         stream._transport.__repr__.return_value = "<Transport>" | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |         self.assertEqual("<StreamReader transport=<Transport>>", repr(stream)) | 
					
						
							| 
									
										
										
										
											2015-09-29 18:36:00 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 17:14:28 -05:00
										 |  |  |     def test_IncompleteReadError_pickleable(self): | 
					
						
							|  |  |  |         e = asyncio.IncompleteReadError(b'abc', 10) | 
					
						
							|  |  |  |         for proto in range(pickle.HIGHEST_PROTOCOL + 1): | 
					
						
							|  |  |  |             with self.subTest(pickle_protocol=proto): | 
					
						
							|  |  |  |                 e2 = pickle.loads(pickle.dumps(e, protocol=proto)) | 
					
						
							|  |  |  |                 self.assertEqual(str(e), str(e2)) | 
					
						
							|  |  |  |                 self.assertEqual(e.partial, e2.partial) | 
					
						
							|  |  |  |                 self.assertEqual(e.expected, e2.expected) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_LimitOverrunError_pickleable(self): | 
					
						
							|  |  |  |         e = asyncio.LimitOverrunError('message', 10) | 
					
						
							|  |  |  |         for proto in range(pickle.HIGHEST_PROTOCOL + 1): | 
					
						
							|  |  |  |             with self.subTest(pickle_protocol=proto): | 
					
						
							|  |  |  |                 e2 = pickle.loads(pickle.dumps(e, protocol=proto)) | 
					
						
							|  |  |  |                 self.assertEqual(str(e), str(e2)) | 
					
						
							|  |  |  |                 self.assertEqual(e.consumed, e2.consumed) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |     def test_wait_closed_on_close(self): | 
					
						
							| 
									
										
										
										
											2022-10-05 18:31:43 +04:00
										 |  |  |         with test_utils.run_test_server() as httpd: | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |             rd, wr = self.loop.run_until_complete( | 
					
						
							|  |  |  |                 asyncio.open_connection(*httpd.address)) | 
					
						
							| 
									
										
										
										
											2018-01-25 00:30:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             wr.write(b'GET / HTTP/1.0\r\n\r\n') | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |             f = rd.readline() | 
					
						
							|  |  |  |             data = self.loop.run_until_complete(f) | 
					
						
							| 
									
										
										
										
											2018-01-25 00:30:30 +02:00
										 |  |  |             self.assertEqual(data, b'HTTP/1.0 200 OK\r\n') | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |             f = rd.read() | 
					
						
							|  |  |  |             data = self.loop.run_until_complete(f) | 
					
						
							| 
									
										
										
										
											2025-01-20 13:32:39 +02:00
										 |  |  |             self.assertEndsWith(data, b'\r\n\r\nTest message') | 
					
						
							| 
									
										
										
										
											2018-01-25 00:30:30 +02:00
										 |  |  |             self.assertFalse(wr.is_closing()) | 
					
						
							|  |  |  |             wr.close() | 
					
						
							|  |  |  |             self.assertTrue(wr.is_closing()) | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |             self.loop.run_until_complete(wr.wait_closed()) | 
					
						
							| 
									
										
										
										
											2018-01-25 00:30:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |     def test_wait_closed_on_close_with_unread_data(self): | 
					
						
							| 
									
										
										
										
											2019-05-27 22:56:22 +03:00
										 |  |  |         with test_utils.run_test_server() as httpd: | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |             rd, wr = self.loop.run_until_complete( | 
					
						
							|  |  |  |                 asyncio.open_connection(*httpd.address)) | 
					
						
							| 
									
										
										
										
											2018-01-25 00:30:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             wr.write(b'GET / HTTP/1.0\r\n\r\n') | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |             f = rd.readline() | 
					
						
							|  |  |  |             data = self.loop.run_until_complete(f) | 
					
						
							| 
									
										
										
										
											2018-01-25 00:30:30 +02:00
										 |  |  |             self.assertEqual(data, b'HTTP/1.0 200 OK\r\n') | 
					
						
							|  |  |  |             wr.close() | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |             self.loop.run_until_complete(wr.wait_closed()) | 
					
						
							| 
									
										
										
										
											2018-01-25 00:30:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |     def test_async_writer_api(self): | 
					
						
							| 
									
										
										
										
											2019-05-09 15:14:58 -04:00
										 |  |  |         async def inner(httpd): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             rd, wr = await asyncio.open_connection(*httpd.address) | 
					
						
							| 
									
										
										
										
											2019-05-09 15:14:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             wr.write(b'GET / HTTP/1.0\r\n\r\n') | 
					
						
							|  |  |  |             data = await rd.readline() | 
					
						
							| 
									
										
										
										
											2019-05-09 15:14:58 -04:00
										 |  |  |             self.assertEqual(data, b'HTTP/1.0 200 OK\r\n') | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             data = await rd.read() | 
					
						
							| 
									
										
										
										
											2025-01-20 13:32:39 +02:00
										 |  |  |             self.assertEndsWith(data, b'\r\n\r\nTest message') | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             wr.close() | 
					
						
							|  |  |  |             await wr.wait_closed() | 
					
						
							| 
									
										
										
										
											2019-05-09 15:14:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |         messages = [] | 
					
						
							|  |  |  |         self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-13 16:53:49 -07:00
										 |  |  |         with test_utils.run_test_server() as httpd: | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |             self.loop.run_until_complete(inner(httpd)) | 
					
						
							| 
									
										
										
										
											2019-05-09 15:14:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |         self.assertEqual(messages, []) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_async_writer_api_exception_after_close(self): | 
					
						
							| 
									
										
										
										
											2019-05-09 15:14:58 -04:00
										 |  |  |         async def inner(httpd): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             rd, wr = await asyncio.open_connection(*httpd.address) | 
					
						
							| 
									
										
										
										
											2019-05-09 15:14:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             wr.write(b'GET / HTTP/1.0\r\n\r\n') | 
					
						
							|  |  |  |             data = await rd.readline() | 
					
						
							| 
									
										
										
										
											2018-09-13 16:53:49 -07:00
										 |  |  |             self.assertEqual(data, b'HTTP/1.0 200 OK\r\n') | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             data = await rd.read() | 
					
						
							| 
									
										
										
										
											2025-01-20 13:32:39 +02:00
										 |  |  |             self.assertEndsWith(data, b'\r\n\r\nTest message') | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |             wr.close() | 
					
						
							| 
									
										
										
										
											2019-05-09 15:14:58 -04:00
										 |  |  |             with self.assertRaises(ConnectionResetError): | 
					
						
							| 
									
										
										
										
											2019-09-29 21:59:55 -07:00
										 |  |  |                 wr.write(b'data') | 
					
						
							|  |  |  |                 await wr.drain() | 
					
						
							| 
									
										
										
										
											2019-05-09 15:14:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |         messages = [] | 
					
						
							|  |  |  |         self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 15:14:58 -04:00
										 |  |  |         with test_utils.run_test_server() as httpd: | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |             self.loop.run_until_complete(inner(httpd)) | 
					
						
							| 
									
										
										
										
											2018-09-13 16:53:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |         self.assertEqual(messages, []) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_eof_feed_when_closing_writer(self): | 
					
						
							| 
									
										
										
										
											2018-11-08 13:21:47 +01:00
										 |  |  |         # See http://bugs.python.org/issue35065 | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |         messages = [] | 
					
						
							|  |  |  |         self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-05 18:31:43 +04:00
										 |  |  |         with test_utils.run_test_server() as httpd: | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |             rd, wr = self.loop.run_until_complete( | 
					
						
							|  |  |  |                     asyncio.open_connection(*httpd.address)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 22:56:22 +03:00
										 |  |  |             wr.close() | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |             f = wr.wait_closed() | 
					
						
							|  |  |  |             self.loop.run_until_complete(f) | 
					
						
							| 
									
										
										
										
											2021-12-20 12:23:05 +02:00
										 |  |  |             self.assertTrue(rd.at_eof()) | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |             f = rd.read() | 
					
						
							|  |  |  |             data = self.loop.run_until_complete(f) | 
					
						
							| 
									
										
										
										
											2021-12-20 12:23:05 +02:00
										 |  |  |             self.assertEqual(data, b'') | 
					
						
							| 
									
										
										
										
											2018-11-08 13:21:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-07 18:14:28 +04:00
										 |  |  |         self.assertEqual(messages, []) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-05 17:48:15 +05:30
										 |  |  |     def test_unclosed_resource_warnings(self): | 
					
						
							|  |  |  |         async def inner(httpd): | 
					
						
							|  |  |  |             rd, wr = await asyncio.open_connection(*httpd.address) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             wr.write(b'GET / HTTP/1.0\r\n\r\n') | 
					
						
							|  |  |  |             data = await rd.readline() | 
					
						
							|  |  |  |             self.assertEqual(data, b'HTTP/1.0 200 OK\r\n') | 
					
						
							|  |  |  |             data = await rd.read() | 
					
						
							| 
									
										
										
										
											2025-01-20 13:32:39 +02:00
										 |  |  |             self.assertEndsWith(data, b'\r\n\r\nTest message') | 
					
						
							| 
									
										
										
										
											2023-11-15 09:17:51 +08:00
										 |  |  |             with self.assertWarns(ResourceWarning) as cm: | 
					
						
							| 
									
										
										
										
											2023-08-05 17:48:15 +05:30
										 |  |  |                 del wr | 
					
						
							|  |  |  |                 gc.collect() | 
					
						
							| 
									
										
										
										
											2023-11-15 09:17:51 +08:00
										 |  |  |                 self.assertEqual(len(cm.warnings), 1) | 
					
						
							| 
									
										
										
										
											2025-01-20 13:32:39 +02:00
										 |  |  |                 self.assertStartsWith(str(cm.warnings[0].message), "unclosed <StreamWriter") | 
					
						
							| 
									
										
										
										
											2023-08-05 17:48:15 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |         messages = [] | 
					
						
							|  |  |  |         self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with test_utils.run_test_server() as httpd: | 
					
						
							|  |  |  |             self.loop.run_until_complete(inner(httpd)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(messages, []) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-15 09:17:51 +08:00
										 |  |  |     def test_loop_is_closed_resource_warnings(self): | 
					
						
							|  |  |  |         async def inner(httpd): | 
					
						
							|  |  |  |             rd, wr = await asyncio.open_connection(*httpd.address) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             wr.write(b'GET / HTTP/1.0\r\n\r\n') | 
					
						
							|  |  |  |             data = await rd.readline() | 
					
						
							|  |  |  |             self.assertEqual(data, b'HTTP/1.0 200 OK\r\n') | 
					
						
							|  |  |  |             data = await rd.read() | 
					
						
							| 
									
										
										
										
											2025-01-20 13:32:39 +02:00
										 |  |  |             self.assertEndsWith(data, b'\r\n\r\nTest message') | 
					
						
							| 
									
										
										
										
											2023-11-15 09:17:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # Make "loop is closed" occur first before "del wr" for this test. | 
					
						
							|  |  |  |             self.loop.stop() | 
					
						
							|  |  |  |             wr.close() | 
					
						
							|  |  |  |             while not self.loop.is_closed(): | 
					
						
							|  |  |  |                 await asyncio.sleep(0.0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             with self.assertWarns(ResourceWarning) as cm: | 
					
						
							|  |  |  |                 del wr | 
					
						
							|  |  |  |                 gc.collect() | 
					
						
							|  |  |  |                 self.assertEqual(len(cm.warnings), 1) | 
					
						
							|  |  |  |                 self.assertEqual("loop is closed", str(cm.warnings[0].message)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         messages = [] | 
					
						
							|  |  |  |         self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with test_utils.run_test_server() as httpd: | 
					
						
							| 
									
										
										
										
											2023-11-19 11:21:34 +08:00
										 |  |  |             with self.assertRaises(RuntimeError): | 
					
						
							|  |  |  |                 # This exception is caused by `self.loop.stop()` as expected. | 
					
						
							| 
									
										
										
										
											2023-11-15 09:17:51 +08:00
										 |  |  |                 self.loop.run_until_complete(inner(httpd)) | 
					
						
							| 
									
										
										
										
											2023-11-19 11:21:34 +08:00
										 |  |  |             gc.collect() | 
					
						
							| 
									
										
										
										
											2023-11-15 09:17:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(messages, []) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-28 02:27:44 +01:00
										 |  |  |     def test_unclosed_server_resource_warnings(self): | 
					
						
							|  |  |  |         async def inner(rd, wr): | 
					
						
							|  |  |  |             fut.set_result(True) | 
					
						
							|  |  |  |             with self.assertWarns(ResourceWarning) as cm: | 
					
						
							|  |  |  |                 del wr | 
					
						
							|  |  |  |                 gc.collect() | 
					
						
							|  |  |  |                 self.assertEqual(len(cm.warnings), 1) | 
					
						
							| 
									
										
										
										
											2025-01-20 13:32:39 +02:00
										 |  |  |                 self.assertStartsWith(str(cm.warnings[0].message), "unclosed <StreamWriter") | 
					
						
							| 
									
										
										
										
											2024-02-28 02:27:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         async def outer(): | 
					
						
							|  |  |  |             srv = await asyncio.start_server(inner, socket_helper.HOSTv4, 0) | 
					
						
							|  |  |  |             async with srv: | 
					
						
							|  |  |  |                 addr = srv.sockets[0].getsockname() | 
					
						
							|  |  |  |                 with socket.create_connection(addr): | 
					
						
							|  |  |  |                     # Give the loop some time to notice the connection | 
					
						
							|  |  |  |                     await fut | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         messages = [] | 
					
						
							|  |  |  |         self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         fut = self.loop.create_future() | 
					
						
							|  |  |  |         self.loop.run_until_complete(outer()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(messages, []) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-04 12:20:21 -08:00
										 |  |  |     def _basetest_unhandled_exceptions(self, handle_echo): | 
					
						
							| 
									
										
										
										
											2023-11-02 13:08:18 +05:30
										 |  |  |         port = socket_helper.find_unused_port() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         messages = [] | 
					
						
							|  |  |  |         self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         async def client(): | 
					
						
							|  |  |  |             rd, wr = await asyncio.open_connection('localhost', port) | 
					
						
							|  |  |  |             wr.write(b'test msg') | 
					
						
							|  |  |  |             await wr.drain() | 
					
						
							|  |  |  |             wr.close() | 
					
						
							|  |  |  |             await wr.wait_closed() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         async def main(): | 
					
						
							|  |  |  |             server = await asyncio.start_server( | 
					
						
							|  |  |  |                 handle_echo, 'localhost', port) | 
					
						
							|  |  |  |             await server.start_serving() | 
					
						
							|  |  |  |             await client() | 
					
						
							|  |  |  |             server.close() | 
					
						
							|  |  |  |             await server.wait_closed() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.loop.run_until_complete(main()) | 
					
						
							| 
									
										
										
										
											2024-01-04 12:20:21 -08:00
										 |  |  |         return messages | 
					
						
							| 
									
										
										
										
											2023-11-02 13:08:18 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-04 12:20:21 -08:00
										 |  |  |     def test_unhandled_exception(self): | 
					
						
							|  |  |  |         async def handle_echo(reader, writer): | 
					
						
							|  |  |  |             raise Exception('test') | 
					
						
							|  |  |  |         messages = self._basetest_unhandled_exceptions(handle_echo) | 
					
						
							| 
									
										
										
										
											2023-11-02 13:08:18 +05:30
										 |  |  |         self.assertEqual(messages[0]['message'], | 
					
						
							| 
									
										
										
										
											2024-01-04 12:20:21 -08:00
										 |  |  |                     'Unhandled exception in client_connected_cb') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_unhandled_cancel(self): | 
					
						
							|  |  |  |         async def handle_echo(reader, writer): | 
					
						
							| 
									
										
										
										
											2024-03-06 01:08:18 +03:00
										 |  |  |             writer.close() | 
					
						
							| 
									
										
										
										
											2024-01-04 12:20:21 -08:00
										 |  |  |             asyncio.current_task().cancel() | 
					
						
							|  |  |  |         messages = self._basetest_unhandled_exceptions(handle_echo) | 
					
						
							|  |  |  |         self.assertEqual(messages, []) | 
					
						
							| 
									
										
										
										
											2023-08-05 17:48:15 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-03 00:32:31 +01:00
										 |  |  |     def test_open_connection_happy_eyeball_refcycles(self): | 
					
						
							|  |  |  |         port = socket_helper.find_unused_port() | 
					
						
							|  |  |  |         async def main(): | 
					
						
							|  |  |  |             exc = None | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 await asyncio.open_connection( | 
					
						
							|  |  |  |                     host="localhost", | 
					
						
							|  |  |  |                     port=port, | 
					
						
							|  |  |  |                     happy_eyeballs_delay=0.25, | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             except* OSError as excs: | 
					
						
							|  |  |  |                 # can't use assertRaises because that clears frames | 
					
						
							|  |  |  |                 exc = excs.exceptions[0] | 
					
						
							|  |  |  |             self.assertIsNotNone(exc) | 
					
						
							| 
									
										
										
										
											2024-10-07 14:56:39 +01:00
										 |  |  |             self.assertListEqual(gc.get_referrers(exc), [main_coro]) | 
					
						
							|  |  |  |         main_coro = main() | 
					
						
							|  |  |  |         asyncio.run(main_coro) | 
					
						
							| 
									
										
										
										
											2024-10-03 00:32:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     unittest.main() |