summaryrefslogtreecommitdiffstats
path: root/contrib/python/google-auth/py3/tests/test__exponential_backoff.py
diff options
context:
space:
mode:
authorrobot-piglet <[email protected]>2024-10-06 13:42:43 +0300
committerrobot-piglet <[email protected]>2024-10-06 13:52:30 +0300
commit52aed29f744afda4549ef5d64acd0fa8c2092789 (patch)
treee40c9abd25653990d13b68936aee518454df424e /contrib/python/google-auth/py3/tests/test__exponential_backoff.py
parent813943fcad905eee1235d764be4268dddd07ce64 (diff)
Intermediate changes
commit_hash:cc4365f5a0e443b92d87079a9c91e77fea2ddcaf
Diffstat (limited to 'contrib/python/google-auth/py3/tests/test__exponential_backoff.py')
-rw-r--r--contrib/python/google-auth/py3/tests/test__exponential_backoff.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/contrib/python/google-auth/py3/tests/test__exponential_backoff.py b/contrib/python/google-auth/py3/tests/test__exponential_backoff.py
index 95422502b0d..b7b6877b2c0 100644
--- a/contrib/python/google-auth/py3/tests/test__exponential_backoff.py
+++ b/contrib/python/google-auth/py3/tests/test__exponential_backoff.py
@@ -54,3 +54,44 @@ def test_minimum_total_attempts():
with pytest.raises(exceptions.InvalidValue):
_exponential_backoff.ExponentialBackoff(total_attempts=-1)
_exponential_backoff.ExponentialBackoff(total_attempts=1)
+
+
[email protected]("asyncio.sleep", return_value=None)
+async def test_exponential_backoff_async(mock_time_async):
+ eb = _exponential_backoff.AsyncExponentialBackoff()
+ curr_wait = eb._current_wait_in_seconds
+ iteration_count = 0
+
+ # Workaround issue in python 3.9 related to code coverage by adding `# pragma: no branch`
+ # See https://github.com/googleapis/gapic-generator-python/pull/1174#issuecomment-1025132372
+ async for attempt in eb: # pragma: no branch
+ if attempt == 1:
+ assert mock_time_async.call_count == 0
+ else:
+ backoff_interval = mock_time_async.call_args[0][0]
+ jitter = curr_wait * eb._randomization_factor
+
+ assert (curr_wait - jitter) <= backoff_interval <= (curr_wait + jitter)
+ assert attempt == iteration_count + 1
+ assert eb.backoff_count == iteration_count + 1
+ assert eb._current_wait_in_seconds == eb._multiplier ** iteration_count
+
+ curr_wait = eb._current_wait_in_seconds
+ iteration_count += 1
+
+ assert eb.total_attempts == _exponential_backoff._DEFAULT_RETRY_TOTAL_ATTEMPTS
+ assert eb.backoff_count == _exponential_backoff._DEFAULT_RETRY_TOTAL_ATTEMPTS
+ assert iteration_count == _exponential_backoff._DEFAULT_RETRY_TOTAL_ATTEMPTS
+ assert (
+ mock_time_async.call_count
+ == _exponential_backoff._DEFAULT_RETRY_TOTAL_ATTEMPTS - 1
+ )
+
+
+def test_minimum_total_attempts_async():
+ with pytest.raises(exceptions.InvalidValue):
+ _exponential_backoff.AsyncExponentialBackoff(total_attempts=0)
+ with pytest.raises(exceptions.InvalidValue):
+ _exponential_backoff.AsyncExponentialBackoff(total_attempts=-1)
+ _exponential_backoff.AsyncExponentialBackoff(total_attempts=1)