|
| 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 | + |
| 17 | +"""Tests for how `HttpRetryOptions` applies to the Interactions API.""" |
| 18 | + |
| 19 | +from unittest import mock |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +from ... import Client, _api_client, types |
| 24 | +from ..._gaos import google_genai |
| 25 | +from ..._gaos.lib import compat_errors |
| 26 | + |
| 27 | +if _api_client._get_http_client_backend() == "httpx2": |
| 28 | + from httpx2 import Client as HTTPClient, Request, Response # pylint: disable=g-import-not-at-top |
| 29 | +else: |
| 30 | + from httpx import Client as HTTPClient, Request, Response # pylint: disable=g-import-not-at-top |
| 31 | + |
| 32 | +ENV_VARS = [ |
| 33 | + "GOOGLE_CLOUD_PROJECT", |
| 34 | + "GEMINI_API_KEY", |
| 35 | + "GOOGLE_API_KEY", |
| 36 | + "GOOGLE_CLOUD_LOCATION", |
| 37 | +] |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture(autouse=True) |
| 41 | +def clear_env_vars(monkeypatch): |
| 42 | + for var in ENV_VARS: |
| 43 | + monkeypatch.delenv(var, raising=False) |
| 44 | + |
| 45 | + |
| 46 | +def _rate_limited(): |
| 47 | + return Response( |
| 48 | + 429, |
| 49 | + request=Request('POST', ''), |
| 50 | + headers={'retry-after-ms': '1', 'content-type': 'application/json'}, |
| 51 | + content='{"error": {"code": "too_many_requests", "message": "quota"}}', |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +def _completed(): |
| 56 | + return Response( |
| 57 | + 200, |
| 58 | + request=Request('POST', ''), |
| 59 | + headers={'content-type': 'application/json'}, |
| 60 | + content='{"status": "completed"}', |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize( |
| 65 | + 'attempts, expected_retries', |
| 66 | + [ |
| 67 | + (None, google_genai._DEFAULT_MAX_RETRIES + 1), |
| 68 | + (0, 0), |
| 69 | + (1, 0), |
| 70 | + (2, 1), |
| 71 | + (5, 4), |
| 72 | + ], |
| 73 | +) |
| 74 | +def test_translate_retry_config_counts_the_original_request( |
| 75 | + attempts, expected_retries |
| 76 | +): |
| 77 | + retry_options = None |
| 78 | + if attempts is not None: |
| 79 | + retry_options = types.HttpRetryOptions(attempts=attempts) |
| 80 | + http_options = types.HttpOptions(retry_options=retry_options) |
| 81 | + |
| 82 | + config = google_genai._translate_retry_config(http_options) |
| 83 | + |
| 84 | + assert config.max_retries == expected_retries |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.parametrize('attempts', [0, 1]) |
| 88 | +def test_interactions_do_not_retry_when_attempts_disable_retries( |
| 89 | + monkeypatch, attempts |
| 90 | +): |
| 91 | + monkeypatch.setenv('GOOGLE_API_KEY', 'test-api-key') |
| 92 | + client = Client( |
| 93 | + http_options=types.HttpOptions( |
| 94 | + retry_options=types.HttpRetryOptions(attempts=attempts) |
| 95 | + ) |
| 96 | + ) |
| 97 | + |
| 98 | + with mock.patch.object(HTTPClient, 'send') as mock_send: |
| 99 | + mock_send.side_effect = [_rate_limited(), _rate_limited()] |
| 100 | + with pytest.raises(compat_errors.RateLimitError): |
| 101 | + client.interactions.create(model='gemini-1.5-flash', input='Hello') |
| 102 | + assert mock_send.call_count == 1 |
| 103 | + |
| 104 | + |
| 105 | +def test_interactions_retry_up_to_attempts(monkeypatch): |
| 106 | + monkeypatch.setenv('GOOGLE_API_KEY', 'test-api-key') |
| 107 | + client = Client( |
| 108 | + http_options=types.HttpOptions( |
| 109 | + retry_options=types.HttpRetryOptions(attempts=3) |
| 110 | + ) |
| 111 | + ) |
| 112 | + |
| 113 | + with mock.patch.object(HTTPClient, 'send') as mock_send: |
| 114 | + mock_send.side_effect = [_rate_limited(), _rate_limited(), _completed()] |
| 115 | + client.interactions.create(model='gemini-1.5-flash', input='Hello') |
| 116 | + assert mock_send.call_count == 3 |
| 117 | + |
| 118 | + with mock.patch.object(HTTPClient, 'send') as mock_send: |
| 119 | + mock_send.side_effect = [_rate_limited()] * 4 |
| 120 | + with pytest.raises(compat_errors.RateLimitError): |
| 121 | + client.interactions.create(model='gemini-1.5-flash', input='Hello') |
| 122 | + assert mock_send.call_count == 3 |
0 commit comments