Skip to content

Commit

Permalink
fix 3.11 testcase (#15)
Browse files Browse the repository at this point in the history
Summary:
Changes to IsolatedAsyncioTestCase and task_factory had to be accounted for

Pull Request resolved: #15

Test Plan: sandcastle

Reviewed By: itamaro

Differential Revision: D51716330

Pulled By: fried

fbshipit-source-id: 673f0ee59345ee4731649c970b90170f99a048a7
  • Loading branch information
fried authored and facebook-github-bot committed Nov 30, 2023
1 parent 97b8f18 commit 475d2db
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
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): # pragma: nocover
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

0 comments on commit 475d2db

Please sign in to comment.