generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 24
Add functional tests for standard retries #601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alexgromero
merged 5 commits into
smithy-lang:develop
from
alexgromero:add-functional-tests
Nov 25, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4193da0
Add functional tests for StandardRetryStrategy and StandardRetryQuota
alexgromero 5fb7e15
Remove access to private attributes
alexgromero c0858d2
Remove async from test
alexgromero 063ed28
Refactor functional retry tests to match production retry flow
alexgromero 7afac12
Add TODO and timeout errors test
alexgromero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from asyncio import gather, sleep | ||
|
|
||
| import pytest | ||
| from smithy_core.exceptions import CallError, ClientTimeoutError, RetryError | ||
| from smithy_core.interfaces import retries as retries_interface | ||
| from smithy_core.retries import ( | ||
| ExponentialBackoffJitterType, | ||
| ExponentialRetryBackoffStrategy, | ||
| StandardRetryQuota, | ||
| StandardRetryStrategy, | ||
| ) | ||
|
|
||
|
|
||
| # TODO: Refactor this to use a smithy-testing generated client | ||
| async def retry_operation( | ||
| strategy: retries_interface.RetryStrategy, | ||
| responses: list[int | Exception], | ||
| ) -> tuple[str, int]: | ||
| token = strategy.acquire_initial_retry_token() | ||
| response_iter = iter(responses) | ||
|
|
||
| while True: | ||
| if token.retry_delay: | ||
| await sleep(token.retry_delay) | ||
|
|
||
| response = next(response_iter) | ||
| attempt = token.retry_count + 1 | ||
|
|
||
| # Success case | ||
| if response == 200: | ||
| strategy.record_success(token=token) | ||
| return "success", attempt | ||
|
|
||
| # Error case - either status code or exception | ||
| if isinstance(response, Exception): | ||
| error = response | ||
| else: | ||
| error = CallError( | ||
| fault="server" if response >= 500 else "client", | ||
| message=f"HTTP {response}", | ||
| is_retry_safe=response >= 500, | ||
| ) | ||
|
|
||
| try: | ||
| token = strategy.refresh_retry_token_for_retry( | ||
| token_to_renew=token, error=error | ||
| ) | ||
| except RetryError: | ||
| raise error | ||
|
|
||
|
|
||
| async def test_standard_retry_eventually_succeeds(): | ||
| quota = StandardRetryQuota(initial_capacity=500) | ||
| strategy = StandardRetryStrategy(max_attempts=3, retry_quota=quota) | ||
|
|
||
| result, attempts = await retry_operation(strategy, [500, 500, 200]) | ||
|
|
||
| assert result == "success" | ||
| assert attempts == 3 | ||
| assert quota.available_capacity == 495 | ||
|
|
||
|
|
||
| async def test_standard_retry_fails_due_to_max_attempts(): | ||
| quota = StandardRetryQuota(initial_capacity=500) | ||
| strategy = StandardRetryStrategy(max_attempts=3, retry_quota=quota) | ||
|
|
||
| with pytest.raises(CallError, match="502"): | ||
| await retry_operation(strategy, [502, 502, 502]) | ||
|
|
||
| assert quota.available_capacity == 490 | ||
|
|
||
|
|
||
| async def test_retry_quota_exhausted_after_single_retry(): | ||
| quota = StandardRetryQuota(initial_capacity=5) | ||
| strategy = StandardRetryStrategy(max_attempts=3, retry_quota=quota) | ||
|
|
||
| with pytest.raises(CallError, match="502"): | ||
| await retry_operation(strategy, [500, 502]) | ||
|
|
||
| assert quota.available_capacity == 0 | ||
|
|
||
|
|
||
| async def test_retry_quota_prevents_retries_when_quota_zero(): | ||
| quota = StandardRetryQuota(initial_capacity=0) | ||
| strategy = StandardRetryStrategy(max_attempts=3, retry_quota=quota) | ||
|
|
||
| with pytest.raises(CallError, match="500"): | ||
| await retry_operation(strategy, [500]) | ||
|
|
||
| assert quota.available_capacity == 0 | ||
|
|
||
|
|
||
| async def test_retry_quota_stops_retries_when_exhausted(): | ||
| quota = StandardRetryQuota(initial_capacity=10) | ||
| strategy = StandardRetryStrategy(max_attempts=5, retry_quota=quota) | ||
|
|
||
| with pytest.raises(CallError, match="503"): | ||
| await retry_operation(strategy, [500, 502, 503]) | ||
|
|
||
| assert quota.available_capacity == 0 | ||
|
|
||
|
|
||
| async def test_retry_quota_recovers_after_successful_responses(): | ||
| quota = StandardRetryQuota(initial_capacity=15) | ||
| strategy = StandardRetryStrategy(max_attempts=5, retry_quota=quota) | ||
|
|
||
| # First operation: 2 retries then success | ||
| await retry_operation(strategy, [500, 502, 200]) | ||
| assert quota.available_capacity == 10 | ||
|
|
||
| # Second operation: 1 retry then success | ||
| await retry_operation(strategy, [500, 200]) | ||
| assert quota.available_capacity == 10 | ||
|
|
||
|
|
||
| async def test_retry_quota_shared_across_concurrent_operations(): | ||
| quota = StandardRetryQuota(initial_capacity=500) | ||
| backoff = ExponentialRetryBackoffStrategy( | ||
| backoff_scale_value=1, | ||
| max_backoff=10, | ||
| jitter_type=ExponentialBackoffJitterType.FULL, | ||
| ) | ||
| strategy = StandardRetryStrategy( | ||
| max_attempts=5, | ||
| retry_quota=quota, | ||
| backoff_strategy=backoff, | ||
| ) | ||
|
|
||
| result1, result2 = await gather( | ||
| retry_operation(strategy, [500, 500, 200]), | ||
| retry_operation(strategy, [500, 200]), | ||
| ) | ||
|
|
||
| assert result1 == ("success", 3) | ||
| assert result2 == ("success", 2) | ||
| assert quota.available_capacity == 495 | ||
|
|
||
|
|
||
| async def test_retry_quota_handles_timeout_errors(): | ||
| quota = StandardRetryQuota(initial_capacity=500) | ||
| strategy = StandardRetryStrategy(max_attempts=3, retry_quota=quota) | ||
|
|
||
| timeout1 = ClientTimeoutError() | ||
| timeout2 = ClientTimeoutError() | ||
|
|
||
| result, attempts = await retry_operation(strategy, [timeout1, timeout2, 200]) | ||
|
|
||
| assert result == "success" | ||
| assert attempts == 3 | ||
| assert quota.available_capacity == 490 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.