Add preference for non 404 responses fix test RC.

Fixed race condition with the 404 test.
This commit is contained in:
czaky 2024-05-17 12:55:00 +00:00
parent c302f113b0
commit 5fe7e42d1a
2 changed files with 22 additions and 3 deletions

View file

@ -222,6 +222,7 @@ class AsyncParallelTransport(httpx.AsyncBaseTransport):
self,
request: httpx.Request,
) -> httpx.Response:
# pylint: disable=too-many-branches
"""Issue parallel requests to all sub-transports.
Return the response of the first completed.
@ -254,7 +255,9 @@ class AsyncParallelTransport(httpx.AsyncBaseTransport):
for task in done:
try:
result = task.result()
if not result.is_error or result.status_code == 404:
if not result.is_error:
response = result
elif result.status_code == 404 and response is None:
response = result
elif not error_response:
self._logger.warning("Error response: %s for %s", result.status_code, request.url)

View file

@ -63,9 +63,9 @@ class TestClient(SearxTestCase):
@patch(
'searx.network.client.AsyncProxyTransportFixed.handle_async_request',
side_effect=[httpx.Response(404, html="<html/>"), httpx.Response(200, html="<html/>")],
side_effect=[httpx.Response(404, html="<html/>"), httpx.Response(404, html="<html/>")],
)
async def test_parallel_transport_404(self, handler_mock: Mock):
async def test_parallel_transport_404_404(self, handler_mock: Mock):
t = client.get_transport(
proxy_urls=["socks5h://local:1080", "socks5h://local:1180"],
proxy_request_redundancy=2,
@ -77,6 +77,22 @@ class TestClient(SearxTestCase):
self.assertEqual(handler_mock.call_count, 2)
self.assertEqual(response.status_code, 404)
@patch(
'searx.network.client.AsyncProxyTransportFixed.handle_async_request',
side_effect=[httpx.Response(200, html="<html/>"), httpx.Response(404, html="<html/>")],
)
async def test_parallel_transport_404_200(self, handler_mock: Mock):
t = client.get_transport(
proxy_urls=["socks5h://local:1080", "socks5h://local:1180"],
proxy_request_redundancy=2,
)
self.assertTrue(isinstance(t, client.AsyncParallelTransport))
request = httpx.Request(url="http://wiki.com", method="GET")
response = await t.handle_async_request(request)
handler_mock.assert_called_with(request)
self.assertEqual(handler_mock.call_count, 2)
self.assertEqual(response.status_code, 200)
@patch(
'searx.network.client.AsyncProxyTransportFixed.handle_async_request',
side_effect=[httpx.Response(403, html="<html/>"), httpx.Response(403, html="<html/>")],