bpo-39101: Fixes BaseException hang in IsolatedAsyncioTestCase. (GH-22654)

This commit is contained in:
Lisa Roach 2020-10-26 09:28:17 -07:00 committed by GitHub
parent 47e1afd2a1
commit 8374d2ee15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 2 deletions

View File

@ -102,9 +102,9 @@ class IsolatedAsyncioTestCase(TestCase):
ret = await awaitable ret = await awaitable
if not fut.cancelled(): if not fut.cancelled():
fut.set_result(ret) fut.set_result(ret)
except asyncio.CancelledError: except (SystemExit, KeyboardInterrupt):
raise raise
except Exception as ex: except (BaseException, asyncio.CancelledError) as ex:
if not fut.cancelled(): if not fut.cancelled():
fut.set_exception(ex) fut.set_exception(ex)

View File

@ -190,6 +190,33 @@ class TestAsyncCase(unittest.TestCase):
'async_cleanup 2', 'async_cleanup 2',
'sync_cleanup 1']) 'sync_cleanup 1'])
def test_base_exception_from_async_method(self):
events = []
class Test(unittest.IsolatedAsyncioTestCase):
async def test_base(self):
events.append("test_base")
raise BaseException()
events.append("not it")
async def test_no_err(self):
events.append("test_no_err")
async def test_cancel(self):
raise asyncio.CancelledError()
test = Test("test_base")
output = test.run()
self.assertFalse(output.wasSuccessful())
test = Test("test_no_err")
test.run()
self.assertEqual(events, ['test_base', 'test_no_err'])
test = Test("test_cancel")
output = test.run()
self.assertFalse(output.wasSuccessful())
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -0,0 +1 @@
Fixed tests using IsolatedAsyncioTestCase from hanging on BaseExceptions.