11package tsdb
22
33import (
4+ "bytes"
45 "fmt"
56 "strings"
67 "sync"
78 "testing"
89 "time"
910
1011 "github.com/prometheus/client_golang/prometheus"
12+ "github.com/prometheus/client_golang/prometheus/testutil"
1113 "github.com/stretchr/testify/require"
1214 "go.uber.org/atomic"
1315)
@@ -88,7 +90,8 @@ func TestFifoCacheExpire(t *testing.T) {
8890
8991 for name , c := range tc {
9092 t .Run (name , func (t * testing.T ) {
91- m := NewPostingCacheMetrics (prometheus .NewPedanticRegistry ())
93+ r := prometheus .NewPedanticRegistry ()
94+ m := NewPostingCacheMetrics (r )
9295 timeNow := time .Now
9396 cache := newFifoCache [int ](c .cfg , "test" , m , timeNow )
9497
@@ -118,6 +121,16 @@ func TestFifoCacheExpire(t *testing.T) {
118121
119122 require .Equal (t , c .expectedFinalItems , totalCacheSize )
120123
124+ if c .expectedFinalItems != numberOfKeys {
125+ err := testutil .GatherAndCompare (r , bytes .NewBufferString (fmt .Sprintf (`
126+ # HELP cortex_ingester_expanded_postings_cache_evicts Total number of evictions in the cache, excluding items that got evicted due to TTL.
127+ # TYPE cortex_ingester_expanded_postings_cache_evicts counter
128+ cortex_ingester_expanded_postings_cache_evicts{cache="test",reason="full"} %v
129+ ` , numberOfKeys - c .expectedFinalItems )), "cortex_ingester_expanded_postings_cache_evicts" )
130+ require .NoError (t , err )
131+
132+ }
133+
121134 if c .ttlExpire {
122135 cache .timeNow = func () time.Time {
123136 return timeNow ().Add (2 * c .cfg .Ttl )
@@ -135,6 +148,29 @@ func TestFifoCacheExpire(t *testing.T) {
135148 // Total Size Updated
136149 require .Equal (t , originalSize + 10 , cache .cachedBytes )
137150 }
151+
152+ err := testutil .GatherAndCompare (r , bytes .NewBufferString (fmt .Sprintf (`
153+ # HELP cortex_ingester_expanded_postings_cache_evicts Total number of evictions in the cache, excluding items that got evicted due to TTL.
154+ # TYPE cortex_ingester_expanded_postings_cache_evicts counter
155+ cortex_ingester_expanded_postings_cache_evicts{cache="test",reason="expired"} %v
156+ ` , numberOfKeys )), "cortex_ingester_expanded_postings_cache_evicts" )
157+ require .NoError (t , err )
158+
159+ cache .timeNow = func () time.Time {
160+ return timeNow ().Add (5 * c .cfg .Ttl )
161+ }
162+
163+ cache .getPromiseForKey ("newKwy" , func () (int , int64 , error ) {
164+ return 2 , 18 , nil
165+ })
166+
167+ // Should expire all keys again as ttl is expired
168+ err = testutil .GatherAndCompare (r , bytes .NewBufferString (fmt .Sprintf (`
169+ # HELP cortex_ingester_expanded_postings_cache_evicts Total number of evictions in the cache, excluding items that got evicted due to TTL.
170+ # TYPE cortex_ingester_expanded_postings_cache_evicts counter
171+ cortex_ingester_expanded_postings_cache_evicts{cache="test",reason="expired"} %v
172+ ` , numberOfKeys * 2 )), "cortex_ingester_expanded_postings_cache_evicts" )
173+ require .NoError (t , err )
138174 }
139175 })
140176 }
0 commit comments