Skip to content

Commit 265d998

Browse files
committed
improved test docs
1 parent 5c3805d commit 265d998

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

google/api_core/retry_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ async def retry_wrapped_func(*args, **kwargs):
240240
)
241241

242242
@functools.wraps(func)
243-
def retry_wrapped_stream(*args, deadline_dt=None, **kwargs):
243+
def retry_wrapped_stream(*args, **kwargs):
244244
"""A wrapper that iterates over target stream with retry."""
245245
target = functools.partial(func, *args, **kwargs)
246246
sleep_generator = exponential_sleep_generator(

tests/asyncio/test_retry_async.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,10 @@ async def _generator_mock(
428428
sleep_time=0,
429429
ignore_sent=False,
430430
):
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+
"""
431435
try:
432436
sent_in = None
433437
for i in range(num):
@@ -447,6 +451,10 @@ async def _generator_mock(
447451
@mock.patch("asyncio.sleep", autospec=True)
448452
@pytest.mark.asyncio
449453
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+
"""
450458
from collections.abc import AsyncGenerator
451459

452460
retry_ = retry_async.AsyncRetry(is_stream=True)
@@ -469,6 +477,9 @@ async def test___call___generator_success(self, sleep):
469477
@mock.patch("asyncio.sleep", autospec=True)
470478
@pytest.mark.asyncio
471479
async def test___call___generator_retry(self, sleep):
480+
"""
481+
Tests that a retry-decorated generator will retry on errors
482+
"""
472483
on_error = mock.Mock(return_value=None)
473484
retry_ = retry_async.AsyncRetry(
474485
on_error=on_error,
@@ -487,6 +498,10 @@ async def test___call___generator_retry(self, sleep):
487498
@mock.patch("asyncio.sleep", autospec=True)
488499
@pytest.mark.asyncio
489500
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+
"""
490505
on_error = mock.Mock()
491506
retry_ = retry_async.AsyncRetry(
492507
predicate=retry_async.if_exception_type(ValueError),
@@ -527,6 +542,10 @@ def increase_time(sleep_delay):
527542

528543
@pytest.mark.asyncio
529544
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+
"""
530549
on_error = mock.Mock(return_value=None)
531550
retry_ = retry_async.AsyncRetry(
532551
predicate=retry_async.if_exception_type(ValueError),
@@ -617,6 +636,9 @@ async def test___call___with_generator_send(self, sleep):
617636
@mock.patch("asyncio.sleep", autospec=True)
618637
@pytest.mark.asyncio
619638
async def test___call___generator_send_retry(self, sleep):
639+
"""
640+
Send should be retried if target generator raises an error
641+
"""
620642
on_error = mock.Mock(return_value=None)
621643
retry_ = retry_async.AsyncRetry(
622644
on_error=on_error,
@@ -639,6 +661,9 @@ async def test___call___generator_send_retry(self, sleep):
639661
@mock.patch("asyncio.sleep", autospec=True)
640662
@pytest.mark.asyncio
641663
async def test___call___with_generator_close(self, sleep):
664+
"""
665+
Close should be passed through retry into target generator
666+
"""
642667
retry_ = retry_async.AsyncRetry(is_stream=True)
643668
decorated = retry_(self._generator_mock)
644669
exception_list = []
@@ -655,6 +680,9 @@ async def test___call___with_generator_close(self, sleep):
655680
@mock.patch("asyncio.sleep", autospec=True)
656681
@pytest.mark.asyncio
657682
async def test___call___with_generator_throw(self, sleep):
683+
"""
684+
Throw should be passed through retry into target generator
685+
"""
658686
retry_ = retry_async.AsyncRetry(
659687
predicate=retry_async.if_exception_type(ValueError),
660688
is_stream=True,
@@ -691,18 +719,21 @@ async def test___call___with_iterable_coroutine_send_close_throw(self, sleep):
691719
retry_ = retry_async.AsyncRetry(is_stream=True)
692720

693721
async def iterable_fn(n):
694-
class CustomIterator:
722+
class CustomIterable:
695723
def __init__(self, n):
696724
self.n = n
697725
self.i = 0
698726

727+
def __aiter__(self):
728+
return self
729+
699730
async def __anext__(self):
700731
if self.i == self.n:
701732
raise StopAsyncIteration
702733
self.i += 1
703734
return self.i - 1
704735

705-
return CustomIterator(n)
736+
return CustomIterable(n)
706737

707738
decorated = retry_(iterable_fn)
708739

@@ -731,18 +762,21 @@ async def test___call___with_iterable_send_close_throw(self, sleep):
731762
retry_ = retry_async.AsyncRetry(is_stream=True)
732763

733764
def iterable_fn(n):
734-
class CustomIterator:
765+
class CustomIterable:
735766
def __init__(self, n):
736767
self.n = n
737768
self.i = 0
738769

770+
def __aiter__(self):
771+
return self
772+
739773
async def __anext__(self):
740774
if self.i == self.n:
741775
raise StopAsyncIteration
742776
self.i += 1
743777
return self.i - 1
744778

745-
return CustomIterator(n)
779+
return CustomIterable(n)
746780

747781
decorated = retry_(iterable_fn)
748782

tests/unit/test_retry.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Copyright 2017 Google LLC
12
#
23
# Licensed under the Apache License, Version 2.0 (the "License");
34
# you may not use this file except in compliance with the License.
@@ -493,6 +494,10 @@ def _generator_mock(
493494
exceptions_seen=None,
494495
ignore_sent=False,
495496
):
497+
"""
498+
Helper to create a mock generator that yields a number of values
499+
Generator can optionally raise an exception on a specific iteration
500+
"""
496501
try:
497502
sent_in = None
498503
for i in range(num):
@@ -510,6 +515,10 @@ def _generator_mock(
510515

511516
@mock.patch("time.sleep", autospec=True)
512517
def test___call___generator_success(self, sleep):
518+
"""
519+
Test that a retry-decorated generator yields values as expected
520+
This test checks a generator with no issues
521+
"""
513522
import types
514523
import collections
515524

@@ -533,6 +542,9 @@ def test___call___generator_success(self, sleep):
533542

534543
@mock.patch("time.sleep", autospec=True)
535544
def test___call___generator_retry(self, sleep):
545+
"""
546+
Tests that a retry-decorated generator will retry on errors
547+
"""
536548
on_error = mock.Mock(return_value=None)
537549
retry_ = retry.Retry(
538550
on_error=on_error,
@@ -550,6 +562,10 @@ def test___call___generator_retry(self, sleep):
550562
@mock.patch("random.uniform", autospec=True, side_effect=lambda m, n: n)
551563
@mock.patch("time.sleep", autospec=True)
552564
def test___call___generator_retry_hitting_deadline(self, sleep, uniform):
565+
"""
566+
Tests that a retry-decorated generator will throw a RetryError
567+
after using the time budget
568+
"""
553569
on_error = mock.Mock(return_value=None)
554570
retry_ = retry.Retry(
555571
predicate=retry.if_exception_type(ValueError),
@@ -677,6 +693,9 @@ def test___call___with_generator_return(self, sleep):
677693

678694
@mock.patch("time.sleep", autospec=True)
679695
def test___call___with_generator_close(self, sleep):
696+
"""
697+
Close should be passed through retry into target generator
698+
"""
680699
retry_ = retry.Retry(is_stream=True)
681700

682701
decorated = retry_(self._generator_mock)
@@ -693,6 +712,9 @@ def test___call___with_generator_close(self, sleep):
693712

694713
@mock.patch("time.sleep", autospec=True)
695714
def test___call___with_generator_throw(self, sleep):
715+
"""
716+
Throw should be passed through retry into target generator
717+
"""
696718
retry_ = retry.Retry(
697719
predicate=retry.if_exception_type(ValueError), is_stream=True
698720
)
@@ -721,6 +743,10 @@ def test___call___with_generator_throw(self, sleep):
721743

722744
@mock.patch("time.sleep", autospec=True)
723745
def test___call___with_is_stream(self, sleep):
746+
"""
747+
is_stream should determine if the target is wrapped as a
748+
generator or as a callable
749+
"""
724750
gen_retry_ = retry.Retry(
725751
is_stream=True, predicate=retry.if_exception_type(ValueError)
726752
)

0 commit comments

Comments
 (0)