Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix 3.11 testcase #15

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions later/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ async def coro() -> None:
try:
await asyncio.sleep(300)
except asyncio.CancelledError:
pass
await asyncio.sleep(2) # take longer than 2 seconds to cancel
await asyncio.sleep(2)
raise

loop = asyncio.get_running_loop()
task = loop.create_task(coro())
Expand All @@ -206,7 +206,7 @@ async def coro() -> None:
watcher.watch(task)
loop.call_later(0.2, watcher.cancel)
# insure the task isn't still pending so we don't fail the later TestCase checks
task.cancel()
await later.cancel(task)

async def test_watcher_cancel(self) -> None:
loop = asyncio.get_running_loop()
Expand Down
26 changes: 16 additions & 10 deletions later/unittest/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
from __future__ import annotations

import asyncio
import asyncio.base_tasks
import asyncio.coroutines
import asyncio.futures
import asyncio.log
import asyncio.tasks
import reprlib
import sys
import unittest.mock as mock
from functools import wraps
Expand Down Expand Up @@ -50,9 +52,10 @@ def __init__(self, coro, *args, **kws) -> None:
self._coro_repr = asyncio.coroutines._format_coroutine(coro)
super().__init__(coro, *args, **kws)

@reprlib.recursive_repr()
def __repr__(self) -> str:
# pyre-fixme[16]: `TestTask` has no attribute `_repr_info`.
repr_info = self._repr_info()
repr_info = asyncio.base_tasks._task_repr_info(self)
coro = f"coro={self._coro_repr}"
if atleastpy38: # py3.8 added name=
repr_info[2] = coro # pragma: nocover
Expand Down Expand Up @@ -119,8 +122,8 @@ def __del__(self) -> None:
super().__del__()


def task_factory(loop, coro) -> TestTask:
task = TestTask(coro, loop=loop)
def task_factory(loop, coro, **kws) -> TestTask:
task = TestTask(coro, loop=loop, **kws)
return task


Expand Down Expand Up @@ -169,9 +172,11 @@ def _callTestMethod(self, testMethod) -> None:
_IGNORE_TASK_LEAKS_ATTR,
getattr(testMethod, _IGNORE_TASK_LEAKS_ATTR, False),
)

# pyre-fixme[16]: `TestCase` has no attribute `_asyncioTestLoop`.
loop = self._asyncioTestLoop
if sys.version_info >= (3, 11):
loop = self._asyncioRunner.get_loop()
else:
# pyre-fixme[16]: `TestCase` has no attribute `_asyncioTestLoop`.
loop = self._asyncioTestLoop
if not ignore_tasks:
# install our own task factory for monitoring usage
loop.set_task_factory(task_factory)
Expand All @@ -186,10 +191,11 @@ def _callTestMethod(self, testMethod) -> None:
# pyre-fixme[16]: `AsyncioTestCase` has no attribute `_callTestMethod`.
super()._callTestMethod(testMethod)

# Lets join the queue to insure all the tasks created by this case
# are cleaned up
# pyre-fixme[16]: `TestCase` has no attribute `_asyncioCallsQueue`.
loop.run_until_complete(self._asyncioCallsQueue.join())
if sys.version_info < (3, 11):
# Lets join the queue to insure all the tasks created by this case
# are cleaned up
# pyre-fixme[16]: `TestCase` has no attribute `_asyncioCallsQueue`.
loop.run_until_complete(self._asyncioCallsQueue.join())
left_over_tasks = set(all_tasks(loop)) - set(start_tasks)
for task in list(left_over_tasks):
if isinstance(task, TestTask) and task.was_managed():
Expand Down