diff options
author | Ćukasz Langa <lukasz@langa.pl> | 2021-09-22 16:42:15 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-22 16:42:15 (GMT) |
commit | 44396aaba9b92b3a38a4b422a000d1a8eb05185a (patch) | |
tree | 7ec3abf49d9f3497bf3fe4afa836f2a7bb050db9 /Lib/unittest/async_case.py | |
parent | 8c1e1da565bca9cec792323eb728e288715ef7c4 (diff) | |
download | cpython-44396aaba9b92b3a38a4b422a000d1a8eb05185a.zip cpython-44396aaba9b92b3a38a4b422a000d1a8eb05185a.tar.gz cpython-44396aaba9b92b3a38a4b422a000d1a8eb05185a.tar.bz2 |
[3.10] bpo-45238: Fix unittest.IsolatedAsyncioTestCase.debug() (GH-28449) (GH-28521)
It runs now asynchronous methods and callbacks.
If it fails, doCleanups() can be called for cleaning up.
(cherry picked from commit ecb6922ff2d56476a6cfb0941ae55aca5e7fae3d)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib/unittest/async_case.py')
-rw-r--r-- | Lib/unittest/async_case.py | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/Lib/unittest/async_case.py b/Lib/unittest/async_case.py index 86ed704..c48380e 100644 --- a/Lib/unittest/async_case.py +++ b/Lib/unittest/async_case.py @@ -72,15 +72,15 @@ class IsolatedAsyncioTestCase(TestCase): self._callMaybeAsync(function, *args, **kwargs) def _callAsync(self, func, /, *args, **kwargs): - assert self._asyncioTestLoop is not None + assert self._asyncioTestLoop is not None, 'asyncio test loop is not initialized' ret = func(*args, **kwargs) - assert inspect.isawaitable(ret) + assert inspect.isawaitable(ret), f'{func!r} returned non-awaitable' fut = self._asyncioTestLoop.create_future() self._asyncioCallsQueue.put_nowait((fut, ret)) return self._asyncioTestLoop.run_until_complete(fut) def _callMaybeAsync(self, func, /, *args, **kwargs): - assert self._asyncioTestLoop is not None + assert self._asyncioTestLoop is not None, 'asyncio test loop is not initialized' ret = func(*args, **kwargs) if inspect.isawaitable(ret): fut = self._asyncioTestLoop.create_future() @@ -109,7 +109,7 @@ class IsolatedAsyncioTestCase(TestCase): fut.set_exception(ex) def _setupAsyncioLoop(self): - assert self._asyncioTestLoop is None + assert self._asyncioTestLoop is None, 'asyncio test loop already initialized' loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.set_debug(True) @@ -119,7 +119,7 @@ class IsolatedAsyncioTestCase(TestCase): loop.run_until_complete(fut) def _tearDownAsyncioLoop(self): - assert self._asyncioTestLoop is not None + assert self._asyncioTestLoop is not None, 'asyncio test loop is not initialized' loop = self._asyncioTestLoop self._asyncioTestLoop = None self._asyncioCallsQueue.put_nowait(None) @@ -158,3 +158,12 @@ class IsolatedAsyncioTestCase(TestCase): return super().run(result) finally: self._tearDownAsyncioLoop() + + def debug(self): + self._setupAsyncioLoop() + super().debug() + self._tearDownAsyncioLoop() + + def __del__(self): + if self._asyncioTestLoop is not None: + self._tearDownAsyncioLoop() |