2013-10-17 17:40:50 -03:00
|
|
|
"""Tests for streams.py."""
|
|
|
|
|
|
|
|
import gc
|
2014-05-12 14:04:37 -03:00
|
|
|
import os
|
2015-10-19 15:49:30 -03:00
|
|
|
import queue
|
2017-11-15 18:14:28 -04:00
|
|
|
import pickle
|
2014-02-18 13:15:06 -04:00
|
|
|
import socket
|
2014-05-12 14:04:37 -03:00
|
|
|
import sys
|
2015-10-19 15:49:30 -03:00
|
|
|
import threading
|
2013-10-17 17:40:50 -03:00
|
|
|
import unittest
|
2014-02-26 05:25:02 -04:00
|
|
|
from unittest import mock
|
2022-10-08 17:52:19 -03:00
|
|
|
import warnings
|
2020-04-25 04:06:29 -03:00
|
|
|
from test.support import socket_helper
|
2013-10-17 18:23:17 -03:00
|
|
|
try:
|
|
|
|
import ssl
|
|
|
|
except ImportError:
|
|
|
|
ssl = None
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
import asyncio
|
2017-12-11 11:04:40 -04:00
|
|
|
from test.test_asyncio import utils as test_utils
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
|
2018-06-02 00:34:09 -03:00
|
|
|
def tearDownModule():
|
|
|
|
asyncio.set_event_loop_policy(None)
|
|
|
|
|
|
|
|
|
2018-01-24 18:30:30 -04:00
|
|
|
class StreamTests(test_utils.TestCase):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
DATA = b'line1\nline2\nline3\n'
|
|
|
|
|
|
|
|
def setUp(self):
|
2016-11-04 15:29:28 -03:00
|
|
|
super().setUp()
|
2014-01-25 10:32:06 -04:00
|
|
|
self.loop = asyncio.new_event_loop()
|
2014-06-17 20:36:32 -03:00
|
|
|
self.set_event_loop(self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# just in case if we have transport close callbacks
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
|
|
|
|
self.loop.close()
|
|
|
|
gc.collect()
|
2014-06-17 20:36:32 -03:00
|
|
|
super().tearDown()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-02-18 13:15:06 -04:00
|
|
|
def _basetest_open_connection(self, open_connection_fut):
|
2018-09-12 15:43:04 -03:00
|
|
|
messages = []
|
|
|
|
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
|
2020-11-26 03:36:37 -04:00
|
|
|
reader, writer = self.loop.run_until_complete(open_connection_fut)
|
2014-02-18 13:15:06 -04: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)
|
|
|
|
self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
|
|
|
|
writer.close()
|
2018-09-12 15:43:04 -03:00
|
|
|
self.assertEqual(messages, [])
|
2014-02-18 13:15:06 -04:00
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_open_connection(self):
|
|
|
|
with test_utils.run_test_server() as httpd:
|
2020-11-26 03:36:37 -04:00
|
|
|
conn_fut = asyncio.open_connection(*httpd.address)
|
2014-02-18 13:15:06 -04:00
|
|
|
self._basetest_open_connection(conn_fut)
|
|
|
|
|
2020-04-25 04:06:29 -03:00
|
|
|
@socket_helper.skip_unless_bind_unix_socket
|
2014-02-18 13:15:06 -04:00
|
|
|
def test_open_unix_connection(self):
|
|
|
|
with test_utils.run_test_unix_server() as httpd:
|
2020-11-26 03:36:37 -04:00
|
|
|
conn_fut = asyncio.open_unix_connection(httpd.address)
|
2014-02-18 13:15:06 -04:00
|
|
|
self._basetest_open_connection(conn_fut)
|
|
|
|
|
|
|
|
def _basetest_open_connection_no_loop_ssl(self, open_connection_fut):
|
2018-09-12 15:43:04 -03:00
|
|
|
messages = []
|
|
|
|
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
|
2014-02-18 13:15:06 -04:00
|
|
|
try:
|
2020-11-26 03:36:37 -04:00
|
|
|
reader, writer = self.loop.run_until_complete(open_connection_fut)
|
2014-02-18 13:15:06 -04:00
|
|
|
finally:
|
|
|
|
asyncio.set_event_loop(None)
|
|
|
|
writer.write(b'GET / HTTP/1.0\r\n\r\n')
|
|
|
|
f = reader.read()
|
|
|
|
data = self.loop.run_until_complete(f)
|
|
|
|
self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
|
|
|
|
|
|
|
|
writer.close()
|
2018-09-12 15:43:04 -03:00
|
|
|
self.assertEqual(messages, [])
|
2013-10-17 17:40:50 -03: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 13:15:06 -04:00
|
|
|
conn_fut = asyncio.open_connection(
|
|
|
|
*httpd.address,
|
2020-11-26 03:36:37 -04:00
|
|
|
ssl=test_utils.dummy_ssl_context())
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-02-18 13:15:06 -04:00
|
|
|
self._basetest_open_connection_no_loop_ssl(conn_fut)
|
|
|
|
|
2020-04-25 04:06:29 -03:00
|
|
|
@socket_helper.skip_unless_bind_unix_socket
|
2014-02-18 13:15:06 -04: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 03:36:37 -04:00
|
|
|
)
|
2014-02-18 13:15:06 -04:00
|
|
|
|
|
|
|
self._basetest_open_connection_no_loop_ssl(conn_fut)
|
|
|
|
|
|
|
|
def _basetest_open_connection_error(self, open_connection_fut):
|
2018-09-12 15:43:04 -03:00
|
|
|
messages = []
|
|
|
|
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
|
2020-11-26 03:36:37 -04:00
|
|
|
reader, writer = self.loop.run_until_complete(open_connection_fut)
|
2014-02-18 13:15:06 -04: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 15:43:04 -03:00
|
|
|
self.assertEqual(messages, [])
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_open_connection_error(self):
|
|
|
|
with test_utils.run_test_server() as httpd:
|
2020-11-26 03:36:37 -04:00
|
|
|
conn_fut = asyncio.open_connection(*httpd.address)
|
2014-02-18 13:15:06 -04:00
|
|
|
self._basetest_open_connection_error(conn_fut)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2020-04-25 04:06:29 -03:00
|
|
|
@socket_helper.skip_unless_bind_unix_socket
|
2014-02-18 13:15:06 -04:00
|
|
|
def test_open_unix_connection_error(self):
|
|
|
|
with test_utils.run_test_unix_server() as httpd:
|
2020-11-26 03:36:37 -04:00
|
|
|
conn_fut = asyncio.open_unix_connection(httpd.address)
|
2014-02-18 13:15:06 -04:00
|
|
|
self._basetest_open_connection_error(conn_fut)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_feed_empty_data(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(b'')
|
2014-02-05 19:11:13 -04:00
|
|
|
self.assertEqual(b'', stream._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-02-05 19:11:13 -04:00
|
|
|
def test_feed_nonempty_data(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(self.DATA)
|
2014-02-05 19:11:13 -04:00
|
|
|
self.assertEqual(self.DATA, stream._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_read_zero(self):
|
|
|
|
# Read zero bytes.
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
|
|
|
stream.feed_data(self.DATA)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
data = self.loop.run_until_complete(stream.read(0))
|
|
|
|
self.assertEqual(b'', data)
|
2014-02-05 19:11:13 -04:00
|
|
|
self.assertEqual(self.DATA, stream._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_read(self):
|
|
|
|
# Read bytes.
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
2019-09-11 10:07:37 -03:00
|
|
|
read_task = self.loop.create_task(stream.read(30))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def cb():
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(self.DATA)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.call_soon(cb)
|
|
|
|
|
|
|
|
data = self.loop.run_until_complete(read_task)
|
|
|
|
self.assertEqual(self.DATA, data)
|
2014-02-05 19:11:13 -04:00
|
|
|
self.assertEqual(b'', stream._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_read_line_breaks(self):
|
|
|
|
# Read bytes without line breaks.
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
|
|
|
stream.feed_data(b'line1')
|
|
|
|
stream.feed_data(b'line2')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
data = self.loop.run_until_complete(stream.read(5))
|
|
|
|
|
|
|
|
self.assertEqual(b'line1', data)
|
2014-02-05 19:11:13 -04:00
|
|
|
self.assertEqual(b'line2', stream._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_read_eof(self):
|
|
|
|
# Read bytes, stop at eof.
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
2019-09-11 10:07:37 -03:00
|
|
|
read_task = self.loop.create_task(stream.read(1024))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def cb():
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_eof()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.call_soon(cb)
|
|
|
|
|
|
|
|
data = self.loop.run_until_complete(read_task)
|
|
|
|
self.assertEqual(b'', data)
|
2014-02-05 19:11:13 -04:00
|
|
|
self.assertEqual(b'', stream._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_read_until_eof(self):
|
|
|
|
# Read all bytes until eof.
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
2019-09-11 10:07:37 -03:00
|
|
|
read_task = self.loop.create_task(stream.read(-1))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def cb():
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(b'chunk1\n')
|
|
|
|
stream.feed_data(b'chunk2')
|
|
|
|
stream.feed_eof()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.call_soon(cb)
|
|
|
|
|
|
|
|
data = self.loop.run_until_complete(read_task)
|
|
|
|
|
|
|
|
self.assertEqual(b'chunk1\nchunk2', data)
|
2014-02-05 19:11:13 -04:00
|
|
|
self.assertEqual(b'', stream._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_read_exception(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
|
|
|
stream.feed_data(b'line\n')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
data = self.loop.run_until_complete(stream.read(2))
|
|
|
|
self.assertEqual(b'li', data)
|
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.set_exception(ValueError())
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertRaises(
|
|
|
|
ValueError, self.loop.run_until_complete, stream.read(2))
|
|
|
|
|
2016-01-11 13:28:19 -04:00
|
|
|
def test_invalid_limit(self):
|
|
|
|
with self.assertRaisesRegex(ValueError, 'imit'):
|
2019-09-30 01:59:55 -03:00
|
|
|
asyncio.StreamReader(limit=0, loop=self.loop)
|
2016-01-11 13:28:19 -04:00
|
|
|
|
|
|
|
with self.assertRaisesRegex(ValueError, 'imit'):
|
2019-09-30 01:59:55 -03:00
|
|
|
asyncio.StreamReader(limit=-1, loop=self.loop)
|
2016-01-11 13:28:19 -04:00
|
|
|
|
|
|
|
def test_read_limit(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(limit=3, loop=self.loop)
|
|
|
|
stream.feed_data(b'chunk')
|
2016-01-11 13:28:19 -04:00
|
|
|
data = self.loop.run_until_complete(stream.read(5))
|
|
|
|
self.assertEqual(b'chunk', data)
|
|
|
|
self.assertEqual(b'', stream._buffer)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_readline(self):
|
2014-02-05 19:11:13 -04:00
|
|
|
# Read one line. 'readline' will need to wait for the data
|
|
|
|
# to come from 'cb'
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
|
|
|
stream.feed_data(b'chunk1 ')
|
2019-09-11 10:07:37 -03:00
|
|
|
read_task = self.loop.create_task(stream.readline())
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def cb():
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(b'chunk2 ')
|
|
|
|
stream.feed_data(b'chunk3 ')
|
|
|
|
stream.feed_data(b'\n chunk4')
|
2013-10-17 17:40:50 -03: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 19:11:13 -04:00
|
|
|
self.assertEqual(b' chunk4', stream._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_readline_limit_with_existing_data(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
# Read one line. The data is in StreamReader's buffer
|
2014-02-05 19:11:13 -04:00
|
|
|
# before the event loop is run.
|
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(limit=3, loop=self.loop)
|
|
|
|
stream.feed_data(b'li')
|
|
|
|
stream.feed_data(b'ne1\nline2\n')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.assertRaises(
|
|
|
|
ValueError, self.loop.run_until_complete, stream.readline())
|
2014-02-05 19:11:13 -04:00
|
|
|
# The buffer should contain the remaining data after exception
|
|
|
|
self.assertEqual(b'line2\n', stream._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2019-09-30 01:59:55 -03: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 17:40:50 -03:00
|
|
|
|
|
|
|
self.assertRaises(
|
|
|
|
ValueError, self.loop.run_until_complete, stream.readline())
|
2014-02-05 19:11:13 -04: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 23:27:48 -04:00
|
|
|
# is more than 3, it will raise a ValueError. The buffer is
|
2014-02-05 19:11:13 -04:00
|
|
|
# expected to be empty now.
|
|
|
|
self.assertEqual(b'', stream._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-02-06 01:14:30 -04:00
|
|
|
def test_at_eof(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
2014-02-06 01:14:30 -04:00
|
|
|
self.assertFalse(stream.at_eof())
|
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(b'some data\n')
|
2014-02-06 01:14:30 -04:00
|
|
|
self.assertFalse(stream.at_eof())
|
|
|
|
|
|
|
|
self.loop.run_until_complete(stream.readline())
|
|
|
|
self.assertFalse(stream.at_eof())
|
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(b'some data\n')
|
|
|
|
stream.feed_eof()
|
2014-02-06 01:14:30 -04:00
|
|
|
self.loop.run_until_complete(stream.readline())
|
|
|
|
self.assertTrue(stream.at_eof())
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_readline_limit(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
# Read one line. StreamReaders are fed with data after
|
2014-02-05 19:11:13 -04:00
|
|
|
# their 'readline' methods are called.
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(limit=7, loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
def cb():
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(b'chunk1')
|
|
|
|
stream.feed_data(b'chunk2')
|
|
|
|
stream.feed_data(b'chunk3\n')
|
|
|
|
stream.feed_eof()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.call_soon(cb)
|
|
|
|
|
|
|
|
self.assertRaises(
|
|
|
|
ValueError, self.loop.run_until_complete, stream.readline())
|
2014-02-05 19:11:13 -04: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-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(limit=7, loop=self.loop)
|
2014-02-05 19:11:13 -04:00
|
|
|
def cb():
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(b'chunk1')
|
|
|
|
stream.feed_data(b'chunk2\n')
|
|
|
|
stream.feed_data(b'chunk3\n')
|
|
|
|
stream.feed_eof()
|
2014-02-05 19:11:13 -04: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 17:40:50 -03:00
|
|
|
|
2016-01-11 13:28:19 -04:00
|
|
|
# check strictness of the limit
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(limit=7, loop=self.loop)
|
|
|
|
stream.feed_data(b'1234567\n')
|
2016-01-11 13:28:19 -04:00
|
|
|
line = self.loop.run_until_complete(stream.readline())
|
|
|
|
self.assertEqual(b'1234567\n', line)
|
|
|
|
self.assertEqual(b'', stream._buffer)
|
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(b'12345678\n')
|
2016-01-11 13:28:19 -04:00
|
|
|
with self.assertRaises(ValueError) as cm:
|
|
|
|
self.loop.run_until_complete(stream.readline())
|
|
|
|
self.assertEqual(b'', stream._buffer)
|
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(b'12345678')
|
2016-01-11 13:28:19 -04:00
|
|
|
with self.assertRaises(ValueError) as cm:
|
|
|
|
self.loop.run_until_complete(stream.readline())
|
|
|
|
self.assertEqual(b'', stream._buffer)
|
|
|
|
|
2014-02-05 19:11:13 -04:00
|
|
|
def test_readline_nolimit_nowait(self):
|
|
|
|
# All needed data for the first 'readline' call will be
|
|
|
|
# in the buffer.
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
|
|
|
stream.feed_data(self.DATA[:6])
|
|
|
|
stream.feed_data(self.DATA[6:])
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
line = self.loop.run_until_complete(stream.readline())
|
|
|
|
|
|
|
|
self.assertEqual(b'line1\n', line)
|
2014-02-05 19:11:13 -04:00
|
|
|
self.assertEqual(b'line2\nline3\n', stream._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_readline_eof(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
|
|
|
stream.feed_data(b'some data')
|
|
|
|
stream.feed_eof()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
line = self.loop.run_until_complete(stream.readline())
|
|
|
|
self.assertEqual(b'some data', line)
|
|
|
|
|
|
|
|
def test_readline_empty_eof(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
|
|
|
stream.feed_eof()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
line = self.loop.run_until_complete(stream.readline())
|
|
|
|
self.assertEqual(b'', line)
|
|
|
|
|
|
|
|
def test_readline_read_byte_count(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
|
|
|
stream.feed_data(self.DATA)
|
2013-10-17 17:40:50 -03: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 19:11:13 -04:00
|
|
|
self.assertEqual(b'ine3\n', stream._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_readline_exception(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
|
|
|
stream.feed_data(b'line\n')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
data = self.loop.run_until_complete(stream.readline())
|
|
|
|
self.assertEqual(b'line\n', data)
|
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.set_exception(ValueError())
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertRaises(
|
|
|
|
ValueError, self.loop.run_until_complete, stream.readline())
|
2014-02-05 19:11:13 -04:00
|
|
|
self.assertEqual(b'', stream._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2016-01-11 13:28:19 -04:00
|
|
|
def test_readuntil_separator(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
2016-01-11 13:28:19 -04:00
|
|
|
with self.assertRaisesRegex(ValueError, 'Separator should be'):
|
|
|
|
self.loop.run_until_complete(stream.readuntil(separator=b''))
|
|
|
|
|
|
|
|
def test_readuntil_multi_chunks(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
2016-01-11 13:28:19 -04:00
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(b'lineAAA')
|
2016-01-11 13:28:19 -04:00
|
|
|
data = self.loop.run_until_complete(stream.readuntil(separator=b'AAA'))
|
|
|
|
self.assertEqual(b'lineAAA', data)
|
|
|
|
self.assertEqual(b'', stream._buffer)
|
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(b'lineAAA')
|
2016-01-11 13:28:19 -04:00
|
|
|
data = self.loop.run_until_complete(stream.readuntil(b'AAA'))
|
|
|
|
self.assertEqual(b'lineAAA', data)
|
|
|
|
self.assertEqual(b'', stream._buffer)
|
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(b'lineAAAxxx')
|
2016-01-11 13:28:19 -04: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-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
2016-01-11 13:28:19 -04:00
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(b'QWEaa')
|
|
|
|
stream.feed_data(b'XYaa')
|
|
|
|
stream.feed_data(b'a')
|
2016-01-11 13:28:19 -04:00
|
|
|
data = self.loop.run_until_complete(stream.readuntil(b'aaa'))
|
|
|
|
self.assertEqual(b'QWEaaXYaaa', data)
|
|
|
|
self.assertEqual(b'', stream._buffer)
|
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(b'QWEaa')
|
|
|
|
stream.feed_data(b'XYa')
|
|
|
|
stream.feed_data(b'aa')
|
2016-01-11 13:28:19 -04:00
|
|
|
data = self.loop.run_until_complete(stream.readuntil(b'aaa'))
|
|
|
|
self.assertEqual(b'QWEaaXYaaa', data)
|
|
|
|
self.assertEqual(b'', stream._buffer)
|
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(b'aaa')
|
2016-01-11 13:28:19 -04:00
|
|
|
data = self.loop.run_until_complete(stream.readuntil(b'aaa'))
|
|
|
|
self.assertEqual(b'aaa', data)
|
|
|
|
self.assertEqual(b'', stream._buffer)
|
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(b'Xaaa')
|
2016-01-11 13:28:19 -04:00
|
|
|
data = self.loop.run_until_complete(stream.readuntil(b'aaa'))
|
|
|
|
self.assertEqual(b'Xaaa', data)
|
|
|
|
self.assertEqual(b'', stream._buffer)
|
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(b'XXX')
|
|
|
|
stream.feed_data(b'a')
|
|
|
|
stream.feed_data(b'a')
|
|
|
|
stream.feed_data(b'a')
|
2016-01-11 13:28:19 -04: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-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
2020-11-28 10:27:28 -04:00
|
|
|
data = b'some dataAA'
|
|
|
|
stream.feed_data(data)
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_eof()
|
2016-01-11 13:28:19 -04:00
|
|
|
|
2020-11-28 10:27:28 -04:00
|
|
|
with self.assertRaisesRegex(asyncio.IncompleteReadError,
|
|
|
|
'undefined expected bytes') as cm:
|
2016-01-11 13:28:19 -04:00
|
|
|
self.loop.run_until_complete(stream.readuntil(b'AAA'))
|
2020-11-28 10:27:28 -04:00
|
|
|
self.assertEqual(cm.exception.partial, data)
|
2016-01-11 13:28:19 -04:00
|
|
|
self.assertIsNone(cm.exception.expected)
|
|
|
|
self.assertEqual(b'', stream._buffer)
|
|
|
|
|
|
|
|
def test_readuntil_limit_found_sep(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop, limit=3)
|
|
|
|
stream.feed_data(b'some dataAA')
|
2016-01-11 13:28:19 -04: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-30 01:59:55 -03:00
|
|
|
stream.feed_data(b'A')
|
2016-01-11 13:28:19 -04: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)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_readexactly_zero_or_less(self):
|
|
|
|
# Read exact number of bytes (zero or less).
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
|
|
|
stream.feed_data(self.DATA)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
data = self.loop.run_until_complete(stream.readexactly(0))
|
|
|
|
self.assertEqual(b'', data)
|
2014-02-05 19:11:13 -04:00
|
|
|
self.assertEqual(self.DATA, stream._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2015-12-16 20:40:03 -04:00
|
|
|
with self.assertRaisesRegex(ValueError, 'less than zero'):
|
2015-12-11 12:32:59 -04:00
|
|
|
self.loop.run_until_complete(stream.readexactly(-1))
|
2014-02-05 19:11:13 -04:00
|
|
|
self.assertEqual(self.DATA, stream._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_readexactly(self):
|
|
|
|
# Read exact number of bytes.
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
n = 2 * len(self.DATA)
|
2019-09-11 10:07:37 -03:00
|
|
|
read_task = self.loop.create_task(stream.readexactly(n))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def cb():
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(self.DATA)
|
|
|
|
stream.feed_data(self.DATA)
|
|
|
|
stream.feed_data(self.DATA)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.call_soon(cb)
|
|
|
|
|
|
|
|
data = self.loop.run_until_complete(read_task)
|
|
|
|
self.assertEqual(self.DATA + self.DATA, data)
|
2014-02-05 19:11:13 -04:00
|
|
|
self.assertEqual(self.DATA, stream._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2016-01-11 13:28:19 -04:00
|
|
|
def test_readexactly_limit(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(limit=3, loop=self.loop)
|
|
|
|
stream.feed_data(b'chunk')
|
2016-01-11 13:28:19 -04:00
|
|
|
data = self.loop.run_until_complete(stream.readexactly(5))
|
|
|
|
self.assertEqual(b'chunk', data)
|
|
|
|
self.assertEqual(b'', stream._buffer)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_readexactly_eof(self):
|
|
|
|
# Read exact number of bytes (eof).
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
n = 2 * len(self.DATA)
|
2019-09-11 10:07:37 -03:00
|
|
|
read_task = self.loop.create_task(stream.readexactly(n))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def cb():
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.feed_data(self.DATA)
|
|
|
|
stream.feed_eof()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.loop.call_soon(cb)
|
|
|
|
|
2014-01-25 10:32:06 -04: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 19:11:13 -04:00
|
|
|
self.assertEqual(b'', stream._buffer)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_readexactly_exception(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
|
|
|
stream.feed_data(b'line\n')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
data = self.loop.run_until_complete(stream.readexactly(2))
|
|
|
|
self.assertEqual(b'li', data)
|
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.set_exception(ValueError())
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertRaises(
|
|
|
|
ValueError, self.loop.run_until_complete, stream.readexactly(2))
|
|
|
|
|
|
|
|
def test_exception(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIsNone(stream.exception())
|
|
|
|
|
|
|
|
exc = ValueError()
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.set_exception(exc)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertIs(stream.exception(), exc)
|
|
|
|
|
|
|
|
def test_exception_waiter(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2019-05-16 11:52:10 -03:00
|
|
|
async def set_err():
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.set_exception(ValueError())
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2019-09-11 10:07:37 -03:00
|
|
|
t1 = self.loop.create_task(stream.readline())
|
|
|
|
t2 = self.loop.create_task(set_err())
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2018-10-02 14:53:06 -03:00
|
|
|
self.loop.run_until_complete(asyncio.wait([t1, t2]))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.assertRaises(ValueError, t1.result)
|
|
|
|
|
|
|
|
def test_exception_cancel(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2019-09-11 10:07:37 -03:00
|
|
|
t = self.loop.create_task(stream.readline())
|
2013-10-17 17:40:50 -03: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-30 01:59:55 -03:00
|
|
|
stream.set_exception(RuntimeError('message'))
|
2013-10-17 17:40:50 -03:00
|
|
|
test_utils.run_briefly(self.loop)
|
2013-10-18 19:17:11 -03:00
|
|
|
self.assertIs(stream._waiter, None)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-10-07 11:14:28 -03: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 15:43:38 -04:00
|
|
|
|
2017-12-08 18:23:48 -04:00
|
|
|
async def client(addr):
|
2020-11-26 03:36:37 -04:00
|
|
|
reader, writer = await asyncio.open_connection(*addr)
|
2013-11-19 15:43:38 -04:00
|
|
|
# send a line
|
|
|
|
writer.write(b"hello world!\n")
|
|
|
|
# read it back
|
2017-12-08 18:23:48 -04:00
|
|
|
msgback = await reader.readline()
|
2013-11-19 15:43:38 -04:00
|
|
|
writer.close()
|
2018-11-08 08:21:47 -04:00
|
|
|
await writer.wait_closed()
|
2013-11-19 15:43:38 -04:00
|
|
|
return msgback
|
|
|
|
|
2022-10-07 11:14:28 -03:00
|
|
|
messages = []
|
|
|
|
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
|
2013-11-19 15:43:38 -04:00
|
|
|
|
2022-10-07 11:14:28 -03: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 15:43:38 -04:00
|
|
|
|
2022-10-07 11:14:28 -03: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 15:43:04 -03:00
|
|
|
|
2020-04-25 04:06:29 -03:00
|
|
|
@socket_helper.skip_unless_bind_unix_socket
|
2022-10-07 11:14:28 -03: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 13:15:06 -04:00
|
|
|
|
2017-12-08 18:23:48 -04:00
|
|
|
async def client(path):
|
2020-11-26 03:36:37 -04:00
|
|
|
reader, writer = await asyncio.open_unix_connection(path)
|
2014-02-18 13:15:06 -04:00
|
|
|
# send a line
|
|
|
|
writer.write(b"hello world!\n")
|
|
|
|
# read it back
|
2017-12-08 18:23:48 -04:00
|
|
|
msgback = await reader.readline()
|
2014-02-18 13:15:06 -04:00
|
|
|
writer.close()
|
2018-11-08 08:21:47 -04:00
|
|
|
await writer.wait_closed()
|
2014-02-18 13:15:06 -04:00
|
|
|
return msgback
|
|
|
|
|
2022-10-07 11:14:28 -03: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 15:43:04 -03:00
|
|
|
|
2022-04-15 09:23:14 -03:00
|
|
|
@unittest.skipIf(ssl is None, 'No ssl module')
|
2022-10-07 11:14:28 -03: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 09: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 11:14:28 -03:00
|
|
|
messages = []
|
|
|
|
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
|
2022-10-04 14:56:47 -03:00
|
|
|
|
2022-10-07 11:14:28 -03:00
|
|
|
server = MyServer(self.loop)
|
|
|
|
addr = server.start()
|
|
|
|
msg1, msg2 = self.loop.run_until_complete(client(addr))
|
|
|
|
server.stop()
|
2022-10-04 14:56:47 -03:00
|
|
|
|
2022-10-07 11:14:28 -03:00
|
|
|
self.assertEqual(messages, [])
|
|
|
|
self.assertEqual(msg1, b"hello world 1!\n")
|
|
|
|
self.assertEqual(msg2, b"hello world 2!\n")
|
2022-10-04 14:56:47 -03:00
|
|
|
|
2014-05-12 14:04:37 -03:00
|
|
|
@unittest.skipIf(sys.platform == 'win32', "Don't have pipes")
|
|
|
|
def test_read_all_from_pipe_reader(self):
|
2015-07-09 18:13:50 -03:00
|
|
|
# See asyncio issue 168. This test is derived from the example
|
2014-05-12 14:04:37 -03:00
|
|
|
# subprocess_attach_read_pipe.py, but we configure the
|
2019-09-30 01:59:55 -03:00
|
|
|
# StreamReader's limit so that twice it is less than the size
|
2021-10-06 20:13:48 -03:00
|
|
|
# of the data writer. Also we must explicitly attach a child
|
2014-05-12 14:04:37 -03:00
|
|
|
# watcher to the event loop.
|
|
|
|
|
|
|
|
code = """\
|
|
|
|
import os, sys
|
|
|
|
fd = int(sys.argv[1])
|
|
|
|
os.write(fd, b'data')
|
|
|
|
os.close(fd)
|
|
|
|
"""
|
|
|
|
rfd, wfd = os.pipe()
|
|
|
|
args = [sys.executable, '-c', code, str(wfd)]
|
|
|
|
|
|
|
|
pipe = open(rfd, 'rb', 0)
|
2019-09-30 01:59:55 -03:00
|
|
|
reader = asyncio.StreamReader(loop=self.loop, limit=1)
|
|
|
|
protocol = asyncio.StreamReaderProtocol(reader, loop=self.loop)
|
2014-05-12 14:04:37 -03:00
|
|
|
transport, _ = self.loop.run_until_complete(
|
|
|
|
self.loop.connect_read_pipe(lambda: protocol, pipe))
|
2022-10-08 17:52:19 -03:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
watcher = asyncio.SafeChildWatcher()
|
2014-05-13 13:19:39 -03:00
|
|
|
watcher.attach_loop(self.loop)
|
|
|
|
try:
|
2022-10-15 20:09:30 -03:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
asyncio.set_child_watcher(watcher)
|
2019-09-12 09:40:40 -03:00
|
|
|
create = asyncio.create_subprocess_exec(
|
|
|
|
*args,
|
|
|
|
pass_fds={wfd},
|
|
|
|
)
|
2015-01-08 19:09:10 -04:00
|
|
|
proc = self.loop.run_until_complete(create)
|
2014-05-13 13:19:39 -03:00
|
|
|
self.loop.run_until_complete(proc.wait())
|
|
|
|
finally:
|
2022-10-15 20:09:30 -03:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
asyncio.set_child_watcher(None)
|
2014-05-12 14:04:37 -03:00
|
|
|
|
|
|
|
os.close(wfd)
|
2019-09-30 01:59:55 -03:00
|
|
|
data = self.loop.run_until_complete(reader.read(-1))
|
2014-05-12 14:04:37 -03:00
|
|
|
self.assertEqual(data, b'data')
|
|
|
|
|
2021-04-25 07:40:44 -03:00
|
|
|
def test_streamreader_constructor_without_loop(self):
|
2022-12-06 13:42:12 -04:00
|
|
|
with self.assertRaisesRegex(RuntimeError, 'no current event loop'):
|
|
|
|
asyncio.StreamReader()
|
2015-01-09 16:32:05 -04:00
|
|
|
|
2021-04-25 07:40:44 -03:00
|
|
|
def test_streamreader_constructor_use_running_loop(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
# asyncio issue #184: Ensure that StreamReaderProtocol constructor
|
2015-01-09 16:32:05 -04:00
|
|
|
# retrieves the current loop if the loop parameter is not set
|
2021-04-25 07:40:44 -03:00
|
|
|
async def test():
|
|
|
|
return asyncio.StreamReader()
|
|
|
|
|
|
|
|
reader = self.loop.run_until_complete(test())
|
2015-01-09 16:32:05 -04:00
|
|
|
self.assertIs(reader._loop, self.loop)
|
|
|
|
|
2021-04-25 07: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 13:42:12 -04:00
|
|
|
# Deprecated in 3.10, undeprecated in 3.12
|
2015-01-09 16:32:05 -04:00
|
|
|
self.addCleanup(asyncio.set_event_loop, None)
|
|
|
|
asyncio.set_event_loop(self.loop)
|
2022-12-06 13:42:12 -04:00
|
|
|
reader = asyncio.StreamReader()
|
2021-04-25 07:40:44 -03:00
|
|
|
self.assertIs(reader._loop, self.loop)
|
2015-01-09 16:32:05 -04:00
|
|
|
|
2021-04-25 07:40:44 -03:00
|
|
|
|
|
|
|
def test_streamreaderprotocol_constructor_without_loop(self):
|
|
|
|
reader = mock.Mock()
|
2022-12-06 13:42:12 -04:00
|
|
|
with self.assertRaisesRegex(RuntimeError, 'no current event loop'):
|
|
|
|
asyncio.StreamReaderProtocol(reader)
|
2021-04-25 07: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-30 01:59:55 -03:00
|
|
|
# asyncio issue #184: Ensure that StreamReaderProtocol constructor
|
2015-01-09 16:32:05 -04:00
|
|
|
# retrieves the current loop if the loop parameter is not set
|
2022-12-06 13:42:12 -04:00
|
|
|
# Deprecated in 3.10, undeprecated in 3.12
|
2021-04-25 07:40:44 -03:00
|
|
|
self.addCleanup(asyncio.set_event_loop, None)
|
|
|
|
asyncio.set_event_loop(self.loop)
|
2019-09-30 01:59:55 -03:00
|
|
|
reader = mock.Mock()
|
2022-12-06 13:42:12 -04:00
|
|
|
protocol = asyncio.StreamReaderProtocol(reader)
|
2015-01-09 16:32:05 -04:00
|
|
|
self.assertIs(protocol._loop, self.loop)
|
|
|
|
|
2022-08-29 15:31:11 -03:00
|
|
|
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-30 01:59:55 -03:00
|
|
|
def test_drain_raises(self):
|
2015-10-19 15:49:30 -03: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 13:09:44 -03:00
|
|
|
messages = []
|
|
|
|
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
|
2015-10-19 15:49:30 -03:00
|
|
|
q = queue.Queue()
|
|
|
|
|
|
|
|
def server():
|
|
|
|
# Runs in a separate thread.
|
2019-09-30 01:59:55 -03:00
|
|
|
with socket.create_server(('localhost', 0)) as sock:
|
2015-12-16 20:51:09 -04:00
|
|
|
addr = sock.getsockname()
|
|
|
|
q.put(addr)
|
|
|
|
clt, _ = sock.accept()
|
|
|
|
clt.close()
|
2015-10-19 16:00:04 -03:00
|
|
|
|
2017-12-08 18:23:48 -04:00
|
|
|
async def client(host, port):
|
2020-11-26 03:36:37 -04:00
|
|
|
reader, writer = await asyncio.open_connection(host, port)
|
2016-01-11 13:28:19 -04:00
|
|
|
|
2015-10-19 15:49:30 -03:00
|
|
|
while True:
|
|
|
|
writer.write(b"foo\n")
|
2017-12-08 18:23:48 -04:00
|
|
|
await writer.drain()
|
2015-10-19 15:49:30 -03:00
|
|
|
|
|
|
|
# Start the server thread and wait for it to be listening.
|
|
|
|
thread = threading.Thread(target=server)
|
2021-04-12 08:12:36 -03:00
|
|
|
thread.daemon = True
|
2015-10-19 15:49:30 -03:00
|
|
|
thread.start()
|
|
|
|
addr = q.get()
|
|
|
|
|
|
|
|
# Should not be stuck in an infinite loop.
|
2018-09-25 12:27:08 -03:00
|
|
|
with self.assertRaises((ConnectionResetError, ConnectionAbortedError,
|
|
|
|
BrokenPipeError)):
|
2015-10-19 15:49:30 -03: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 13:09:44 -03:00
|
|
|
self.assertEqual([], messages)
|
2015-10-19 15:49:30 -03:00
|
|
|
|
2015-09-29 12:36:00 -03:00
|
|
|
def test___repr__(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
|
|
|
self.assertEqual("<StreamReader>", repr(stream))
|
2015-09-29 12:36:00 -03:00
|
|
|
|
|
|
|
def test___repr__nondefault_limit(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop, limit=123)
|
|
|
|
self.assertEqual("<StreamReader limit=123>", repr(stream))
|
2015-09-29 12:36:00 -03:00
|
|
|
|
|
|
|
def test___repr__eof(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
|
|
|
stream.feed_eof()
|
|
|
|
self.assertEqual("<StreamReader eof>", repr(stream))
|
2015-09-29 12:36:00 -03:00
|
|
|
|
|
|
|
def test___repr__data(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
|
|
|
stream.feed_data(b'data')
|
|
|
|
self.assertEqual("<StreamReader 4 bytes>", repr(stream))
|
2015-09-29 12:36:00 -03:00
|
|
|
|
|
|
|
def test___repr__exception(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
2015-09-29 12:36:00 -03:00
|
|
|
exc = RuntimeError()
|
2019-09-30 01:59:55 -03:00
|
|
|
stream.set_exception(exc)
|
|
|
|
self.assertEqual("<StreamReader exception=RuntimeError()>",
|
2017-12-10 19:36:12 -04:00
|
|
|
repr(stream))
|
2015-09-29 12:36:00 -03:00
|
|
|
|
|
|
|
def test___repr__waiter(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
|
|
|
stream._waiter = asyncio.Future(loop=self.loop)
|
2015-09-29 12:36:00 -03:00
|
|
|
self.assertRegex(
|
|
|
|
repr(stream),
|
2019-09-30 01:59:55 -03:00
|
|
|
r"<StreamReader waiter=<Future pending[\S ]*>>")
|
2015-09-29 12:36:00 -03:00
|
|
|
stream._waiter.set_result(None)
|
|
|
|
self.loop.run_until_complete(stream._waiter)
|
|
|
|
stream._waiter = None
|
2019-09-30 01:59:55 -03:00
|
|
|
self.assertEqual("<StreamReader>", repr(stream))
|
2015-09-29 12:36:00 -03:00
|
|
|
|
|
|
|
def test___repr__transport(self):
|
2019-09-30 01:59:55 -03:00
|
|
|
stream = asyncio.StreamReader(loop=self.loop)
|
2015-09-29 12:36:00 -03:00
|
|
|
stream._transport = mock.Mock()
|
|
|
|
stream._transport.__repr__ = mock.Mock()
|
|
|
|
stream._transport.__repr__.return_value = "<Transport>"
|
2019-09-30 01:59:55 -03:00
|
|
|
self.assertEqual("<StreamReader transport=<Transport>>", repr(stream))
|
2015-09-29 12:36:00 -03:00
|
|
|
|
2017-11-15 18:14:28 -04: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 11:14:28 -03:00
|
|
|
def test_wait_closed_on_close(self):
|
2022-10-05 11:31:43 -03:00
|
|
|
with test_utils.run_test_server() as httpd:
|
2022-10-07 11:14:28 -03:00
|
|
|
rd, wr = self.loop.run_until_complete(
|
|
|
|
asyncio.open_connection(*httpd.address))
|
2018-01-24 18:30:30 -04:00
|
|
|
|
|
|
|
wr.write(b'GET / HTTP/1.0\r\n\r\n')
|
2022-10-07 11:14:28 -03:00
|
|
|
f = rd.readline()
|
|
|
|
data = self.loop.run_until_complete(f)
|
2018-01-24 18:30:30 -04:00
|
|
|
self.assertEqual(data, b'HTTP/1.0 200 OK\r\n')
|
2022-10-07 11:14:28 -03:00
|
|
|
f = rd.read()
|
|
|
|
data = self.loop.run_until_complete(f)
|
2018-01-24 18:30:30 -04:00
|
|
|
self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
|
|
|
|
self.assertFalse(wr.is_closing())
|
|
|
|
wr.close()
|
|
|
|
self.assertTrue(wr.is_closing())
|
2022-10-07 11:14:28 -03:00
|
|
|
self.loop.run_until_complete(wr.wait_closed())
|
2018-01-24 18:30:30 -04:00
|
|
|
|
2022-10-07 11:14:28 -03:00
|
|
|
def test_wait_closed_on_close_with_unread_data(self):
|
2019-05-27 16:56:22 -03:00
|
|
|
with test_utils.run_test_server() as httpd:
|
2022-10-07 11:14:28 -03:00
|
|
|
rd, wr = self.loop.run_until_complete(
|
|
|
|
asyncio.open_connection(*httpd.address))
|
2018-01-24 18:30:30 -04:00
|
|
|
|
|
|
|
wr.write(b'GET / HTTP/1.0\r\n\r\n')
|
2022-10-07 11:14:28 -03:00
|
|
|
f = rd.readline()
|
|
|
|
data = self.loop.run_until_complete(f)
|
2018-01-24 18:30:30 -04:00
|
|
|
self.assertEqual(data, b'HTTP/1.0 200 OK\r\n')
|
|
|
|
wr.close()
|
2022-10-07 11:14:28 -03:00
|
|
|
self.loop.run_until_complete(wr.wait_closed())
|
2018-01-24 18:30:30 -04:00
|
|
|
|
2022-10-07 11:14:28 -03:00
|
|
|
def test_async_writer_api(self):
|
2019-05-09 16:14:58 -03:00
|
|
|
async def inner(httpd):
|
2019-09-30 01:59:55 -03:00
|
|
|
rd, wr = await asyncio.open_connection(*httpd.address)
|
2019-05-09 16:14:58 -03:00
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
wr.write(b'GET / HTTP/1.0\r\n\r\n')
|
|
|
|
data = await rd.readline()
|
2019-05-09 16:14:58 -03:00
|
|
|
self.assertEqual(data, b'HTTP/1.0 200 OK\r\n')
|
2019-09-30 01:59:55 -03:00
|
|
|
data = await rd.read()
|
2019-05-09 16:14:58 -03:00
|
|
|
self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
|
2019-09-30 01:59:55 -03:00
|
|
|
wr.close()
|
|
|
|
await wr.wait_closed()
|
2019-05-09 16:14:58 -03:00
|
|
|
|
2022-10-07 11:14:28 -03:00
|
|
|
messages = []
|
|
|
|
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
|
|
|
|
|
2018-09-13 20:53:49 -03:00
|
|
|
with test_utils.run_test_server() as httpd:
|
2022-10-07 11:14:28 -03:00
|
|
|
self.loop.run_until_complete(inner(httpd))
|
2019-05-09 16:14:58 -03:00
|
|
|
|
2022-10-07 11:14:28 -03:00
|
|
|
self.assertEqual(messages, [])
|
|
|
|
|
|
|
|
def test_async_writer_api_exception_after_close(self):
|
2019-05-09 16:14:58 -03:00
|
|
|
async def inner(httpd):
|
2019-09-30 01:59:55 -03:00
|
|
|
rd, wr = await asyncio.open_connection(*httpd.address)
|
2019-05-09 16:14:58 -03:00
|
|
|
|
2019-09-30 01:59:55 -03:00
|
|
|
wr.write(b'GET / HTTP/1.0\r\n\r\n')
|
|
|
|
data = await rd.readline()
|
2018-09-13 20:53:49 -03:00
|
|
|
self.assertEqual(data, b'HTTP/1.0 200 OK\r\n')
|
2019-09-30 01:59:55 -03:00
|
|
|
data = await rd.read()
|
2018-09-13 20:53:49 -03:00
|
|
|
self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
|
2019-09-30 01:59:55 -03:00
|
|
|
wr.close()
|
2019-05-09 16:14:58 -03:00
|
|
|
with self.assertRaises(ConnectionResetError):
|
2019-09-30 01:59:55 -03:00
|
|
|
wr.write(b'data')
|
|
|
|
await wr.drain()
|
2019-05-09 16:14:58 -03:00
|
|
|
|
2022-10-07 11:14:28 -03:00
|
|
|
messages = []
|
|
|
|
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
|
|
|
|
|
2019-05-09 16:14:58 -03:00
|
|
|
with test_utils.run_test_server() as httpd:
|
2022-10-07 11:14:28 -03:00
|
|
|
self.loop.run_until_complete(inner(httpd))
|
2018-09-13 20:53:49 -03:00
|
|
|
|
2022-10-07 11:14:28 -03:00
|
|
|
self.assertEqual(messages, [])
|
|
|
|
|
|
|
|
def test_eof_feed_when_closing_writer(self):
|
2018-11-08 08:21:47 -04:00
|
|
|
# See http://bugs.python.org/issue35065
|
2022-10-07 11:14:28 -03:00
|
|
|
messages = []
|
|
|
|
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
|
|
|
|
|
2022-10-05 11:31:43 -03:00
|
|
|
with test_utils.run_test_server() as httpd:
|
2022-10-07 11:14:28 -03:00
|
|
|
rd, wr = self.loop.run_until_complete(
|
|
|
|
asyncio.open_connection(*httpd.address))
|
|
|
|
|
2019-05-27 16:56:22 -03:00
|
|
|
wr.close()
|
2022-10-07 11:14:28 -03:00
|
|
|
f = wr.wait_closed()
|
|
|
|
self.loop.run_until_complete(f)
|
2021-12-20 06:23:05 -04:00
|
|
|
self.assertTrue(rd.at_eof())
|
2022-10-07 11:14:28 -03:00
|
|
|
f = rd.read()
|
|
|
|
data = self.loop.run_until_complete(f)
|
2021-12-20 06:23:05 -04:00
|
|
|
self.assertEqual(data, b'')
|
2018-11-08 08:21:47 -04:00
|
|
|
|
2022-10-07 11:14:28 -03:00
|
|
|
self.assertEqual(messages, [])
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|