@@ -10,12 +10,12 @@ import (
1010 "testing"
1111
1212 "github.com/prometheus/client_golang/prometheus"
13- "github.com/prometheus/client_golang/prometheus/promauto"
1413 "github.com/prometheus/client_golang/prometheus/testutil"
1514)
1615
1716func TestNewFailedQueryCache (t * testing.T ) {
18- cache , err := NewFailedQueryCache (2 )
17+ reg := prometheus .NewRegistry ()
18+ cache , err := NewFailedQueryCache (2 , reg )
1919 if cache == nil {
2020 t .Fatalf ("Expected cache to be created, but got nil" )
2121 }
@@ -25,7 +25,8 @@ func TestNewFailedQueryCache(t *testing.T) {
2525}
2626
2727func TestUpdateFailedQueryCache (t * testing.T ) {
28- cache , _ := NewFailedQueryCache (2 )
28+ reg := prometheus .NewRegistry ()
29+ cache , _ := NewFailedQueryCache (2 , reg )
2930
3031 tests := []struct {
3132 name string
@@ -120,7 +121,8 @@ func TestUpdateFailedQueryCache(t *testing.T) {
120121
121122// TestQueryHitCache tests the QueryHitCache method
122123func TestQueryHitCache (t * testing.T ) {
123- cache , _ := NewFailedQueryCache (2 )
124+ reg := prometheus .NewRegistry ()
125+ cache , _ := NewFailedQueryCache (2 , reg )
124126 lruCache := cache .lruCache
125127
126128 lruCache .Add ("test_query" , 100 )
@@ -202,26 +204,83 @@ func TestQueryHitCache(t *testing.T) {
202204
203205func TestCacheCounterVec (t * testing.T ) {
204206 reg := prometheus .NewRegistry ()
205- cachedHits := promauto .With (reg ).NewCounterVec (prometheus.CounterOpts {
206- Name : "cached_failed_queries_count" ,
207- Help : "Total number of queries that hit the failed query cache." ,
208- }, []string {"total" })
209-
210- cachedHits .WithLabelValues ("total" ).Inc ()
211- expectedValue := 1.0
212- value := testutil .ToFloat64 (cachedHits .WithLabelValues ("total" ))
213- if value != expectedValue {
214- t .Errorf ("expected %v, got %v" , expectedValue , value )
215- }
216- cachedHits .WithLabelValues ("total" ).Inc ()
217- expectedValue = 2.0
218- value = testutil .ToFloat64 (cachedHits .WithLabelValues ("total" ))
219- if value != expectedValue {
220- t .Errorf ("expected %v, got %v" , expectedValue , value )
207+ cache , _ := NewFailedQueryCache (2 , reg )
208+ lruCache := cache .lruCache
209+
210+ lruCache .Add ("test_query" , 100 )
211+ lruCache .Add (" tes t query " , 100 )
212+
213+ tests := []struct {
214+ name string
215+ query url.Values
216+ expectedCounter int
217+ }{
218+ {
219+ name : "Cache miss" ,
220+ query : url.Values {
221+ "start" : {"100" },
222+ "end" : {"2000" },
223+ "query" : {"miss_query_counter_test" },
224+ },
225+ expectedCounter : 0 ,
226+ }, {
227+ name : "Cache hit" ,
228+ query : url.Values {
229+ "start" : {"100" },
230+ "end" : {"200" },
231+ "query" : {"test_query" },
232+ },
233+ expectedCounter : 1 ,
234+ },
235+ {
236+ name : "Cache miss" ,
237+ query : url.Values {
238+ "start" : {"100" },
239+ "end" : {"200" },
240+ "query" : {"miss" },
241+ },
242+ expectedCounter : 1 ,
243+ },
244+
245+ {
246+ name : "Cache miss due to shorter range length" ,
247+ query : url.Values {
248+ "start" : {"100" },
249+ "end" : {"150" },
250+ "query" : {"test_query" },
251+ },
252+ expectedCounter : 1 ,
253+ },
254+
255+ {
256+ name : "Cache hit whitespace" ,
257+ query : url.Values {
258+ "start" : {"100" },
259+ "end" : {"200" },
260+ "query" : {" \n \t tes \t t \n query \t \n " },
261+ },
262+ expectedCounter : 2 ,
263+ },
264+
265+ {
266+ name : "Cache miss whitespace" ,
267+ query : url.Values {
268+ "start" : {"100" },
269+ "end" : {"200" },
270+ "query" : {" \n \t te s \t t \n query \t \n " },
271+ },
272+ expectedCounter : 2 ,
273+ },
221274 }
222- expectedValue = 0.0
223- value = testutil .ToFloat64 (cachedHits .WithLabelValues ("incorrect_label" ))
224- if value != expectedValue {
225- t .Errorf ("expected %v, got %v" , expectedValue , value )
275+
276+ for _ , tt := range tests {
277+ t .Run (tt .name , func (t * testing.T ) {
278+ cache .QueryHitCache (tt .query )
279+ result := int (testutil .ToFloat64 (cache .cachedHits .WithLabelValues ()))
280+ if result != tt .expectedCounter {
281+ t .Errorf ("expected counter value to be %v, got %v" , tt .expectedCounter , result )
282+ }
283+
284+ })
226285 }
227286}
0 commit comments