@@ -498,29 +498,31 @@ def coro():
498
498
self .assertFalse (isinstance (task , MyTask ))
499
499
self .loop .run_until_complete (task )
500
500
501
- def test_shutdown_asyncgens_01 (self ):
502
- finalized = list ()
503
-
504
- if not hasattr (self .loop , 'shutdown_asyncgens' ):
505
- raise unittest .SkipTest ()
506
-
507
- waiter_src = '''async def waiter(timeout, finalized, loop):
508
- try:
509
- await asyncio.sleep(timeout, loop=loop)
510
- yield 1
511
- finally:
512
- await asyncio.sleep(0, loop=loop)
513
- finalized.append(1)
514
- '''
515
-
501
+ def _compile_agen (self , src ):
516
502
try :
517
503
g = {}
518
- exec (waiter_src , globals (), g )
504
+ exec (src , globals (), g )
519
505
except SyntaxError :
520
506
# Python < 3.6
521
507
raise unittest .SkipTest ()
522
508
else :
523
- waiter = g ['waiter' ]
509
+ return g ['waiter' ]
510
+
511
+ def test_shutdown_asyncgens_01 (self ):
512
+ finalized = list ()
513
+
514
+ if not hasattr (self .loop , 'shutdown_asyncgens' ):
515
+ raise unittest .SkipTest ()
516
+
517
+ waiter = self ._compile_agen (
518
+ '''async def waiter(timeout, finalized, loop):
519
+ try:
520
+ await asyncio.sleep(timeout, loop=loop)
521
+ yield 1
522
+ finally:
523
+ await asyncio.sleep(0, loop=loop)
524
+ finalized.append(1)
525
+ ''' )
524
526
525
527
async def wait ():
526
528
async for _ in waiter (1 , finalized , self .loop ):
@@ -539,6 +541,59 @@ async def wait():
539
541
t2 .cancel ()
540
542
self .loop .run_until_complete (asyncio .sleep (0.1 , loop = self .loop ))
541
543
544
+ def test_shutdown_asyncgens_02 (self ):
545
+ if not hasattr (self .loop , 'shutdown_asyncgens' ):
546
+ raise unittest .SkipTest ()
547
+
548
+ logged = 0
549
+
550
+ def logger (loop , context ):
551
+ nonlocal logged
552
+ self .assertIn ('asyncgen' , context )
553
+ expected = 'an error occurred during closing of asynchronous'
554
+ if expected in context ['message' ]:
555
+ logged += 1
556
+
557
+ waiter = self ._compile_agen ('''async def waiter(timeout, loop):
558
+ try:
559
+ await asyncio.sleep(timeout, loop=loop)
560
+ yield 1
561
+ finally:
562
+ 1 / 0
563
+ ''' )
564
+
565
+ async def wait ():
566
+ async for _ in waiter (1 , self .loop ):
567
+ pass
568
+
569
+ t = self .loop .create_task (wait ())
570
+ self .loop .run_until_complete (asyncio .sleep (0.1 , loop = self .loop ))
571
+
572
+ self .loop .set_exception_handler (logger )
573
+ self .loop .run_until_complete (self .loop .shutdown_asyncgens ())
574
+
575
+ self .assertEqual (logged , 1 )
576
+
577
+ # Silence warnings
578
+ t .cancel ()
579
+ self .loop .run_until_complete (asyncio .sleep (0.1 , loop = self .loop ))
580
+
581
+ def test_shutdown_asyncgens_03 (self ):
582
+ if not hasattr (self .loop , 'shutdown_asyncgens' ):
583
+ raise unittest .SkipTest ()
584
+
585
+ waiter = self ._compile_agen ('''async def waiter():
586
+ yield 1
587
+ yield 2
588
+ ''' )
589
+
590
+ async def foo ():
591
+ # We specifically want to hit _asyncgen_finalizer_hook
592
+ # method.
593
+ await waiter ().asend (None )
594
+
595
+ self .loop .run_until_complete (foo ())
596
+ self .loop .run_until_complete (asyncio .sleep (0.01 , loop = self .loop ))
542
597
543
598
class TestBaseUV (_TestBase , UVTestCase ):
544
599
0 commit comments