MCPcopy Create free account
hub / github.com/astercloud/aster / TestToolCache_BothStrategy

Function TestToolCache_BothStrategy

pkg/tools/cache_test.go:180–230  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

178}
179
180func TestToolCache_BothStrategy(t *testing.T) {
181 tmpDir := t.TempDir()
182
183 config := &CacheConfig{
184 Enabled: true,
185 Strategy: CacheStrategyBoth,
186 TTL: 1 * time.Hour,
187 CacheDir: tmpDir,
188 }
189
190 cache := NewToolCache(config)
191 ctx := context.Background()
192 key := "test_key"
193 value := "test_value"
194
195 // 设置缓存
196 err := cache.Set(ctx, key, value, config.TTL)
197 if err != nil {
198 t.Fatalf("Failed to set cache: %v", err)
199 }
200
201 // 从内存获取
202 if _, ok := cache.getFromMemory(key); !ok {
203 t.Fatal("Expected memory cache hit")
204 }
205
206 // 从文件获取
207 if _, ok := cache.getFromFile(key); !ok {
208 t.Fatal("Expected file cache hit")
209 }
210
211 // 清空内存缓存
212 cache.memoryMu.Lock()
213 cache.memoryCache = make(map[string]*CacheEntry)
214 cache.memoryMu.Unlock()
215
216 // 应该从文件加载到内存
217 cached, ok := cache.Get(ctx, key)
218 if !ok {
219 t.Fatal("Expected cache hit from file")
220 }
221
222 if cached != value {
223 t.Errorf("Expected '%s', got: %v", value, cached)
224 }
225
226 // 验证已加载到内存
227 if _, ok := cache.getFromMemory(key); !ok {
228 t.Fatal("Expected memory cache hit after loading from file")
229 }
230}
231
232func TestToolCache_MaxMemoryItems(t *testing.T) {
233 config := &CacheConfig{

Callers

nothing calls this directly

Calls 5

SetMethod · 0.95
getFromMemoryMethod · 0.95
getFromFileMethod · 0.95
GetMethod · 0.95
NewToolCacheFunction · 0.85

Tested by

no test coverage detected