(t *testing.T)
| 506 | } |
| 507 | |
| 508 | func TestToolCache_Cleanup(t *testing.T) { |
| 509 | config := &CacheConfig{ |
| 510 | Enabled: true, |
| 511 | Strategy: CacheStrategyMemory, |
| 512 | TTL: 100 * time.Millisecond, |
| 513 | } |
| 514 | |
| 515 | cache := NewToolCache(config) |
| 516 | ctx := context.Background() |
| 517 | |
| 518 | // 添加多个条目 |
| 519 | for i := range 5 { |
| 520 | key := cache.GenerateKey("tool", map[string]any{"index": i}) |
| 521 | err := cache.Set(ctx, key, i, config.TTL) |
| 522 | if err != nil { |
| 523 | t.Fatalf("Failed to set cache: %v", err) |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | // 验证条目存在 |
| 528 | stats := cache.GetStats() |
| 529 | if stats.ItemCount != 5 { |
| 530 | t.Errorf("Expected 5 items, got: %d", stats.ItemCount) |
| 531 | } |
| 532 | |
| 533 | // 等待过期 |
| 534 | time.Sleep(150 * time.Millisecond) |
| 535 | |
| 536 | // 手动触发清理 |
| 537 | cache.cleanup() |
| 538 | |
| 539 | // 验证已清理 |
| 540 | stats = cache.GetStats() |
| 541 | if stats.ItemCount != 0 { |
| 542 | t.Errorf("Expected 0 items after cleanup, got: %d", stats.ItemCount) |
| 543 | } |
| 544 | |
| 545 | if stats.Evictions != 5 { |
| 546 | t.Errorf("Expected 5 evictions, got: %d", stats.Evictions) |
| 547 | } |
| 548 | } |
nothing calls this directly
no test coverage detected