@@ -5,9 +5,13 @@ import (
55 "path/filepath"
66 "strings"
77 "testing"
8+ "time"
89
910 candle_binding "github.com/vllm-project/semantic-router/candle-binding"
1011 "github.com/vllm-project/semantic-router/src/semantic-router/pkg/cache"
12+ "github.com/vllm-project/semantic-router/src/semantic-router/pkg/metrics"
13+
14+ "github.com/prometheus/client_golang/prometheus/testutil"
1115
1216 . "github.com/onsi/ginkgo/v2"
1317 . "github.com/onsi/gomega"
@@ -501,6 +505,32 @@ development:
501505 Expect (response ).To (Equal ([]byte ("response" )))
502506 })
503507
508+ It ("should update cache entries metric when cleanup occurs during UpdateWithResponse" , func () {
509+ // Reset gauge defensively so the assertion stands alone even if other specs fail early
510+ metrics .UpdateCacheEntries ("memory" , 0 )
511+
512+ Expect (inMemoryCache .Close ()).NotTo (HaveOccurred ())
513+ inMemoryCache = cache .NewInMemoryCache (cache.InMemoryCacheOptions {
514+ Enabled : true ,
515+ SimilarityThreshold : 0.8 ,
516+ MaxEntries : 100 ,
517+ TTLSeconds : 1 ,
518+ })
519+
520+ err := inMemoryCache .AddPendingRequest ("expired-request-id" , "test-model" , "stale query" , []byte ("request" ))
521+ Expect (err ).NotTo (HaveOccurred ())
522+ Expect (testutil .ToFloat64 (metrics .CacheEntriesTotal .WithLabelValues ("memory" ))).To (Equal (float64 (1 )))
523+
524+ // Wait for TTL to expire before triggering the update path
525+ time .Sleep (2 * time .Second )
526+
527+ err = inMemoryCache .UpdateWithResponse ("expired-request-id" , []byte ("response" ))
528+ Expect (err ).To (HaveOccurred ())
529+ Expect (err .Error ()).To (ContainSubstring ("no pending request" ))
530+
531+ Expect (testutil .ToFloat64 (metrics .CacheEntriesTotal .WithLabelValues ("memory" ))).To (BeZero ())
532+ })
533+
504534 It ("should respect similarity threshold" , func () {
505535 // Add entry with a very high similarity threshold
506536 highThresholdOptions := cache.InMemoryCacheOptions {
0 commit comments