mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
bpo-47104: Rewrite asyncio.to_thread tests to use IsolatedAsyncioTestCase (GH-32086)
This commit is contained in:
parent
48e2010d92
commit
ff619c7dfe
2 changed files with 20 additions and 47 deletions
|
@ -5,87 +5,58 @@ import unittest
|
||||||
|
|
||||||
from contextvars import ContextVar
|
from contextvars import ContextVar
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
from test.test_asyncio import utils as test_utils
|
|
||||||
|
|
||||||
|
|
||||||
def tearDownModule():
|
def tearDownModule():
|
||||||
asyncio.set_event_loop_policy(None)
|
asyncio.set_event_loop_policy(None)
|
||||||
|
|
||||||
|
|
||||||
class ToThreadTests(test_utils.TestCase):
|
class ToThreadTests(unittest.IsolatedAsyncioTestCase):
|
||||||
def setUp(self):
|
async def test_to_thread(self):
|
||||||
super().setUp()
|
result = await asyncio.to_thread(sum, [40, 2])
|
||||||
self.loop = asyncio.new_event_loop()
|
|
||||||
asyncio.set_event_loop(self.loop)
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
self.loop.run_until_complete(
|
|
||||||
self.loop.shutdown_default_executor())
|
|
||||||
self.loop.close()
|
|
||||||
asyncio.set_event_loop(None)
|
|
||||||
self.loop = None
|
|
||||||
super().tearDown()
|
|
||||||
|
|
||||||
def test_to_thread(self):
|
|
||||||
async def main():
|
|
||||||
return await asyncio.to_thread(sum, [40, 2])
|
|
||||||
|
|
||||||
result = self.loop.run_until_complete(main())
|
|
||||||
self.assertEqual(result, 42)
|
self.assertEqual(result, 42)
|
||||||
|
|
||||||
def test_to_thread_exception(self):
|
async def test_to_thread_exception(self):
|
||||||
def raise_runtime():
|
def raise_runtime():
|
||||||
raise RuntimeError("test")
|
raise RuntimeError("test")
|
||||||
|
|
||||||
async def main():
|
with self.assertRaisesRegex(RuntimeError, "test"):
|
||||||
await asyncio.to_thread(raise_runtime)
|
await asyncio.to_thread(raise_runtime)
|
||||||
|
|
||||||
with self.assertRaisesRegex(RuntimeError, "test"):
|
async def test_to_thread_once(self):
|
||||||
self.loop.run_until_complete(main())
|
|
||||||
|
|
||||||
def test_to_thread_once(self):
|
|
||||||
func = mock.Mock()
|
func = mock.Mock()
|
||||||
|
|
||||||
async def main():
|
await asyncio.to_thread(func)
|
||||||
await asyncio.to_thread(func)
|
|
||||||
|
|
||||||
self.loop.run_until_complete(main())
|
|
||||||
func.assert_called_once()
|
func.assert_called_once()
|
||||||
|
|
||||||
def test_to_thread_concurrent(self):
|
async def test_to_thread_concurrent(self):
|
||||||
func = mock.Mock()
|
func = mock.Mock()
|
||||||
|
|
||||||
async def main():
|
futs = []
|
||||||
futs = []
|
for _ in range(10):
|
||||||
for _ in range(10):
|
fut = asyncio.to_thread(func)
|
||||||
fut = asyncio.to_thread(func)
|
futs.append(fut)
|
||||||
futs.append(fut)
|
await asyncio.gather(*futs)
|
||||||
await asyncio.gather(*futs)
|
|
||||||
|
|
||||||
self.loop.run_until_complete(main())
|
|
||||||
self.assertEqual(func.call_count, 10)
|
self.assertEqual(func.call_count, 10)
|
||||||
|
|
||||||
def test_to_thread_args_kwargs(self):
|
async def test_to_thread_args_kwargs(self):
|
||||||
# Unlike run_in_executor(), to_thread() should directly accept kwargs.
|
# Unlike run_in_executor(), to_thread() should directly accept kwargs.
|
||||||
func = mock.Mock()
|
func = mock.Mock()
|
||||||
|
|
||||||
async def main():
|
await asyncio.to_thread(func, 'test', something=True)
|
||||||
await asyncio.to_thread(func, 'test', something=True)
|
|
||||||
|
|
||||||
self.loop.run_until_complete(main())
|
|
||||||
func.assert_called_once_with('test', something=True)
|
func.assert_called_once_with('test', something=True)
|
||||||
|
|
||||||
def test_to_thread_contextvars(self):
|
async def test_to_thread_contextvars(self):
|
||||||
test_ctx = ContextVar('test_ctx')
|
test_ctx = ContextVar('test_ctx')
|
||||||
|
|
||||||
def get_ctx():
|
def get_ctx():
|
||||||
return test_ctx.get()
|
return test_ctx.get()
|
||||||
|
|
||||||
async def main():
|
test_ctx.set('parrot')
|
||||||
test_ctx.set('parrot')
|
result = await asyncio.to_thread(get_ctx)
|
||||||
return await asyncio.to_thread(get_ctx)
|
|
||||||
|
|
||||||
result = self.loop.run_until_complete(main())
|
|
||||||
self.assertEqual(result, 'parrot')
|
self.assertEqual(result, 'parrot')
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Rewrite :func:`asyncio.to_thread` tests to use
|
||||||
|
:class:`unittest.IsolatedAsyncioTestCase`.
|
Loading…
Add table
Add a link
Reference in a new issue