-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathtest_gemini_backoff.py
More file actions
99 lines (79 loc) · 3.38 KB
/
test_gemini_backoff.py
File metadata and controls
99 lines (79 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Copyright 2025 Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for Gemini provider exponential backoff."""
from unittest import mock
from absl.testing import absltest
from langextract.core import exceptions
from langextract.providers import gemini
class TestGeminiBackoff(absltest.TestCase):
@mock.patch("google.genai.Client")
@mock.patch("time.sleep") # Mock sleep to speed up tests
def test_gemini_retry_on_429(self, mock_sleep, mock_client_class):
"""Test that Gemini retries on 429 errors and eventually succeeds."""
mock_client = mock.Mock()
mock_client_class.return_value = mock_client
# Simulate one 429 error followed by a success
mock_response = mock.Mock()
mock_response.text = '{"result": "success"}'
mock_client.models.generate_content.side_effect = [
Exception("429 RESOURCE_EXHAUSTED"),
mock_response
]
model = gemini.GeminiLanguageModel(
api_key="test-key",
max_retries=3
)
results = list(model.infer(["Test prompt"]))
self.assertEqual(len(results), 1)
self.assertEqual(results[0][0].output, '{"result": "success"}')
self.assertEqual(mock_client.models.generate_content.call_count, 2)
mock_sleep.assert_called_once()
@mock.patch("google.genai.Client")
@mock.patch("time.sleep")
def test_gemini_max_retries_exceeded(self, mock_sleep, mock_client_class):
"""Test that Gemini fails after exceeding max retries."""
mock_client = mock.Mock()
mock_client_class.return_value = mock_client
# Simulate continuous 429 errors
mock_client.models.generate_content.side_effect = Exception("429 RESOURCE_EXHAUSTED")
model = gemini.GeminiLanguageModel(
api_key="test-key",
max_retries=2
)
with self.assertRaises(exceptions.InferenceRuntimeError) as cm:
list(model.infer(["Test prompt"]))
self.assertIn("Gemini API error", str(cm.exception))
self.assertIn("429", str(cm.exception))
# 1 initial call + 2 retries = 3 calls
self.assertEqual(mock_client.models.generate_content.call_count, 3)
self.assertEqual(mock_sleep.call_count, 2)
@mock.patch("google.genai.Client")
@mock.patch("time.sleep")
def test_gemini_no_retry_on_other_errors(self, mock_sleep, mock_client_class):
"""Test that Gemini does not retry on non-429 errors."""
mock_client = mock.Mock()
mock_client_class.return_value = mock_client
# Simulate a non-429 error
mock_client.models.generate_content.side_effect = Exception("500 Internal Server Error")
model = gemini.GeminiLanguageModel(
api_key="test-key",
max_retries=3
)
with self.assertRaises(exceptions.InferenceRuntimeError) as cm:
list(model.infer(["Test prompt"]))
self.assertIn("500", str(cm.exception))
self.assertEqual(mock_client.models.generate_content.call_count, 1)
mock_sleep.assert_not_called()
if __name__ == "__main__":
absltest.main()