Skip to content

Commit 9fbc1d8

Browse files
kumaraditya303gvanrossum
authored andcommitted
add two tests
1 parent 1d2d081 commit 9fbc1d8

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Lib/test/test_asyncio/test_futures2.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# IsolatedAsyncioTestCase based tests
22
import asyncio
3+
import contextvars
34
import traceback
45
import unittest
56
from asyncio import tasks
@@ -27,6 +28,25 @@ async def raise_exc():
2728
else:
2829
self.fail('TypeError was not raised')
2930

31+
async def test_task_exc_handler_correct_context(self):
32+
# see https://github.com/python/cpython/issues/96704
33+
name = contextvars.ContextVar('name', default='foo')
34+
exc_handler_called = False
35+
def exc_handler(*args):
36+
self.assertEqual(name.get(), 'bar')
37+
nonlocal exc_handler_called
38+
exc_handler_called = True
39+
40+
async def task():
41+
name.set('bar')
42+
1/0
43+
44+
loop = asyncio.get_running_loop()
45+
loop.set_exception_handler(exc_handler)
46+
self.cls(task())
47+
await asyncio.sleep(0)
48+
self.assertTrue(exc_handler_called)
49+
3050
@unittest.skipUnless(hasattr(tasks, '_CTask'),
3151
'requires the C _asyncio module')
3252
class CFutureTests(FutureTests, unittest.IsolatedAsyncioTestCase):

Lib/test/test_asyncio/test_tasks.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2482,6 +2482,17 @@ def test_get_coro(self):
24822482
finally:
24832483
loop.close()
24842484

2485+
def test_get_context(self):
2486+
loop = asyncio.new_event_loop()
2487+
coro = coroutine_function()
2488+
context = contextvars.copy_context()
2489+
try:
2490+
task = self.new_task(loop, coro, context=context)
2491+
loop.run_until_complete(task)
2492+
self.assertIs(task.get_context(), context)
2493+
finally:
2494+
loop.close()
2495+
24852496

24862497
def add_subclass_tests(cls):
24872498
BaseTask = cls.Task

0 commit comments

Comments
 (0)