-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
Asyncio test case #13228
Closed
Closed
Asyncio test case #13228
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b77e62d
Test on
asvetlov de89fda
Work on
asvetlov 96fcbd9
Merge branch 'master' into async-test-case
asvetlov 9eef723
Fix test method call
asvetlov 18cb7d4
Merge branch 'master' into async-test-case
asvetlov 43091a0
Code cleanup
asvetlov d8ad4d7
Fix test by using positional-arguments only
asvetlov 85df707
Make basic test passing
asvetlov 78d885f
Test addCleanup
asvetlov 58cb1c8
Make test_support happy
asvetlov 0853043
More tests
asvetlov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import asyncio | ||
import inspect | ||
|
||
from .case import TestCase | ||
|
||
|
||
|
||
class AsyncioTestCase(TestCase): | ||
# Names intentionally have a long prefix | ||
# to reduce a chance of clashing with user-defined attributes | ||
# from inherited test case | ||
# | ||
# The class doesn't call loop.run_until_complete(self.setUp()) and family | ||
# but uses a different approach: | ||
# 1. create a long-running task that reads self.setUp() | ||
# awaitable from queue along with a future | ||
# 2. await the awaitable object passing in and set the result | ||
# into the future object | ||
# 3. Outer code puts the awaitable and the future object into a queue | ||
# with waiting for the future | ||
# The trick is necessary because every run_until_complete() call | ||
# creates a new task with embedded ContextVar context. | ||
# To share contextvars between setUp(), test and tearDown() we need to execute | ||
# them inside the same task. | ||
|
||
# Note: the test case modifies event loop policy if the policy was not instantiated | ||
# yet. | ||
# asyncio.get_event_loop_policy() creates a default policy on demand but never | ||
# returns None | ||
# I believe this is not an issue in user level tests but python itself for testing | ||
# should reset a policy in every test module | ||
# by calling asyncio.set_event_loop_policy(None) in tearDownModule() | ||
|
||
def __init__(self, methodName='runTest'): | ||
super().__init__(methodName) | ||
self._asyncioTestLoop = None | ||
self._asyncioCallsQueue = None | ||
|
||
def _callSetUp(self): | ||
self._callMaybeAsync(self.setUp) | ||
|
||
def _callTestMethod(self, method): | ||
self._callMaybeAsync(method) | ||
|
||
def _callTearDown(self): | ||
self._callMaybeAsync(self.tearDown) | ||
|
||
def _callCleanup(self, function, *args, **kwargs): | ||
self._callMaybeAsync(function, *args, **kwargs) | ||
|
||
def _callMaybeAsync(self, func, /, *args, **kwargs): | ||
assert self._asyncioTestLoop is not None | ||
ret = func(*args, **kwargs) | ||
if inspect.isawaitable(ret): | ||
fut = self._asyncioTestLoop.create_future() | ||
self._asyncioCallsQueue.put_nowait((fut, ret)) | ||
return self._asyncioTestLoop.run_until_complete(fut) | ||
else: | ||
return ret | ||
|
||
async def _asyncioLoopRunner(self): | ||
queue = self._asyncioCallsQueue | ||
while True: | ||
query = await queue.get() | ||
queue.task_done() | ||
if query is None: | ||
return | ||
fut, awaitable = query | ||
try: | ||
ret = await awaitable | ||
if not fut.cancelled(): | ||
fut.set_result(ret) | ||
except asyncio.CancelledError: | ||
raise | ||
except Exception as ex: | ||
if not fut.cancelled(): | ||
fut.set_exception(ex) | ||
|
||
def _setupAsyncioLoop(self): | ||
assert self._asyncioTestLoop is None | ||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop) | ||
loop.set_debug(True) | ||
self._asyncioTestLoop = loop | ||
self._asyncioCallsQueue = asyncio.Queue(loop=loop) | ||
self._asyncioCallsTask = loop.create_task(self._asyncioLoopRunner()) | ||
|
||
def _tearDownAsyncioLoop(self): | ||
assert self._asyncioTestLoop is not None | ||
loop = self._asyncioTestLoop | ||
self._asyncioTestLoop = None | ||
self._asyncioCallsQueue.put_nowait(None) | ||
loop.run_until_complete(self._asyncioCallsQueue.join()) | ||
|
||
try: | ||
# cancel all tasks | ||
to_cancel = asyncio.all_tasks(loop) | ||
if not to_cancel: | ||
return | ||
|
||
for task in to_cancel: | ||
task.cancel() | ||
|
||
loop.run_until_complete( | ||
asyncio.gather(*to_cancel, loop=loop, return_exceptions=True)) | ||
|
||
for task in to_cancel: | ||
if task.cancelled(): | ||
continue | ||
if task.exception() is not None: | ||
loop.call_exception_handler({ | ||
'message': 'unhandled exception during test shutdown', | ||
'exception': task.exception(), | ||
'task': task, | ||
}) | ||
# shutdown asyncgens | ||
loop.run_until_complete(loop.shutdown_asyncgens()) | ||
finally: | ||
asyncio.set_event_loop(None) | ||
loop.close() | ||
|
||
def run(self, result=None): | ||
self._setupAsyncioLoop() | ||
try: | ||
return super().run(result) | ||
finally: | ||
self._tearDownAsyncioLoop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import asyncio | ||
import unittest | ||
|
||
|
||
def tearDownModule(): | ||
asyncio.set_event_loop_policy(None) | ||
|
||
|
||
class TestAsyncCase(unittest.TestCase): | ||
def test_basic(self): | ||
events = [] | ||
|
||
class Test(unittest.AsyncioTestCase): | ||
async def setUp(self): | ||
self.assertEqual(events, []) | ||
events.append('setUp') | ||
|
||
async def test_func(self): | ||
self.assertEqual(events, ['setUp']) | ||
events.append('test') | ||
self.addCleanup(self.on_cleanup) | ||
|
||
async def tearDown(self): | ||
self.assertEqual(events, ['setUp', 'test']) | ||
events.append('tearDown') | ||
|
||
async def on_cleanup(self): | ||
self.assertEqual(events, ['setUp', 'test', 'tearDown']) | ||
events.append('cleanup') | ||
|
||
test = Test("test_func") | ||
test.run() | ||
self.assertEqual(events, ['setUp', 'test', 'tearDown', 'cleanup']) | ||
|
||
|
||
def test_exception_in_setup(self): | ||
events = [] | ||
|
||
class Test(unittest.AsyncioTestCase): | ||
async def setUp(self): | ||
events.append('setUp') | ||
raise Exception() | ||
|
||
async def test_func(self): | ||
events.append('test') | ||
self.addCleanup(self.on_cleanup) | ||
|
||
async def tearDown(self): | ||
events.append('tearDown') | ||
|
||
async def on_cleanup(self): | ||
events.append('cleanup') | ||
|
||
|
||
test = Test("test_func") | ||
test.run() | ||
self.assertEqual(events, ['setUp']) | ||
|
||
|
||
def test_exception_in_test(self): | ||
events = [] | ||
|
||
class Test(unittest.AsyncioTestCase): | ||
async def setUp(self): | ||
events.append('setUp') | ||
|
||
async def test_func(self): | ||
events.append('test') | ||
raise Exception() | ||
self.addCleanup(self.on_cleanup) | ||
|
||
async def tearDown(self): | ||
events.append('tearDown') | ||
|
||
async def on_cleanup(self): | ||
events.append('cleanup') | ||
|
||
test = Test("test_func") | ||
test.run() | ||
self.assertEqual(events, ['setUp', 'test', 'tearDown']) | ||
|
||
def test_exception_in_test_after_adding_cleanup(self): | ||
events = [] | ||
|
||
class Test(unittest.AsyncioTestCase): | ||
async def setUp(self): | ||
events.append('setUp') | ||
|
||
async def test_func(self): | ||
events.append('test') | ||
self.addCleanup(self.on_cleanup) | ||
raise Exception() | ||
|
||
async def tearDown(self): | ||
events.append('tearDown') | ||
|
||
async def on_cleanup(self): | ||
events.append('cleanup') | ||
|
||
test = Test("test_func") | ||
test.run() | ||
self.assertEqual(events, ['setUp', 'test', 'tearDown', 'cleanup']) | ||
|
||
def test_exception_in_tear_down(self): | ||
events = [] | ||
|
||
class Test(unittest.AsyncioTestCase): | ||
async def setUp(self): | ||
events.append('setUp') | ||
|
||
async def test_func(self): | ||
events.append('test') | ||
self.addCleanup(self.on_cleanup) | ||
|
||
async def tearDown(self): | ||
events.append('tearDown') | ||
raise Exception() | ||
|
||
async def on_cleanup(self): | ||
events.append('cleanup') | ||
|
||
test = Test("test_func") | ||
test.run() | ||
self.assertEqual(events, ['setUp', 'test', 'tearDown', 'cleanup']) | ||
|
||
|
||
def test_exception_in_tear_clean_up(self): | ||
events = [] | ||
|
||
class Test(unittest.AsyncioTestCase): | ||
async def setUp(self): | ||
events.append('setUp') | ||
|
||
async def test_func(self): | ||
events.append('test') | ||
self.addCleanup(self.on_cleanup) | ||
|
||
async def tearDown(self): | ||
events.append('tearDown') | ||
|
||
async def on_cleanup(self): | ||
events.append('cleanup') | ||
raise Exception() | ||
|
||
test = Test("test_func") | ||
test.run() | ||
self.assertEqual(events, ['setUp', 'test', 'tearDown', 'cleanup']) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One of the problems I can see happening here is multiple inheritance of tests cases with both "async" and "sync" versions of "tearDown".
super().tearDown()
in such case?super()
.