(t *testing.T)
| 50 | } |
| 51 | |
| 52 | func TestToolCache_MemoryCache(t *testing.T) { |
| 53 | config := &CacheConfig{ |
| 54 | Enabled: true, |
| 55 | Strategy: CacheStrategyMemory, |
| 56 | TTL: 1 * time.Second, |
| 57 | MaxMemoryItems: 10, |
| 58 | } |
| 59 | |
| 60 | cache := NewToolCache(config) |
| 61 | |
| 62 | // 测试设置和获取 |
| 63 | ctx := context.Background() |
| 64 | key := "test_key" |
| 65 | value := map[string]any{"data": "test"} |
| 66 | |
| 67 | err := cache.Set(ctx, key, value, config.TTL) |
| 68 | if err != nil { |
| 69 | t.Fatalf("Failed to set cache: %v", err) |
| 70 | } |
| 71 | |
| 72 | // 获取缓存 |
| 73 | cached, ok := cache.Get(ctx, key) |
| 74 | if !ok { |
| 75 | t.Fatal("Expected cache hit") |
| 76 | } |
| 77 | |
| 78 | cachedMap, ok := cached.(map[string]any) |
| 79 | if !ok { |
| 80 | t.Fatal("Expected map[string]any") |
| 81 | } |
| 82 | |
| 83 | if cachedMap["data"] != "test" { |
| 84 | t.Errorf("Expected 'test', got: %v", cachedMap["data"]) |
| 85 | } |
| 86 | |
| 87 | // 检查统计 |
| 88 | stats := cache.GetStats() |
| 89 | if stats.Hits != 1 { |
| 90 | t.Errorf("Expected 1 hit, got: %d", stats.Hits) |
| 91 | } |
| 92 | |
| 93 | if stats.Sets != 1 { |
| 94 | t.Errorf("Expected 1 set, got: %d", stats.Sets) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func TestToolCache_Expiration(t *testing.T) { |
| 99 | config := &CacheConfig{ |
nothing calls this directly
no test coverage detected