|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | + |
| 16 | +"""Tests that HttpRetryOptions governs retries of aiohttp connection errors.""" |
| 17 | + |
| 18 | +import asyncio |
| 19 | +from unittest import mock |
| 20 | + |
| 21 | +from google.oauth2 import credentials |
| 22 | +import pytest |
| 23 | + |
| 24 | +from ... import _api_client as api_client |
| 25 | +from ... import types |
| 26 | + |
| 27 | +aiohttp = pytest.importorskip('aiohttp') |
| 28 | + |
| 29 | + |
| 30 | +def _patch_auth_default(): |
| 31 | + return mock.patch( |
| 32 | + 'google.auth.default', |
| 33 | + return_value=(credentials.Credentials('magic_token'), 'test_project'), |
| 34 | + autospec=True, |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +async def _aiohttp_async_response(status: int): |
| 39 | + """Has to return a coroutine hence async.""" |
| 40 | + response = mock.Mock(spec=aiohttp.ClientResponse) |
| 41 | + response.status = status |
| 42 | + response.headers = {'status-code': str(status)} |
| 43 | + response.text.return_value = 'test' |
| 44 | + return response |
| 45 | + |
| 46 | + |
| 47 | +def _client(retry_options=None): |
| 48 | + return api_client.BaseApiClient( |
| 49 | + vertexai=True, |
| 50 | + project='test_project', |
| 51 | + location='global', |
| 52 | + http_options=types.HttpOptions(retry_options=retry_options), |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +def test_retry_args_retries_aiohttp_transport_errors(): |
| 57 | + """HttpRetryOptions retries aiohttp connection errors, like httpx.""" |
| 58 | + |
| 59 | + retry = api_client.retry_args(types.HttpRetryOptions())['retry'] |
| 60 | + |
| 61 | + assert retry.predicate(aiohttp.ServerDisconnectedError()) |
| 62 | + assert retry.predicate(aiohttp.ClientOSError()) |
| 63 | + assert retry.predicate( |
| 64 | + aiohttp.ClientConnectorError( |
| 65 | + connection_key=aiohttp.client_reqrep.ConnectionKey( |
| 66 | + 'localhost', 80, False, True, None, None, None |
| 67 | + ), |
| 68 | + os_error=OSError(), |
| 69 | + ) |
| 70 | + ) |
| 71 | + |
| 72 | + assert not retry.predicate(aiohttp.ClientPayloadError()) |
| 73 | + assert not retry.predicate(ValueError('not a transport error')) |
| 74 | + |
| 75 | + |
| 76 | +@mock.patch.object(aiohttp.ClientSession, 'request', autospec=True) |
| 77 | +def test_aiohttp_single_attempt_server_disconnected_error_executes_once( |
| 78 | + mock_request, |
| 79 | +): |
| 80 | + """attempts=1 means no retries, so a dropped connection is not resent.""" |
| 81 | + |
| 82 | + api_client.has_aiohttp = True |
| 83 | + |
| 84 | + async def run(): |
| 85 | + mock_request.side_effect = aiohttp.ServerDisconnectedError() |
| 86 | + client = _client(types.HttpRetryOptions(attempts=1)) |
| 87 | + |
| 88 | + with _patch_auth_default(): |
| 89 | + with pytest.raises(aiohttp.ServerDisconnectedError): |
| 90 | + await client.async_request( |
| 91 | + http_method='POST', path='path', request_dict={} |
| 92 | + ) |
| 93 | + assert mock_request.call_count == 1 |
| 94 | + |
| 95 | + asyncio.run(run()) |
| 96 | + |
| 97 | + |
| 98 | +@mock.patch.object(aiohttp.ClientSession, 'request', autospec=True) |
| 99 | +def test_aiohttp_single_attempt_server_disconnected_error_executes_once_at_request_level( |
| 100 | + mock_request, |
| 101 | +): |
| 102 | + """Request-level attempts=1 also disables the implicit aiohttp reconnect.""" |
| 103 | + |
| 104 | + api_client.has_aiohttp = True |
| 105 | + |
| 106 | + async def run(): |
| 107 | + mock_request.side_effect = aiohttp.ServerDisconnectedError() |
| 108 | + client = _client() |
| 109 | + |
| 110 | + with _patch_auth_default(): |
| 111 | + with pytest.raises(aiohttp.ServerDisconnectedError): |
| 112 | + await client.async_request( |
| 113 | + http_method='POST', |
| 114 | + path='path', |
| 115 | + request_dict={}, |
| 116 | + http_options={'retry_options': {'attempts': 1}}, |
| 117 | + ) |
| 118 | + assert mock_request.call_count == 1 |
| 119 | + |
| 120 | + asyncio.run(run()) |
| 121 | + |
| 122 | + |
| 123 | +@mock.patch.object(aiohttp.ClientSession, 'request', autospec=True) |
| 124 | +def test_aiohttp_single_attempt_streamed_server_disconnected_error_executes_once( |
| 125 | + mock_request, |
| 126 | +): |
| 127 | + """The streaming branch honors attempts=1 the same way.""" |
| 128 | + |
| 129 | + api_client.has_aiohttp = True |
| 130 | + |
| 131 | + async def run(): |
| 132 | + mock_request.side_effect = aiohttp.ServerDisconnectedError() |
| 133 | + client = _client(types.HttpRetryOptions(attempts=1)) |
| 134 | + |
| 135 | + with _patch_auth_default(): |
| 136 | + with pytest.raises(aiohttp.ServerDisconnectedError): |
| 137 | + await client.async_request_streamed( |
| 138 | + http_method='POST', path='path', request_dict={} |
| 139 | + ) |
| 140 | + assert mock_request.call_count == 1 |
| 141 | + |
| 142 | + asyncio.run(run()) |
| 143 | + |
| 144 | + |
| 145 | +@mock.patch.object(aiohttp.ClientSession, 'request', autospec=True) |
| 146 | +def test_aiohttp_retries_server_disconnected_error_up_to_attempts( |
| 147 | + mock_request, |
| 148 | +): |
| 149 | + """With retry options, tenacity retries aiohttp errors up to `attempts`.""" |
| 150 | + |
| 151 | + api_client.has_aiohttp = True |
| 152 | + |
| 153 | + async def run(): |
| 154 | + mock_request.side_effect = aiohttp.ServerDisconnectedError() |
| 155 | + client = _client( |
| 156 | + types.HttpRetryOptions( |
| 157 | + attempts=3, initial_delay=0, max_delay=0, jitter=0 |
| 158 | + ) |
| 159 | + ) |
| 160 | + |
| 161 | + with _patch_auth_default(): |
| 162 | + with pytest.raises(aiohttp.ServerDisconnectedError): |
| 163 | + await client.async_request( |
| 164 | + http_method='POST', path='path', request_dict={} |
| 165 | + ) |
| 166 | + assert mock_request.call_count == 3 |
| 167 | + |
| 168 | + asyncio.run(run()) |
| 169 | + |
| 170 | + |
| 171 | +@mock.patch.object(aiohttp.ClientSession, 'request', autospec=True) |
| 172 | +def test_aiohttp_retries_server_disconnected_error_retries_successfully( |
| 173 | + mock_request, |
| 174 | +): |
| 175 | + """A dropped connection is still retried when attempts > 1.""" |
| 176 | + |
| 177 | + api_client.has_aiohttp = True |
| 178 | + |
| 179 | + async def run(): |
| 180 | + mock_request.side_effect = ( |
| 181 | + aiohttp.ServerDisconnectedError(), |
| 182 | + _aiohttp_async_response(200), |
| 183 | + ) |
| 184 | + client = _client( |
| 185 | + types.HttpRetryOptions( |
| 186 | + attempts=2, initial_delay=0, max_delay=0, jitter=0 |
| 187 | + ) |
| 188 | + ) |
| 189 | + |
| 190 | + with _patch_auth_default(): |
| 191 | + response = await client.async_request( |
| 192 | + http_method='GET', path='path', request_dict={} |
| 193 | + ) |
| 194 | + assert mock_request.call_count == 2 |
| 195 | + assert response.headers['status-code'] == '200' |
| 196 | + |
| 197 | + asyncio.run(run()) |
| 198 | + |
| 199 | + |
| 200 | +@mock.patch.object(aiohttp.ClientSession, 'request', autospec=True) |
| 201 | +def test_aiohttp_without_retry_options_reconnects_once(mock_request): |
| 202 | + """Without retry options, the legacy single reconnect is kept.""" |
| 203 | + |
| 204 | + api_client.has_aiohttp = True |
| 205 | + |
| 206 | + async def run(): |
| 207 | + mock_request.side_effect = ( |
| 208 | + aiohttp.ServerDisconnectedError(), |
| 209 | + _aiohttp_async_response(200), |
| 210 | + ) |
| 211 | + client = _client() |
| 212 | + |
| 213 | + with _patch_auth_default(), mock.patch.object( |
| 214 | + asyncio, 'sleep', new=mock.AsyncMock() |
| 215 | + ): |
| 216 | + response = await client.async_request( |
| 217 | + http_method='GET', path='path', request_dict={} |
| 218 | + ) |
| 219 | + assert mock_request.call_count == 2 |
| 220 | + assert response.headers['status-code'] == '200' |
| 221 | + |
| 222 | + asyncio.run(run()) |
0 commit comments