-
Notifications
You must be signed in to change notification settings - Fork 371
fix(search): detect and retry Google's HTTP-200 ErrorResponse envelope (#200) #201
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 | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,13 +2,16 @@ | |||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| from __future__ import annotations | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| import json | ||||||||||||||||||||||||||||||
| from unittest.mock import MagicMock | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| import pytest | ||||||||||||||||||||||||||||||
| from tenacity import wait_none | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| import fli.search.client as client_module | ||||||||||||||||||||||||||||||
| from fli.search.client import _host_from_url, _wrap_request_error, get_client | ||||||||||||||||||||||||||||||
| from fli.search.client import _host_from_url, _wrap_request_error, get_client, post_rpc | ||||||||||||||||||||||||||||||
| from fli.search.exceptions import ( | ||||||||||||||||||||||||||||||
| SearchBackendError, | ||||||||||||||||||||||||||||||
| SearchClientError, | ||||||||||||||||||||||||||||||
| SearchConnectionError, | ||||||||||||||||||||||||||||||
| SearchHTTPError, | ||||||||||||||||||||||||||||||
|
|
@@ -101,6 +104,85 @@ def test_empty_string_returns_empty(self): | |||||||||||||||||||||||||||||
| assert result == "" | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def _resp(text: str): | ||||||||||||||||||||||||||||||
| """Return a minimal stand-in for a curl_cffi Response.""" | ||||||||||||||||||||||||||||||
| r = MagicMock() | ||||||||||||||||||||||||||||||
| r.text = text | ||||||||||||||||||||||||||||||
| r.raise_for_status = MagicMock() | ||||||||||||||||||||||||||||||
| return r | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def _ok_body(): | ||||||||||||||||||||||||||||||
| return ")]}'\n\n" + json.dumps([["wrb.fr", None, json.dumps([[1, "data"]])]]) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def _error_body(code: int): | ||||||||||||||||||||||||||||||
| type_url = "type.googleapis.com/travel.frontend.flights.ErrorResponse" | ||||||||||||||||||||||||||||||
| row = ["wrb.fr", None, None, None, None, [code, None, [[type_url, [[None, [], 0]]]]]] | ||||||||||||||||||||||||||||||
| return ")]}'\n\n" + json.dumps([row]) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| @pytest.fixture(autouse=True) | ||||||||||||||||||||||||||||||
| def _no_backoff_sleep(): | ||||||||||||||||||||||||||||||
| """Strip the exponential wait from post_rpc so retry tests run instantly.""" | ||||||||||||||||||||||||||||||
| original = post_rpc.retry.wait | ||||||||||||||||||||||||||||||
| post_rpc.retry.wait = wait_none() | ||||||||||||||||||||||||||||||
| yield | ||||||||||||||||||||||||||||||
| post_rpc.retry.wait = original | ||||||||||||||||||||||||||||||
|
Comment on lines
+125
to
+131
Contributor
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.
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: tests/search/test_client.py
Line: 125-131
Comment:
`autouse=True` at module scope applies this fixture to every test in the file — including `TestHostFromUrl`, `TestWrapRequestError`, and `TestGetClientSingleton` — even though none of them exercise `post_rpc`. The fixture mutates and then restores `post_rpc.retry.wait` on every test run unnecessarily; limiting the scope to the class that actually needs it avoids the spurious global-state churn.
```suggestion
@pytest.fixture()
def _no_backoff_sleep():
"""Strip the exponential wait from post_rpc so retry tests run instantly."""
original = post_rpc.retry.wait
post_rpc.retry.wait = wait_none()
yield
post_rpc.retry.wait = original
```
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| class TestPostRpcBackendErrors: | ||||||||||||||||||||||||||||||
| """post_rpc must turn Google's HTTP-200 error envelope into a retryable error (issue #200).""" | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def test_returns_body_text_on_success(self): | ||||||||||||||||||||||||||||||
| c = MagicMock() | ||||||||||||||||||||||||||||||
| c.post.return_value = _resp(_ok_body()) | ||||||||||||||||||||||||||||||
| assert post_rpc(c, "https://x/y", "ENC") == _ok_body() | ||||||||||||||||||||||||||||||
| assert c.post.call_count == 1 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def test_retries_transient_internal_error_then_succeeds(self): | ||||||||||||||||||||||||||||||
| c = MagicMock() | ||||||||||||||||||||||||||||||
| c.post.side_effect = [_resp(_error_body(13)), _resp(_error_body(13)), _resp(_ok_body())] | ||||||||||||||||||||||||||||||
| assert post_rpc(c, "https://x/y", "ENC") == _ok_body() | ||||||||||||||||||||||||||||||
| assert c.post.call_count == 3 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def test_exhausts_retries_and_raises_backend_error(self): | ||||||||||||||||||||||||||||||
| c = MagicMock() | ||||||||||||||||||||||||||||||
| c.post.return_value = _resp(_error_body(13)) | ||||||||||||||||||||||||||||||
| with pytest.raises(SearchBackendError) as exc_info: | ||||||||||||||||||||||||||||||
| post_rpc(c, "https://x/y", "ENC") | ||||||||||||||||||||||||||||||
| assert exc_info.value.code == 13 | ||||||||||||||||||||||||||||||
| # stop_after_attempt(4) → exactly four POSTs before giving up. | ||||||||||||||||||||||||||||||
| assert c.post.call_count == 4 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def test_non_retryable_code_fails_fast(self): | ||||||||||||||||||||||||||||||
| c = MagicMock() | ||||||||||||||||||||||||||||||
| c.post.return_value = _resp(_error_body(3)) # INVALID_ARGUMENT | ||||||||||||||||||||||||||||||
| with pytest.raises(SearchBackendError) as exc_info: | ||||||||||||||||||||||||||||||
| post_rpc(c, "https://x/y", "ENC") | ||||||||||||||||||||||||||||||
| assert exc_info.value.code == 3 | ||||||||||||||||||||||||||||||
| assert exc_info.value.retryable is False | ||||||||||||||||||||||||||||||
| assert c.post.call_count == 1 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def test_post_receives_freq_body(self): | ||||||||||||||||||||||||||||||
| c = MagicMock() | ||||||||||||||||||||||||||||||
| c.post.return_value = _resp(_ok_body()) | ||||||||||||||||||||||||||||||
| post_rpc(c, "https://x/y", "MYENCODED") | ||||||||||||||||||||||||||||||
| _, kwargs = c.post.call_args | ||||||||||||||||||||||||||||||
| assert kwargs["data"] == "f.req=MYENCODED" | ||||||||||||||||||||||||||||||
| assert kwargs["impersonate"] == "chrome" | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| class TestSearchBackendErrorRetryable: | ||||||||||||||||||||||||||||||
| def test_transient_codes_are_retryable(self): | ||||||||||||||||||||||||||||||
| for code in (13, 14, 4, 8, None, -1): | ||||||||||||||||||||||||||||||
| assert SearchBackendError("x", code=code).retryable is True | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def test_bad_request_codes_are_not_retryable(self): | ||||||||||||||||||||||||||||||
| for code in (3, 5, 7, 9, 11, 16): | ||||||||||||||||||||||||||||||
| assert SearchBackendError("x", code=code).retryable is False | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| class TestGetClientSingleton: | ||||||||||||||||||||||||||||||
| def test_returns_client_instance(self): | ||||||||||||||||||||||||||||||
| from fli.search.client import Client | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
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.
response.raise_for_status()here is dead code.Client.postalready callsraise_for_status()inside its owntryblock — any non-2xx response is caught, wrapped intoSearchHTTPError, and re-raised before theResponseobject is ever returned. The call here will always be a no-op for a 200.Prompt To Fix With AI