1
1
package tsdb
2
2
3
3
import (
4
+ "bytes"
4
5
"fmt"
5
6
"strings"
6
7
"sync"
7
8
"testing"
8
9
"time"
9
10
10
11
"github.com/prometheus/client_golang/prometheus"
12
+ "github.com/prometheus/client_golang/prometheus/testutil"
11
13
"github.com/stretchr/testify/require"
12
14
"go.uber.org/atomic"
13
15
)
@@ -88,7 +90,8 @@ func TestFifoCacheExpire(t *testing.T) {
88
90
89
91
for name , c := range tc {
90
92
t .Run (name , func (t * testing.T ) {
91
- m := NewPostingCacheMetrics (prometheus .NewPedanticRegistry ())
93
+ r := prometheus .NewPedanticRegistry ()
94
+ m := NewPostingCacheMetrics (r )
92
95
timeNow := time .Now
93
96
cache := newFifoCache [int ](c .cfg , "test" , m , timeNow )
94
97
@@ -118,6 +121,16 @@ func TestFifoCacheExpire(t *testing.T) {
118
121
119
122
require .Equal (t , c .expectedFinalItems , totalCacheSize )
120
123
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
+
121
134
if c .ttlExpire {
122
135
cache .timeNow = func () time.Time {
123
136
return timeNow ().Add (2 * c .cfg .Ttl )
@@ -135,6 +148,29 @@ func TestFifoCacheExpire(t *testing.T) {
135
148
// Total Size Updated
136
149
require .Equal (t , originalSize + 10 , cache .cachedBytes )
137
150
}
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 )
138
174
}
139
175
})
140
176
}
0 commit comments