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

gh-96030: IsolatedAsyncioTestCase: don't create asyncio runner for skipped tests #96031

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
29 changes: 13 additions & 16 deletions Lib/unittest/async_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,26 @@ async def enterAsyncContext(self, cm):
return result

def _callSetUp(self):
self._asyncioTestContext.run(self.setUp)
self._callAsync(self.asyncSetUp)
self._setupAsyncioRunner()
try:
self._asyncioTestContext.run(self.setUp)
self._callAsync(self.asyncSetUp)
except BaseException:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I don't like catching BaseException here. It seems to only coincidentally fix the underlying issue -- which IMO is that the asyncio runner is set up even for skipped tests.

And the duplication of calling _tearDownAsyncioRunner both here and in _callTearDown also jars.

Copy link
Contributor

@graingert graingert Aug 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes this means if you have an IsolatedAsyncioTestCase with only skipped tests it means set_event_loop(None) is called when you might not have expected it to be

# _callTearDown is not called when _callSetUp fails.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that doCleanups() is called in run() if _callSetUp() fails. It will try to call callbacks after closing the runner.

self._tearDownAsyncioRunner()
raise

def _callTestMethod(self, method):
if self._callMaybeAsync(method) is not None:
warnings.warn(f'It is deprecated to return a value!=None from a '
f'test case ({method})', DeprecationWarning, stacklevel=4)

def _callTearDown(self):
self._callAsync(self.asyncTearDown)
self._asyncioTestContext.run(self.tearDown)
try:
self._callAsync(self.asyncTearDown)
self._asyncioTestContext.run(self.tearDown)
finally:
self._tearDownAsyncioRunner()

def _callCleanup(self, function, *args, **kwargs):
self._callMaybeAsync(function, *args, **kwargs)
Expand Down Expand Up @@ -125,18 +134,6 @@ def _tearDownAsyncioRunner(self):
runner = self._asyncioRunner
runner.close()

def run(self, result=None):
self._setupAsyncioRunner()
try:
return super().run(result)
finally:
self._tearDownAsyncioRunner()

def debug(self):
self._setupAsyncioRunner()
super().debug()
self._tearDownAsyncioRunner()
Copy link
Contributor

@graingert graingert Aug 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this used to not be called on an exception in debug() you probably want a test for this new behavior ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I think you fixed this #96021

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "this"? The function call self._tearDownAsyncioRunner()? It still does not get called when the setup or test case fails. It now gets called when asyncTearDown or tearDown fail with an exception. I think it's an acceptable deviation. Exceptions in tear down code are rare and it's ok to clean up the runner on error.

Copy link
Contributor

@graingert graingert Aug 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right but it used to not get called when debug failed, and now it does? So I think you fixed two bugs


def __del__(self):
if self._asyncioRunner is not None:
self._tearDownAsyncioRunner()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The ``run`` method of :class:`unittest.IsolatedAsyncioTestCase`
no longer sets up an asyncio runner if a test case or class is skipped.