Skip to content

Commit 2ad114d

Browse files
[3.10] bpo-45097: Add more tests for shutdown_asyncgens() (GH-28154) (GH-28159)
* bpo-45097: Add more tests for shutdown_asyncgens() (GH-28154) (cherry picked from commit c2970fd) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 9438443 commit 2ad114d

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

Lib/test/test_asyncgen.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import unittest
44

55
from test.support.import_helper import import_module
6+
from test.support import gc_collect
67
asyncio = import_module("asyncio")
78

89

@@ -1287,6 +1288,85 @@ async def wait():
12871288

12881289
self.assertEqual(finalized, 2)
12891290

1291+
def test_async_gen_asyncio_shutdown_02(self):
1292+
messages = []
1293+
1294+
def exception_handler(loop, context):
1295+
messages.append(context)
1296+
1297+
async def async_iterate():
1298+
yield 1
1299+
yield 2
1300+
1301+
it = async_iterate()
1302+
async def main():
1303+
loop = asyncio.get_running_loop()
1304+
loop.set_exception_handler(exception_handler)
1305+
1306+
async for i in it:
1307+
break
1308+
1309+
asyncio.run(main())
1310+
1311+
self.assertEqual(messages, [])
1312+
1313+
def test_async_gen_asyncio_shutdown_exception_01(self):
1314+
messages = []
1315+
1316+
def exception_handler(loop, context):
1317+
messages.append(context)
1318+
1319+
async def async_iterate():
1320+
try:
1321+
yield 1
1322+
yield 2
1323+
finally:
1324+
1/0
1325+
1326+
it = async_iterate()
1327+
async def main():
1328+
loop = asyncio.get_running_loop()
1329+
loop.set_exception_handler(exception_handler)
1330+
1331+
async for i in it:
1332+
break
1333+
1334+
asyncio.run(main())
1335+
1336+
message, = messages
1337+
self.assertEqual(message['asyncgen'], it)
1338+
self.assertIsInstance(message['exception'], ZeroDivisionError)
1339+
self.assertIn('an error occurred during closing of asynchronous generator',
1340+
message['message'])
1341+
1342+
def test_async_gen_asyncio_shutdown_exception_02(self):
1343+
messages = []
1344+
1345+
def exception_handler(loop, context):
1346+
messages.append(context)
1347+
1348+
async def async_iterate():
1349+
try:
1350+
yield 1
1351+
yield 2
1352+
finally:
1353+
1/0
1354+
1355+
async def main():
1356+
loop = asyncio.get_running_loop()
1357+
loop.set_exception_handler(exception_handler)
1358+
1359+
async for i in async_iterate():
1360+
break
1361+
gc_collect()
1362+
1363+
asyncio.run(main())
1364+
1365+
message, = messages
1366+
self.assertIsInstance(message['exception'], ZeroDivisionError)
1367+
self.assertIn('unhandled exception during asyncio.run() shutdown',
1368+
message['message'])
1369+
12901370
def test_async_gen_expression_01(self):
12911371
async def arange(n):
12921372
for i in range(n):

0 commit comments

Comments
 (0)