|
5 | 5 |
|
6 | 6 | from contextvars import ContextVar |
7 | 7 | from unittest import mock |
| 8 | +from test.test_asyncio import utils as test_utils |
8 | 9 |
|
9 | 10 |
|
10 | 11 | def tearDownModule(): |
11 | 12 | asyncio.set_event_loop_policy(None) |
12 | 13 |
|
13 | 14 |
|
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()) |
17 | 34 | self.assertEqual(result, 42) |
18 | 35 |
|
19 | | - async def test_to_thread_exception(self): |
| 36 | + def test_to_thread_exception(self): |
20 | 37 | def raise_runtime(): |
21 | 38 | raise RuntimeError("test") |
22 | 39 |
|
23 | | - with self.assertRaisesRegex(RuntimeError, "test"): |
| 40 | + async def main(): |
24 | 41 | await asyncio.to_thread(raise_runtime) |
25 | 42 |
|
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): |
27 | 47 | func = mock.Mock() |
28 | 48 |
|
29 | | - await asyncio.to_thread(func) |
| 49 | + async def main(): |
| 50 | + await asyncio.to_thread(func) |
| 51 | + |
| 52 | + self.loop.run_until_complete(main()) |
30 | 53 | func.assert_called_once() |
31 | 54 |
|
32 | | - async def test_to_thread_concurrent(self): |
| 55 | + def test_to_thread_concurrent(self): |
33 | 56 | func = mock.Mock() |
34 | 57 |
|
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) |
40 | 64 |
|
| 65 | + self.loop.run_until_complete(main()) |
41 | 66 | self.assertEqual(func.call_count, 10) |
42 | 67 |
|
43 | | - async def test_to_thread_args_kwargs(self): |
| 68 | + def test_to_thread_args_kwargs(self): |
44 | 69 | # Unlike run_in_executor(), to_thread() should directly accept kwargs. |
45 | 70 | func = mock.Mock() |
46 | 71 |
|
47 | | - await asyncio.to_thread(func, 'test', something=True) |
| 72 | + async def main(): |
| 73 | + await asyncio.to_thread(func, 'test', something=True) |
48 | 74 |
|
| 75 | + self.loop.run_until_complete(main()) |
49 | 76 | func.assert_called_once_with('test', something=True) |
50 | 77 |
|
51 | | - async def test_to_thread_contextvars(self): |
| 78 | + def test_to_thread_contextvars(self): |
52 | 79 | test_ctx = ContextVar('test_ctx') |
53 | 80 |
|
54 | 81 | def get_ctx(): |
55 | 82 | return test_ctx.get() |
56 | 83 |
|
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) |
59 | 87 |
|
| 88 | + result = self.loop.run_until_complete(main()) |
60 | 89 | self.assertEqual(result, 'parrot') |
61 | 90 |
|
62 | 91 |
|
|
0 commit comments