|
3 | 3 | """Tests for Purview cache provider.""" |
4 | 4 |
|
5 | 5 | import asyncio |
| 6 | +from unittest.mock import patch |
6 | 7 |
|
7 | 8 | from agent_framework_purview._cache import ( |
8 | 9 | InMemoryCacheProvider, |
@@ -162,6 +163,30 @@ async def test_eviction_with_stale_heap_entries(self) -> None: |
162 | 163 | result = await cache.get("key1") |
163 | 164 | assert result is not None |
164 | 165 |
|
| 166 | + async def test_growing_entry_at_capacity_maintains_size_and_evicts_other_entry(self) -> None: |
| 167 | + """Test that updating an entry to a larger size at capacity correctly tracks size and evicts.""" |
| 168 | + cache = InMemoryCacheProvider(max_size_bytes=20) |
| 169 | + |
| 170 | + with patch("agent_framework_purview._cache.time.time", return_value=1000.0): |
| 171 | + # JSON string quotes add 2 bytes to string length. |
| 172 | + await cache.set("older", "a" * 6, ttl_seconds=10) # 8 bytes |
| 173 | + await cache.set("newer", "b" * 6, ttl_seconds=20) # 8 bytes |
| 174 | + assert cache._current_size_bytes == 16 |
| 175 | + |
| 176 | + # Replace 'older' with 16-byte value. Total needed = 16. |
| 177 | + # 'older' was 8 bytes. If 'older' was not removed from cache before eviction, |
| 178 | + # eviction pops 'older' from heap and subtracts its size again. |
| 179 | + await cache.set("older", "c" * 14, ttl_seconds=30) # 16 bytes |
| 180 | + |
| 181 | + assert await cache.get("newer") is None |
| 182 | + assert await cache.get("older") == "c" * 14 |
| 183 | + assert cache._current_size_bytes == 16 |
| 184 | + assert sum(entry[2] for entry in cache._cache.values()) == 16 |
| 185 | + |
| 186 | + await cache.remove("older") |
| 187 | + assert cache._current_size_bytes == 0 |
| 188 | + |
| 189 | + |
165 | 190 |
|
166 | 191 | class TestCreateProtectionScopesCacheKey: |
167 | 192 | """Test cache key generation for ProtectionScopesRequest.""" |
|
0 commit comments