From 8a0d65fd1f9586d30d59fe0b1e1617542a0918b1 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Thu, 6 Oct 2022 10:14:50 +0400 Subject: [PATCH] Port test_pep492 to IsolatedAsyncioTestCase --- Lib/test/test_asyncio/test_pep492.py | 37 +++++++++++++--------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/Lib/test/test_asyncio/test_pep492.py b/Lib/test/test_asyncio/test_pep492.py index f833f788dcb98f..3889cfc9864a17 100644 --- a/Lib/test/test_asyncio/test_pep492.py +++ b/Lib/test/test_asyncio/test_pep492.py @@ -7,7 +7,6 @@ from unittest import mock import asyncio -from test.test_asyncio import utils as test_utils def tearDownModule(): @@ -29,20 +28,18 @@ def __await__(self): yield -class BaseTest(test_utils.TestCase): +class BaseTest(unittest.IsolatedAsyncioTestCase): - def setUp(self): - super().setUp() - self.loop = asyncio.BaseEventLoop() - self.loop._process_events = mock.Mock() - self.loop._selector = mock.Mock() - self.loop._selector.select.return_value = () - self.set_event_loop(self.loop) + async def asyncSetUp(self): + loop = asyncio.get_running_loop() + loop._process_events = mock.Mock() + loop._selector = mock.Mock() + loop._selector.select.return_value = () class LockTests(BaseTest): - def test_context_manager_async_with(self): + async def test_context_manager_async_with(self): primitives = [ asyncio.Lock(), asyncio.Condition(), @@ -61,10 +58,10 @@ async def test(lock): self.assertFalse(lock.locked()) for primitive in primitives: - self.loop.run_until_complete(test(primitive)) + await test(primitive) self.assertFalse(primitive.locked()) - def test_context_manager_with_await(self): + async def test_context_manager_with_await(self): primitives = [ asyncio.Lock(), asyncio.Condition(), @@ -83,13 +80,13 @@ async def test(lock): pass for primitive in primitives: - self.loop.run_until_complete(test(primitive)) + await test(primitive) self.assertFalse(primitive.locked()) class StreamReaderTests(BaseTest): - def test_readline(self): + async def test_readline(self): DATA = b'line1\nline2\nline3' stream = asyncio.StreamReader(loop=self.loop) @@ -102,7 +99,7 @@ async def reader(): data.append(line) return data - data = self.loop.run_until_complete(reader()) + data = await reader() self.assertEqual(data, [b'line1\n', b'line2\n', b'line3']) @@ -164,7 +161,7 @@ async def coro(): data = self.loop.run_until_complete(coro()) self.assertEqual(data, 'spam') - def test_task_print_stack(self): + async def test_task_print_stack(self): T = None async def foo(): @@ -179,9 +176,9 @@ async def runner(): T = asyncio.ensure_future(foo(), loop=self.loop) await T - self.loop.run_until_complete(runner()) + await runner() - def test_double_await(self): + async def test_double_await(self): async def afunc(): await asyncio.sleep(0.1) @@ -194,12 +191,12 @@ async def runner(): finally: t.cancel() - self.loop.set_debug(True) + asyncio.get_running_loop().set_debug(True) with self.assertRaises( RuntimeError, msg='coroutine is being awaited already'): - self.loop.run_until_complete(runner()) + await runner() if __name__ == '__main__':