Skip to content

Commit 3eac828

Browse files
Revert "bpo-47104: Rewrite asyncio.to_thread tests to use IsolatedAsyncioTestCase (pythonGH-32086)"
This reverts commit 9e1bfd8.
1 parent 0897a0b commit 3eac828

2 files changed

Lines changed: 47 additions & 20 deletions

File tree

‎Lib/test/test_asyncio/test_threads.py‎

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,87 @@
55

66
from contextvars import ContextVar
77
from unittest import mock
8+
from test.test_asyncio import utils as test_utils
89

910

1011
def tearDownModule():
1112
asyncio.set_event_loop_policy(None)
1213

1314

14-
class ToThreadTests(unittest.IsolatedAsyncioTestCase):
15-
async def test_to_thread(self):
16-
result = await asyncio.to_thread(sum, [40, 2])
15+
class ToThreadTests(test_utils.TestCase):
16+
def setUp(self):
17+
super().setUp()
18+
self.loop = asyncio.new_event_loop()
19+
asyncio.set_event_loop(self.loop)
20+
21+
def tearDown(self):
22+
self.loop.run_until_complete(
23+
self.loop.shutdown_default_executor())
24+
self.loop.close()
25+
asyncio.set_event_loop(None)
26+
self.loop = None
27+
super().tearDown()
28+
29+
def test_to_thread(self):
30+
async def main():
31+
return await asyncio.to_thread(sum, [40, 2])
32+
33+
result = self.loop.run_until_complete(main())
1734
self.assertEqual(result, 42)
1835

19-
async def test_to_thread_exception(self):
36+
def test_to_thread_exception(self):
2037
def raise_runtime():
2138
raise RuntimeError("test")
2239

23-
with self.assertRaisesRegex(RuntimeError, "test"):
40+
async def main():
2441
await asyncio.to_thread(raise_runtime)
2542

26-
async def test_to_thread_once(self):
43+
with self.assertRaisesRegex(RuntimeError, "test"):
44+
self.loop.run_until_complete(main())
45+
46+
def test_to_thread_once(self):
2747
func = mock.Mock()
2848

29-
await asyncio.to_thread(func)
49+
async def main():
50+
await asyncio.to_thread(func)
51+
52+
self.loop.run_until_complete(main())
3053
func.assert_called_once()
3154

32-
async def test_to_thread_concurrent(self):
55+
def test_to_thread_concurrent(self):
3356
func = mock.Mock()
3457

35-
futs = []
36-
for _ in range(10):
37-
fut = asyncio.to_thread(func)
38-
futs.append(fut)
39-
await asyncio.gather(*futs)
58+
async def main():
59+
futs = []
60+
for _ in range(10):
61+
fut = asyncio.to_thread(func)
62+
futs.append(fut)
63+
await asyncio.gather(*futs)
4064

65+
self.loop.run_until_complete(main())
4166
self.assertEqual(func.call_count, 10)
4267

43-
async def test_to_thread_args_kwargs(self):
68+
def test_to_thread_args_kwargs(self):
4469
# Unlike run_in_executor(), to_thread() should directly accept kwargs.
4570
func = mock.Mock()
4671

47-
await asyncio.to_thread(func, 'test', something=True)
72+
async def main():
73+
await asyncio.to_thread(func, 'test', something=True)
4874

75+
self.loop.run_until_complete(main())
4976
func.assert_called_once_with('test', something=True)
5077

51-
async def test_to_thread_contextvars(self):
78+
def test_to_thread_contextvars(self):
5279
test_ctx = ContextVar('test_ctx')
5380

5481
def get_ctx():
5582
return test_ctx.get()
5683

57-
test_ctx.set('parrot')
58-
result = await asyncio.to_thread(get_ctx)
84+
async def main():
85+
test_ctx.set('parrot')
86+
return await asyncio.to_thread(get_ctx)
5987

88+
result = self.loop.run_until_complete(main())
6089
self.assertEqual(result, 'parrot')
6190

6291

‎Misc/NEWS.d/next/Tests/2022-03-23-22-45-51.bpo-47104._esUq8.rst‎

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)