From 79505dffe223bd32f6076b93ad2f229e7960a235 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Wed, 5 Oct 2022 09:37:50 +0400 Subject: [PATCH 1/6] Port test cases to IsolatedAsyncioTestCase, part 2 --- Lib/test/test_asyncio/test_streams.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index d1f8aef4bb9cbd..78febe70356b75 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -941,6 +941,7 @@ def test_LimitOverrunError_pickleable(self): self.assertEqual(str(e), str(e2)) self.assertEqual(e.consumed, e2.consumed) +class NewStreamTests2(unittest.IsolatedAsyncioTestCase): async def test_wait_closed_on_close(self): async with test_utils.run_test_server() as httpd: rd, wr = self.loop.run_until_complete( @@ -956,19 +957,18 @@ async def test_wait_closed_on_close(self): self.assertTrue(wr.is_closing()) await wr.wait_closed() - def test_wait_closed_on_close_with_unread_data(self): + async def test_wait_closed_on_close_with_unread_data(self): with test_utils.run_test_server() as httpd: - rd, wr = self.loop.run_until_complete( - asyncio.open_connection(*httpd.address)) + rd, wr = await asyncio.open_connection(*httpd.address) wr.write(b'GET / HTTP/1.0\r\n\r\n') f = rd.readline() data = self.loop.run_until_complete(f) self.assertEqual(data, b'HTTP/1.0 200 OK\r\n') wr.close() - self.loop.run_until_complete(wr.wait_closed()) + await wr.wait_closed() - def test_async_writer_api(self): + async def test_async_writer_api(self): async def inner(httpd): rd, wr = await asyncio.open_connection(*httpd.address) @@ -984,11 +984,11 @@ async def inner(httpd): 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)) + await inner(httpd) self.assertEqual(messages, []) - def test_async_writer_api_exception_after_close(self): + async def test_async_writer_api_exception_after_close(self): async def inner(httpd): rd, wr = await asyncio.open_connection(*httpd.address) @@ -1006,7 +1006,7 @@ async def inner(httpd): 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)) + await inner(httpd) self.assertEqual(messages, []) From a46c2227b1b7d44ff776f8c79c5241a9c9c803ae Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Wed, 5 Oct 2022 10:22:40 +0400 Subject: [PATCH 2/6] Remove self.loop.set_exception_handler --- Lib/test/test_asyncio/test_streams.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index 78febe70356b75..07d7f337bdaa02 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -980,14 +980,9 @@ async def inner(httpd): wr.close() await wr.wait_closed() - messages = [] - self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) - with test_utils.run_test_server() as httpd: await inner(httpd) - self.assertEqual(messages, []) - async def test_async_writer_api_exception_after_close(self): async def inner(httpd): rd, wr = await asyncio.open_connection(*httpd.address) From ee7e96626fa8615899d81e98492f6d3071d9194d Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Wed, 5 Oct 2022 10:25:40 +0400 Subject: [PATCH 3/6] Another fix --- Lib/test/test_asyncio/test_streams.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index 07d7f337bdaa02..bd953d22424254 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -997,14 +997,9 @@ async def inner(httpd): wr.write(b'data') await wr.drain() - messages = [] - self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) - with test_utils.run_test_server() as httpd: await inner(httpd) - self.assertEqual(messages, []) - async def test_eof_feed_when_closing_writer(self): # See http://bugs.python.org/issue35065 async with test_utils.run_test_server() as httpd: From bc21d76edce3de151c68c166035d277c56215560 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Wed, 5 Oct 2022 10:57:06 +0400 Subject: [PATCH 4/6] Fix a misuse of async with --- Lib/test/test_asyncio/test_streams.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index bd953d22424254..ce386550586364 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -943,7 +943,7 @@ def test_LimitOverrunError_pickleable(self): class NewStreamTests2(unittest.IsolatedAsyncioTestCase): async def test_wait_closed_on_close(self): - async with test_utils.run_test_server() as httpd: + with test_utils.run_test_server() as httpd: rd, wr = self.loop.run_until_complete( asyncio.open_connection(*httpd.address)) @@ -1002,7 +1002,7 @@ async def inner(httpd): async def test_eof_feed_when_closing_writer(self): # See http://bugs.python.org/issue35065 - async with test_utils.run_test_server() as httpd: + with test_utils.run_test_server() as httpd: rd, wr = await asyncio.open_connection(*httpd.address) wr.close() f = wr.wait_closed() From 87e841c59a12625f5ba531f71b71a2bda195ff0a Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Wed, 5 Oct 2022 11:17:45 +0400 Subject: [PATCH 5/6] Remove more asyncio.open_connection() --- Lib/test/test_asyncio/test_streams.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index ce386550586364..abdd564fc00f62 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -944,8 +944,7 @@ def test_LimitOverrunError_pickleable(self): class NewStreamTests2(unittest.IsolatedAsyncioTestCase): async def test_wait_closed_on_close(self): with test_utils.run_test_server() as httpd: - rd, wr = self.loop.run_until_complete( - asyncio.open_connection(*httpd.address)) + rd, wr = await asyncio.open_connection(*httpd.address) wr.write(b'GET / HTTP/1.0\r\n\r\n') data = await rd.readline() @@ -962,8 +961,7 @@ async def test_wait_closed_on_close_with_unread_data(self): rd, wr = await asyncio.open_connection(*httpd.address) wr.write(b'GET / HTTP/1.0\r\n\r\n') - f = rd.readline() - data = self.loop.run_until_complete(f) + data = await rd.readline() self.assertEqual(data, b'HTTP/1.0 200 OK\r\n') wr.close() await wr.wait_closed() @@ -1005,11 +1003,9 @@ async def test_eof_feed_when_closing_writer(self): with test_utils.run_test_server() as httpd: rd, wr = await asyncio.open_connection(*httpd.address) wr.close() - f = wr.wait_closed() - self.loop.run_until_complete(f) + await wr.wait_closed() self.assertTrue(rd.at_eof()) - f = rd.read() - data = self.loop.run_until_complete(f) + data = await rd.read() self.assertEqual(data, b'') From 7b8cacafd255eb5be172a4df355868f835030147 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Wed, 5 Oct 2022 12:01:28 +0400 Subject: [PATCH 6/6] Return an assignment to data --- Lib/test/test_asyncio/test_streams.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index abdd564fc00f62..61d5e984dfbfbb 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -949,7 +949,7 @@ async def test_wait_closed_on_close(self): 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') - await rd.read() + data = await rd.read() self.assertTrue(data.endswith(b'\r\n\r\nTest message')) self.assertFalse(wr.is_closing()) wr.close()