diff --git a/Lib/test/test_asyncio/test_pep492.py b/Lib/test/test_asyncio/test_pep492.py index dc25a46985e349..ecedf5010640ac 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']) @@ -170,7 +167,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(): @@ -185,9 +182,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) @@ -200,12 +197,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__':