(t *testing.T)
| 230 | } |
| 231 | |
| 232 | func TestToolCache_MaxMemoryItems(t *testing.T) { |
| 233 | config := &CacheConfig{ |
| 234 | Enabled: true, |
| 235 | Strategy: CacheStrategyMemory, |
| 236 | TTL: 1 * time.Hour, |
| 237 | MaxMemoryItems: 3, |
| 238 | } |
| 239 | |
| 240 | cache := NewToolCache(config) |
| 241 | ctx := context.Background() |
| 242 | |
| 243 | // 添加4个条目,应该驱逐最旧的 |
| 244 | for i := range 4 { |
| 245 | key := cache.GenerateKey("tool", map[string]any{"index": i}) |
| 246 | err := cache.Set(ctx, key, i, config.TTL) |
| 247 | if err != nil { |
| 248 | t.Fatalf("Failed to set cache: %v", err) |
| 249 | } |
| 250 | time.Sleep(10 * time.Millisecond) // 确保时间戳不同 |
| 251 | } |
| 252 | |
| 253 | // 检查统计 |
| 254 | stats := cache.GetStats() |
| 255 | if stats.Evictions != 1 { |
| 256 | t.Errorf("Expected 1 eviction, got: %d", stats.Evictions) |
| 257 | } |
| 258 | |
| 259 | // 第一个条目应该被驱逐 |
| 260 | firstKey := cache.GenerateKey("tool", map[string]any{"index": 0}) |
| 261 | if _, ok := cache.Get(ctx, firstKey); ok { |
| 262 | t.Fatal("Expected first entry to be evicted") |
| 263 | } |
| 264 | |
| 265 | // 最后一个条目应该存在 |
| 266 | lastKey := cache.GenerateKey("tool", map[string]any{"index": 3}) |
| 267 | if _, ok := cache.Get(ctx, lastKey); !ok { |
| 268 | t.Fatal("Expected last entry to exist") |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | func TestToolCache_Delete(t *testing.T) { |
| 273 | config := &CacheConfig{ |
nothing calls this directly
no test coverage detected