From eef418d50c9bdf4b15eb1e3f54f6935162e46b7a Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Fri, 22 Nov 2024 15:01:39 -0400 Subject: [PATCH] feat(test): test_create_task.py (#423) * feat(test): test_create_task.py * chore: `black .` --------- Co-authored-by: github-actions[bot] --- tests/{ => asyncio}/test_as_completed.py | 0 tests/asyncio/test_create_task.py | 64 ++++++++++++++++++++++++ tests/{ => asyncio}/test_gather.py | 0 3 files changed, 64 insertions(+) rename tests/{ => asyncio}/test_as_completed.py (100%) create mode 100644 tests/asyncio/test_create_task.py rename tests/{ => asyncio}/test_gather.py (100%) diff --git a/tests/test_as_completed.py b/tests/asyncio/test_as_completed.py similarity index 100% rename from tests/test_as_completed.py rename to tests/asyncio/test_as_completed.py diff --git a/tests/asyncio/test_create_task.py b/tests/asyncio/test_create_task.py new file mode 100644 index 00000000..cb7dbbc5 --- /dev/null +++ b/tests/asyncio/test_create_task.py @@ -0,0 +1,64 @@ +import logging +import pytest +import asyncio +from a_sync.asyncio.create_task import create_task + + +@pytest.mark.asyncio_cooperative +async def test_create_task_with_coroutine(): + async def sample_coroutine(): + return "Hello, World!" + + task = create_task(sample_coroutine()) + result = await task + assert result == "Hello, World!" + + +@pytest.mark.asyncio_cooperative +async def test_create_task_with_future(): + loop = asyncio.get_event_loop() + future = loop.create_future() + future.set_result("Future Result") + + task = create_task(future) + result = await task + assert result == "Future Result" + + +@pytest.mark.asyncio_cooperative +async def test_create_task_with_name(): + async def sample_coroutine(): + return "Named Task" + + task = create_task(sample_coroutine(), name="TestTask") + assert task.get_name() == "TestTask" + + +@pytest.mark.asyncio_cooperative +async def test_create_task_skip_gc_until_done(): + async def sample_coroutine(): + return "GC Test" + + task = create_task(sample_coroutine(), skip_gc_until_done=True) + result = await task + assert result == "GC Test" + + +@pytest.mark.asyncio_cooperative +async def test_create_task_log_destroy_pending(): + async def sample_coroutine(): + return "Log Test" + + task = create_task(sample_coroutine(), log_destroy_pending=False) + assert not task._log_destroy_pending + + +@pytest.mark.asyncio_cooperative +async def test_create_task_handles_non_coroutine_awaitable(): + class CustomAwaitable: + def __await__(self): + yield + return "Custom Awaitable Result" + + result = await create_task(CustomAwaitable()) + assert result == "Custom Awaitable Result" diff --git a/tests/test_gather.py b/tests/asyncio/test_gather.py similarity index 100% rename from tests/test_gather.py rename to tests/asyncio/test_gather.py