gh-128307: Support eager_start=<bool> in create_eager_task_factory and various create_task functions (#128306)

Some create_task() functions were changed from `name=None, context=None` to `**kwargs`.

Co-authored-by: Guido van Rossum <gvanrossum@gmail.com>
This commit is contained in:
Thomas Grainger 2025-05-05 05:58:07 +01:00 committed by GitHub
parent c4cc5d58ae
commit 08d7687094
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 85 additions and 19 deletions

View file

@ -89,8 +89,8 @@ class BaseTaskTests:
Future = None
all_tasks = None
def new_task(self, loop, coro, name='TestTask', context=None):
return self.__class__.Task(coro, loop=loop, name=name, context=context)
def new_task(self, loop, coro, name='TestTask', context=None, eager_start=None):
return self.__class__.Task(coro, loop=loop, name=name, context=context, eager_start=eager_start)
def new_future(self, loop):
return self.__class__.Future(loop=loop)
@ -2686,6 +2686,35 @@ class BaseTaskTests:
self.assertEqual([None, 1, 2], ret)
def test_eager_start_true(self):
name = None
async def asyncfn():
nonlocal name
name = self.current_task().get_name()
async def main():
t = self.new_task(coro=asyncfn(), loop=asyncio.get_running_loop(), eager_start=True, name="example")
self.assertTrue(t.done())
self.assertEqual(name, "example")
await t
def test_eager_start_false(self):
name = None
async def asyncfn():
nonlocal name
name = self.current_task().get_name()
async def main():
t = self.new_task(coro=asyncfn(), loop=asyncio.get_running_loop(), eager_start=False, name="example")
self.assertFalse(t.done())
self.assertIsNone(name)
await t
self.assertEqual(name, "example")
asyncio.run(main(), loop_factory=asyncio.EventLoop)
def test_get_coro(self):
loop = asyncio.new_event_loop()
coro = coroutine_function()