Skip to content

Commit c93d0fd

Browse files
committed
test: add test for metrics fix for UpdateWithResponse
Signed-off-by: cryo <[email protected]>
1 parent 2c5ee46 commit c93d0fd

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/semantic-router/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ require (
5959
github.com/json-iterator/go v1.1.12 // indirect
6060
github.com/kr/pretty v0.3.1 // indirect
6161
github.com/kr/text v0.2.0 // indirect
62+
github.com/kylelemons/godebug v1.1.0 // indirect
6263
github.com/milvus-io/milvus-proto/go-api/v2 v2.4.10-0.20240819025435-512e3b98866a // indirect
6364
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
6465
github.com/modern-go/reflect2 v1.0.2 // indirect

src/semantic-router/pkg/cache/cache_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)