-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathtest_gemini_context_cache_manager.py
More file actions
689 lines (575 loc) · 24.6 KB
/
test_gemini_context_cache_manager.py
File metadata and controls
689 lines (575 loc) · 24.6 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
# Copyright 2026 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 GeminiContextCacheManager."""
import time
from unittest.mock import AsyncMock
from unittest.mock import MagicMock
from unittest.mock import patch
from google.adk.agents.context_cache_config import ContextCacheConfig
from google.adk.models.cache_metadata import CacheMetadata
from google.adk.models.gemini_context_cache_manager import GeminiContextCacheManager
from google.adk.models.llm_request import LlmRequest
from google.adk.models.llm_response import LlmResponse
from google.genai import Client
from google.genai import types
import pytest
class TestGeminiContextCacheManager:
"""Test suite for GeminiContextCacheManager."""
def setup_method(self):
"""Set up test fixtures."""
mock_client = AsyncMock(spec=Client)
self.manager = GeminiContextCacheManager(mock_client)
self.cache_config = ContextCacheConfig(
cache_intervals=10,
ttl_seconds=1800,
min_tokens=0, # Allow caching for tests
)
def create_llm_request(self, cache_metadata=None, contents_count=3):
"""Helper to create test LlmRequest."""
contents = []
for i in range(contents_count):
contents.append(
types.Content(
role="user", parts=[types.Part(text=f"Test message {i}")]
)
)
# Create tools for testing fingerprinting
tools = [
types.Tool(
function_declarations=[
types.FunctionDeclaration(
name="test_tool",
description="A test tool",
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"param": types.Schema(type=types.Type.STRING)
},
),
)
]
)
]
tool_config = types.ToolConfig(
function_calling_config=types.FunctionCallingConfig(mode="AUTO")
)
return LlmRequest(
model="gemini-2.0-flash",
contents=contents,
config=types.GenerateContentConfig(
system_instruction="Test instruction",
tools=tools,
tool_config=tool_config,
),
cache_config=self.cache_config,
cache_metadata=cache_metadata,
)
def create_cache_metadata(
self, invocations_used=0, expired=False, contents_count=3
):
"""Helper to create test CacheMetadata."""
current_time = time.time()
expire_time = current_time - 300 if expired else current_time + 1800
return CacheMetadata(
cache_name="projects/test/locations/us-central1/cachedContents/test123",
expire_time=expire_time,
fingerprint="test_fingerprint",
invocations_used=invocations_used,
contents_count=contents_count,
created_at=current_time - 600,
)
def test_init(self):
"""Test manager initialization."""
mock_client = MagicMock(spec=Client)
manager = GeminiContextCacheManager(mock_client)
assert manager is not None
assert manager.genai_client == mock_client
async def test_handle_context_caching_no_existing_cache(self):
"""Test handling context caching with no existing cache returns fingerprint-only metadata."""
llm_request = self.create_llm_request(contents_count=5)
with patch.object(
self.manager, "_generate_cache_fingerprint", return_value="test_fp"
):
result = await self.manager.handle_context_caching(llm_request)
assert result is not None
# Should return fingerprint-only metadata (no active cache)
assert result.cache_name is None
assert result.expire_time is None
assert result.invocations_used is None
assert result.created_at is None
assert result.fingerprint == "test_fp"
assert result.contents_count == 5 # Total contents count
# No cache should be created
self.manager.genai_client.aio.caches.create.assert_not_called()
async def test_handle_context_caching_valid_existing_cache(self):
"""Test handling context caching with valid existing cache."""
# Create request with existing valid cache
existing_cache = self.create_cache_metadata(invocations_used=5)
llm_request = self.create_llm_request(cache_metadata=existing_cache)
with patch.object(self.manager, "_is_cache_valid", return_value=True):
result = await self.manager.handle_context_caching(llm_request)
assert result is not None
# Verify that existing cache metadata is preserved (copied)
assert result.cache_name == existing_cache.cache_name
assert (
result.invocations_used == existing_cache.invocations_used
) # Should preserve original invocations_used
assert (
result.expire_time == existing_cache.expire_time
) # Should preserve original expire_time
assert (
result.fingerprint == existing_cache.fingerprint
) # Should preserve original fingerprint
assert (
result.created_at == existing_cache.created_at
) # Should preserve original created_at
# Verify it's a copy, not the same object
assert result is not existing_cache
# Should not create new cache
self.manager.genai_client.aio.caches.create.assert_not_called()
async def test_handle_context_caching_invalid_cache_fingerprint_match(self):
"""Test invalid cache with matching fingerprint creates new cache."""
# Setup mocks
mock_cached_content = AsyncMock()
mock_cached_content.name = (
"projects/test/locations/us-central1/cachedContents/new456"
)
self.manager.genai_client.aio.caches.create = AsyncMock(
return_value=mock_cached_content
)
# Create request with invalid existing cache
existing_cache = self.create_cache_metadata(
invocations_used=15
) # Exceeds cache_intervals
llm_request = self.create_llm_request(cache_metadata=existing_cache)
llm_request.cacheable_contents_token_count = (
2048 # Add token count for cache creation
)
with (
patch.object(self.manager, "_is_cache_valid", return_value=False),
patch.object(self.manager, "cleanup_cache") as mock_cleanup,
patch.object(
self.manager,
"_generate_cache_fingerprint",
return_value="test_fingerprint", # Match old fingerprint
),
):
result = await self.manager.handle_context_caching(llm_request)
assert result is not None
# Should create new cache when fingerprints match
assert (
result.cache_name
== "projects/test/locations/us-central1/cachedContents/new456"
)
mock_cleanup.assert_called_once_with(existing_cache.cache_name)
self.manager.genai_client.aio.caches.create.assert_called_once()
async def test_handle_context_caching_invalid_cache_fingerprint_mismatch(
self,
):
"""Test invalid cache with mismatched fingerprint returns fingerprint-only metadata."""
# Create request with invalid existing cache
existing_cache = self.create_cache_metadata(
invocations_used=15, contents_count=3
) # Exceeds cache_intervals
llm_request = self.create_llm_request(
cache_metadata=existing_cache, contents_count=5
)
with (
patch.object(self.manager, "_is_cache_valid", return_value=False),
patch.object(self.manager, "cleanup_cache") as mock_cleanup,
patch.object(
self.manager,
"_generate_cache_fingerprint",
side_effect=["old_fp", "new_fp"], # Different fingerprints
),
):
result = await self.manager.handle_context_caching(llm_request)
assert result is not None
# Should return fingerprint-only metadata
assert result.cache_name is None
assert result.expire_time is None
assert result.invocations_used is None
assert result.created_at is None
assert result.fingerprint == "new_fp"
assert result.contents_count == 5 # Total contents count
mock_cleanup.assert_called_once_with(existing_cache.cache_name)
self.manager.genai_client.aio.caches.create.assert_not_called()
async def test_is_cache_valid_fingerprint_mismatch(self):
"""Test cache validation with fingerprint mismatch."""
cache_metadata = self.create_cache_metadata()
llm_request = self.create_llm_request(cache_metadata=cache_metadata)
with patch.object(
self.manager,
"_generate_cache_fingerprint",
return_value="different_fingerprint",
):
result = await self.manager._is_cache_valid(llm_request)
assert result is False
async def test_is_cache_valid_expired_cache(self):
"""Test cache validation with expired cache."""
cache_metadata = self.create_cache_metadata(expired=True)
llm_request = self.create_llm_request(cache_metadata=cache_metadata)
with patch.object(
self.manager,
"_generate_cache_fingerprint",
return_value="test_fingerprint",
):
result = await self.manager._is_cache_valid(llm_request)
assert result is False
async def test_is_cache_valid_fingerprint_only_metadata(self):
"""Test cache validation with fingerprint-only metadata (no active cache)."""
# Create fingerprint-only metadata (cache_name is None)
cache_metadata = CacheMetadata(
fingerprint="test_fingerprint",
contents_count=5,
)
llm_request = self.create_llm_request(cache_metadata=cache_metadata)
result = await self.manager._is_cache_valid(llm_request)
assert (
result is False
) # Fingerprint-only metadata is not a valid active cache
async def test_is_cache_valid_cache_intervals_exceeded(self):
"""Test cache validation with max invocations exceeded."""
cache_metadata = self.create_cache_metadata(
invocations_used=15
) # Exceeds cache_intervals=10
llm_request = self.create_llm_request(cache_metadata=cache_metadata)
with patch.object(
self.manager,
"_generate_cache_fingerprint",
return_value="test_fingerprint",
):
result = await self.manager._is_cache_valid(llm_request)
assert result is False
async def test_is_cache_valid_all_checks_pass(self):
"""Test cache validation when all checks pass."""
cache_metadata = self.create_cache_metadata(
invocations_used=5
) # Within cache_intervals=10
llm_request = self.create_llm_request(cache_metadata=cache_metadata)
with patch.object(
self.manager,
"_generate_cache_fingerprint",
return_value="test_fingerprint",
):
result = await self.manager._is_cache_valid(llm_request)
assert result is True
async def test_cleanup_cache(self):
"""Test cache cleanup functionality."""
cache_name = "projects/test/locations/us-central1/cachedContents/test123"
await self.manager.cleanup_cache(cache_name)
self.manager.genai_client.aio.caches.delete.assert_called_once_with(
name=cache_name
)
def test_generate_cache_fingerprint(self):
"""Test cache fingerprint generation includes tools and tool_config."""
llm_request = self.create_llm_request()
cache_contents_count = 2 # Cache all but last content
fingerprint1 = self.manager._generate_cache_fingerprint(
llm_request, cache_contents_count
)
fingerprint2 = self.manager._generate_cache_fingerprint(
llm_request, cache_contents_count
)
# Same request should generate same fingerprint
assert fingerprint1 == fingerprint2
assert isinstance(fingerprint1, str)
assert len(fingerprint1) > 0
# Test that tool_config and tools are included in fingerprint
# Create request without tools/tool_config
llm_request_no_tools = LlmRequest(
model="gemini-2.0-flash",
contents=[types.Content(role="user", parts=[types.Part(text="Test")])],
config=types.GenerateContentConfig(
system_instruction="Test instruction"
),
cache_config=self.cache_config,
)
fingerprint_no_tools = self.manager._generate_cache_fingerprint(
llm_request_no_tools, cache_contents_count
)
# Should be different from request with tools
assert fingerprint1 != fingerprint_no_tools
def test_generate_cache_fingerprint_different_requests(self):
"""Test that different requests generate different fingerprints."""
llm_request1 = self.create_llm_request()
llm_request2 = LlmRequest(
model="gemini-2.0-flash",
contents=[
types.Content(
role="user", parts=[types.Part(text="Different message")]
)
],
config=types.GenerateContentConfig(
system_instruction="Different instruction"
),
cache_config=self.cache_config,
)
cache_contents_count = 2
fingerprint1 = self.manager._generate_cache_fingerprint(
llm_request1, cache_contents_count
)
fingerprint2 = self.manager._generate_cache_fingerprint(
llm_request2, cache_contents_count
)
assert fingerprint1 != fingerprint2
def test_generate_cache_fingerprint_tool_config_variations(self):
"""Test that different tool configs generate different fingerprints."""
# Request with AUTO mode
llm_request_auto = self.create_llm_request()
# Request with NONE mode
tool_config_none = types.ToolConfig(
function_calling_config=types.FunctionCallingConfig(mode="NONE")
)
llm_request_none = LlmRequest(
model="gemini-2.0-flash",
contents=[types.Content(role="user", parts=[types.Part(text="Test")])],
config=types.GenerateContentConfig(
system_instruction="Test instruction",
tools=llm_request_auto.config.tools,
tool_config=tool_config_none,
),
cache_config=self.cache_config,
)
cache_contents_count = 2
fingerprint_auto = self.manager._generate_cache_fingerprint(
llm_request_auto, cache_contents_count
)
fingerprint_none = self.manager._generate_cache_fingerprint(
llm_request_none, cache_contents_count
)
assert fingerprint_auto != fingerprint_none
async def test_populate_cache_metadata_in_response_no_invocations_increment(
self,
):
"""Test that populate_cache_metadata_in_response doesn't increment invocations_used."""
# Create mock response with usage metadata
usage_metadata = MagicMock()
usage_metadata.cached_content_token_count = 800
usage_metadata.prompt_token_count = 1000
llm_response = MagicMock(spec=LlmResponse)
llm_response.usage_metadata = usage_metadata
cache_metadata = self.create_cache_metadata(invocations_used=3)
self.manager.populate_cache_metadata_in_response(
llm_response, cache_metadata
)
# Verify response metadata preserves the original invocations_used (no increment)
updated_metadata = llm_response.cache_metadata
assert (
updated_metadata.invocations_used == 3
) # Should preserve original value
assert updated_metadata.cache_name == cache_metadata.cache_name
assert updated_metadata.fingerprint == cache_metadata.fingerprint
assert updated_metadata.expire_time == cache_metadata.expire_time
assert updated_metadata.created_at == cache_metadata.created_at
async def test_populate_cache_metadata_no_usage_metadata(self):
"""Test populating cache metadata when no usage metadata."""
llm_response = MagicMock(spec=LlmResponse)
llm_response.usage_metadata = None
cache_metadata = self.create_cache_metadata(invocations_used=3)
self.manager.populate_cache_metadata_in_response(
llm_response, cache_metadata
)
# Should still create metadata even without usage info
updated_metadata = llm_response.cache_metadata
assert (
updated_metadata.invocations_used == 3
) # Should preserve original value
assert updated_metadata.cache_name == cache_metadata.cache_name
async def test_create_new_cache_with_proper_ttl(self):
"""Test that new cache is created with proper TTL."""
mock_cached_content = AsyncMock()
mock_cached_content.name = (
"projects/test/locations/us-central1/cachedContents/test123"
)
self.manager.genai_client.aio.caches.create = AsyncMock(
return_value=mock_cached_content
)
llm_request = self.create_llm_request()
cache_contents_count = max(0, len(llm_request.contents) - 1)
with patch.object(
self.manager, "_generate_cache_fingerprint", return_value="test_fp"
):
await self.manager._create_gemini_cache(llm_request, cache_contents_count)
# Verify cache creation call includes TTL
create_call = self.manager.genai_client.aio.caches.create.call_args
assert create_call is not None
cache_config = create_call[1]["config"]
assert cache_config.ttl == "1800s" # From cache_config
def test_all_but_last_content_caching(self):
"""Test that cache content counting works correctly."""
# Test with multiple contents
llm_request_multi = self.create_llm_request(contents_count=5)
# Test cache contents count calculation
cache_contents_count = max(0, len(llm_request_multi.contents) - 1)
assert cache_contents_count == 4 # 5 contents, so cache 4 contents
# Test with single content
llm_request_single = self.create_llm_request(contents_count=1)
single_cache_contents_count = max(0, len(llm_request_single.contents) - 1)
assert single_cache_contents_count == 0 # Single content, cache 0 contents
def test_edge_cases(self):
"""Test various edge cases."""
# Test with None cache_config
llm_request_no_config = LlmRequest(
model="gemini-2.0-flash",
contents=[types.Content(role="user", parts=[types.Part(text="Test")])],
config=types.GenerateContentConfig(system_instruction="Test"),
cache_config=None,
)
# Should handle gracefully
cache_contents_count = 2
fingerprint = self.manager._generate_cache_fingerprint(
llm_request_no_config, cache_contents_count
)
assert isinstance(fingerprint, str)
# Test with empty contents
llm_request_empty = LlmRequest(
model="gemini-2.0-flash",
contents=[],
config=types.GenerateContentConfig(system_instruction="Test"),
cache_config=self.cache_config,
)
empty_cache_contents_count = 0
fingerprint = self.manager._generate_cache_fingerprint(
llm_request_empty, empty_cache_contents_count
)
assert isinstance(fingerprint, str)
def test_parameter_types_enforcement(self):
"""Test that method calls with correct parameter types work properly."""
# Create proper objects
usage_metadata = MagicMock()
usage_metadata.cached_content_token_count = 500
usage_metadata.prompt_token_count = 1000
llm_response = MagicMock(spec=LlmResponse)
llm_response.usage_metadata = usage_metadata
cache_metadata = self.create_cache_metadata(invocations_used=3)
# This should work fine (correct types and order)
self.manager.populate_cache_metadata_in_response(
llm_response, cache_metadata
)
updated_metadata = llm_response.cache_metadata
assert updated_metadata.invocations_used == 3 # No increment in this method
# Document expected types for integration tests
assert isinstance(cache_metadata, CacheMetadata)
assert hasattr(
llm_response, "usage_metadata"
) # LlmResponse should have this
assert not hasattr(
cache_metadata, "usage_metadata"
) # CacheMetadata should NOT have this
def create_llm_request_with_token_count(
self, token_count=None, cache_metadata=None
):
"""Helper to create LlmRequest with cacheable_contents_token_count."""
llm_request = self.create_llm_request(cache_metadata=cache_metadata)
llm_request.cacheable_contents_token_count = token_count
return llm_request
async def test_cache_creation_with_sufficient_token_count(self):
"""Test that fingerprint-only metadata is returned even with sufficient tokens."""
# With new prefix matching logic, no cache is created without existing metadata
# Create request with sufficient token count
llm_request = self.create_llm_request_with_token_count(token_count=2048)
with patch.object(
self.manager, "_generate_cache_fingerprint", return_value="test_fp"
):
result = await self.manager.handle_context_caching(llm_request)
# Should return fingerprint-only metadata (no cache creation)
assert result is not None
assert result.cache_name is None # Fingerprint-only state
assert result.fingerprint == "test_fp"
assert result.contents_count == 3
self.manager.genai_client.aio.caches.create.assert_not_called()
async def test_cache_creation_with_insufficient_token_count(self):
"""Test that fingerprint-only metadata is returned even with insufficient tokens."""
# Set higher minimum token requirement
self.manager.cache_config = ContextCacheConfig(
cache_intervals=10,
ttl_seconds=1800,
min_tokens=2048,
)
# Create request with insufficient token count
llm_request = self.create_llm_request_with_token_count(token_count=1024)
llm_request.cache_config = self.manager.cache_config
with patch.object(
self.manager, "_generate_cache_fingerprint", return_value="test_fp"
):
result = await self.manager.handle_context_caching(llm_request)
# Should return fingerprint-only metadata
assert result is not None
assert result.cache_name is None
assert result.fingerprint == "test_fp"
self.manager.genai_client.aio.caches.create.assert_not_called()
async def test_cache_creation_without_token_count(self):
"""Test that fingerprint-only metadata is returned even without token count."""
# Create request without token count (initial request)
llm_request = self.create_llm_request_with_token_count(token_count=None)
with patch.object(
self.manager, "_generate_cache_fingerprint", return_value="test_fp"
):
result = await self.manager.handle_context_caching(llm_request)
# Should return fingerprint-only metadata
assert result is not None
assert result.cache_name is None
assert result.fingerprint == "test_fp"
self.manager.genai_client.aio.caches.create.assert_not_called()
async def test_create_http_options_passthrough(self):
"""Test that create_http_options is passed through to cache creation config."""
mock_cached_content = AsyncMock()
mock_cached_content.name = (
"projects/test/locations/us-central1/cachedContents/test123"
)
self.manager.genai_client.aio.caches.create = AsyncMock(
return_value=mock_cached_content
)
# Create config with http_options (e.g. 10s timeout)
http_options = types.HttpOptions(timeout=10000)
cache_config_with_timeout = ContextCacheConfig(
cache_intervals=10,
ttl_seconds=1800,
min_tokens=0,
create_http_options=http_options,
)
llm_request = self.create_llm_request()
llm_request.cache_config = cache_config_with_timeout
cache_contents_count = max(0, len(llm_request.contents) - 1)
with patch.object(
self.manager, "_generate_cache_fingerprint", return_value="test_fp"
):
await self.manager._create_gemini_cache(llm_request, cache_contents_count)
# Verify cache creation call includes http_options
create_call = self.manager.genai_client.aio.caches.create.call_args
assert create_call is not None
cache_config = create_call[1]["config"]
assert cache_config.http_options is not None
assert cache_config.http_options.timeout == 10000
async def test_create_without_http_options(self):
"""Test that cache creation works without create_http_options."""
mock_cached_content = AsyncMock()
mock_cached_content.name = (
"projects/test/locations/us-central1/cachedContents/test123"
)
self.manager.genai_client.aio.caches.create = AsyncMock(
return_value=mock_cached_content
)
llm_request = self.create_llm_request()
cache_contents_count = max(0, len(llm_request.contents) - 1)
with patch.object(
self.manager, "_generate_cache_fingerprint", return_value="test_fp"
):
await self.manager._create_gemini_cache(llm_request, cache_contents_count)
# Verify cache creation call does not include http_options
create_call = self.manager.genai_client.aio.caches.create.call_args
assert create_call is not None
cache_config = create_call[1]["config"]
assert cache_config.http_options is None