-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix #2723: Retry on HTTP 400 failedPrecondition #2731
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -878,6 +878,20 @@ def test_media_io_base_download_unknown_media_size(self): | |
| } | ||
| }""" | ||
|
|
||
| FAILED_PRECONDITION_RESPONSE = """{ | ||
| "error": { | ||
| "errors": [ | ||
| { | ||
| "domain": "global", | ||
| "reason": "failedPrecondition", | ||
| "message": "Precondition Failed" | ||
| } | ||
| ], | ||
| "code": 400, | ||
| "message": "Precondition Failed" | ||
| } | ||
| }""" | ||
|
|
||
|
|
||
| NOT_CONFIGURED_RESPONSE = """{ | ||
| "error": { | ||
|
|
@@ -1056,6 +1070,34 @@ def test_no_retry_succeeds(self): | |
|
|
||
| self.assertEqual(0, len(sleeptimes)) | ||
|
|
||
| def test_retry_400_failed_precondition(self): | ||
| num_retries = 2 | ||
| resp_seq = [ | ||
| ({"status": "400"}, FAILED_PRECONDITION_RESPONSE), | ||
| ({"status": "200"}, "{}") | ||
| ] | ||
| http = HttpMockSequence(resp_seq) | ||
| model = JsonModel() | ||
| uri = "https://www.googleapis.com/someapi/v1/collection/?foo=bar" | ||
| method = "POST" | ||
| request = HttpRequest( | ||
| http, | ||
| model.response, | ||
| uri, | ||
| method=method, | ||
| body="{}", | ||
| headers={"content-type": "application/json"}, | ||
| ) | ||
|
|
||
| sleeptimes = [] | ||
| request._sleep = lambda x: sleeptimes.append(x) | ||
| request._rand = lambda: 10 | ||
|
|
||
| request.execute(num_retries=num_retries) | ||
|
|
||
| self.assertEqual(1, len(sleeptimes)) | ||
| self.assertEqual(10 * 2 ** 1, sleeptimes[0]) | ||
|
Comment on lines
+1073
to
+1099
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new test You could achieve this by parameterizing the test or by adding a separate test method for the |
||
|
|
||
| def test_no_retry_fails_fast(self): | ||
| http = HttpMockSequence([({"status": "500"}, ""), ({"status": "200"}, "{}")]) | ||
| model = JsonModel() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better maintainability and to make it easier to add more retryable status codes in the future, consider refactoring this
if/elifblock to be more data-driven. You can use a dictionary to map status codes to their retryable reasons and log messages.