Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add two tests
  • Loading branch information
kumaraditya303 authored and gvanrossum committed Oct 4, 2022
commit 9fbc1d8f4bc16c0da6b92e831a8b454bc5d34230
20 changes: 20 additions & 0 deletions Lib/test/test_asyncio/test_futures2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# IsolatedAsyncioTestCase based tests
import asyncio
import contextvars
import traceback
import unittest
from asyncio import tasks
Expand Down Expand Up @@ -27,6 +28,25 @@ async def raise_exc():
else:
self.fail('TypeError was not raised')

async def test_task_exc_handler_correct_context(self):
# see https://github.com/python/cpython/issues/96704
name = contextvars.ContextVar('name', default='foo')
exc_handler_called = False
def exc_handler(*args):
self.assertEqual(name.get(), 'bar')
nonlocal exc_handler_called
exc_handler_called = True

async def task():
name.set('bar')
1/0

loop = asyncio.get_running_loop()
loop.set_exception_handler(exc_handler)
self.cls(task())
await asyncio.sleep(0)
self.assertTrue(exc_handler_called)

@unittest.skipUnless(hasattr(tasks, '_CTask'),
'requires the C _asyncio module')
class CFutureTests(FutureTests, unittest.IsolatedAsyncioTestCase):
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2482,6 +2482,17 @@ def test_get_coro(self):
finally:
loop.close()

def test_get_context(self):
loop = asyncio.new_event_loop()
coro = coroutine_function()
context = contextvars.copy_context()
try:
task = self.new_task(loop, coro, context=context)
loop.run_until_complete(task)
self.assertIs(task.get_context(), context)
finally:
loop.close()


def add_subclass_tests(cls):
BaseTask = cls.Task
Expand Down