@@ -428,6 +428,10 @@ async def _generator_mock(
428
428
sleep_time = 0 ,
429
429
ignore_sent = False ,
430
430
):
431
+ """
432
+ Helper to create a mock generator that yields a number of values
433
+ Generator can optionally raise an exception on a specific iteration
434
+ """
431
435
try :
432
436
sent_in = None
433
437
for i in range (num ):
@@ -447,6 +451,10 @@ async def _generator_mock(
447
451
@mock .patch ("asyncio.sleep" , autospec = True )
448
452
@pytest .mark .asyncio
449
453
async def test___call___generator_success (self , sleep ):
454
+ """
455
+ Test that a retry-decorated generator yields values as expected
456
+ This test checks a generator with no issues
457
+ """
450
458
from collections .abc import AsyncGenerator
451
459
452
460
retry_ = retry_async .AsyncRetry (is_stream = True )
@@ -469,6 +477,9 @@ async def test___call___generator_success(self, sleep):
469
477
@mock .patch ("asyncio.sleep" , autospec = True )
470
478
@pytest .mark .asyncio
471
479
async def test___call___generator_retry (self , sleep ):
480
+ """
481
+ Tests that a retry-decorated generator will retry on errors
482
+ """
472
483
on_error = mock .Mock (return_value = None )
473
484
retry_ = retry_async .AsyncRetry (
474
485
on_error = on_error ,
@@ -487,6 +498,10 @@ async def test___call___generator_retry(self, sleep):
487
498
@mock .patch ("asyncio.sleep" , autospec = True )
488
499
@pytest .mark .asyncio
489
500
async def test___call___generator_retry_hitting_deadline (self , sleep , uniform ):
501
+ """
502
+ Tests that a retry-decorated generator will throw a RetryError
503
+ after using the time budget
504
+ """
490
505
on_error = mock .Mock ()
491
506
retry_ = retry_async .AsyncRetry (
492
507
predicate = retry_async .if_exception_type (ValueError ),
@@ -527,6 +542,10 @@ def increase_time(sleep_delay):
527
542
528
543
@pytest .mark .asyncio
529
544
async def test___call___generator_timeout_cancellations (self ):
545
+ """
546
+ Tests that a retry-decorated generator will throw a RetryError
547
+ after using its time budget
548
+ """
530
549
on_error = mock .Mock (return_value = None )
531
550
retry_ = retry_async .AsyncRetry (
532
551
predicate = retry_async .if_exception_type (ValueError ),
@@ -617,6 +636,9 @@ async def test___call___with_generator_send(self, sleep):
617
636
@mock .patch ("asyncio.sleep" , autospec = True )
618
637
@pytest .mark .asyncio
619
638
async def test___call___generator_send_retry (self , sleep ):
639
+ """
640
+ Send should be retried if target generator raises an error
641
+ """
620
642
on_error = mock .Mock (return_value = None )
621
643
retry_ = retry_async .AsyncRetry (
622
644
on_error = on_error ,
@@ -639,6 +661,9 @@ async def test___call___generator_send_retry(self, sleep):
639
661
@mock .patch ("asyncio.sleep" , autospec = True )
640
662
@pytest .mark .asyncio
641
663
async def test___call___with_generator_close (self , sleep ):
664
+ """
665
+ Close should be passed through retry into target generator
666
+ """
642
667
retry_ = retry_async .AsyncRetry (is_stream = True )
643
668
decorated = retry_ (self ._generator_mock )
644
669
exception_list = []
@@ -655,6 +680,9 @@ async def test___call___with_generator_close(self, sleep):
655
680
@mock .patch ("asyncio.sleep" , autospec = True )
656
681
@pytest .mark .asyncio
657
682
async def test___call___with_generator_throw (self , sleep ):
683
+ """
684
+ Throw should be passed through retry into target generator
685
+ """
658
686
retry_ = retry_async .AsyncRetry (
659
687
predicate = retry_async .if_exception_type (ValueError ),
660
688
is_stream = True ,
@@ -691,18 +719,21 @@ async def test___call___with_iterable_coroutine_send_close_throw(self, sleep):
691
719
retry_ = retry_async .AsyncRetry (is_stream = True )
692
720
693
721
async def iterable_fn (n ):
694
- class CustomIterator :
722
+ class CustomIterable :
695
723
def __init__ (self , n ):
696
724
self .n = n
697
725
self .i = 0
698
726
727
+ def __aiter__ (self ):
728
+ return self
729
+
699
730
async def __anext__ (self ):
700
731
if self .i == self .n :
701
732
raise StopAsyncIteration
702
733
self .i += 1
703
734
return self .i - 1
704
735
705
- return CustomIterator (n )
736
+ return CustomIterable (n )
706
737
707
738
decorated = retry_ (iterable_fn )
708
739
@@ -731,18 +762,21 @@ async def test___call___with_iterable_send_close_throw(self, sleep):
731
762
retry_ = retry_async .AsyncRetry (is_stream = True )
732
763
733
764
def iterable_fn (n ):
734
- class CustomIterator :
765
+ class CustomIterable :
735
766
def __init__ (self , n ):
736
767
self .n = n
737
768
self .i = 0
738
769
770
+ def __aiter__ (self ):
771
+ return self
772
+
739
773
async def __anext__ (self ):
740
774
if self .i == self .n :
741
775
raise StopAsyncIteration
742
776
self .i += 1
743
777
return self .i - 1
744
778
745
- return CustomIterator (n )
779
+ return CustomIterable (n )
746
780
747
781
decorated = retry_ (iterable_fn )
748
782
0 commit comments