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

Function TestToolCache_Cleanup

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

Source from the content-addressed store, hash-verified

506}
507
508func 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}

Callers

nothing calls this directly

Calls 5

GenerateKeyMethod · 0.95
SetMethod · 0.95
GetStatsMethod · 0.95
cleanupMethod · 0.95
NewToolCacheFunction · 0.85

Tested by

no test coverage detected