MCPcopy Create free account
hub / github.com/cortexproject/cortex / TestFifoCacheEviction

Function TestFifoCacheEviction

pkg/chunk/cache/fifo_cache_test.go:16–164  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

14)
15
16func TestFifoCacheEviction(t *testing.T) {
17 const (
18 cnt = 10
19 evicted = 5
20 )
21 itemTemplate := &cacheEntry{
22 key: "00",
23 value: []byte("00"),
24 }
25
26 tests := []struct {
27 name string
28 cfg FifoCacheConfig
29 }{
30 {
31 name: "test-memory-eviction",
32 cfg: FifoCacheConfig{MaxSizeBytes: strconv.FormatInt(int64(cnt*sizeOf(itemTemplate)), 10), Validity: 1 * time.Minute},
33 },
34 {
35 name: "test-items-eviction",
36 cfg: FifoCacheConfig{MaxSizeItems: cnt, Validity: 1 * time.Minute},
37 },
38 }
39
40 for _, test := range tests {
41 c := NewFifoCache(test.name, test.cfg, nil, log.NewNopLogger())
42 ctx := context.Background()
43
44 // Check put / get works
45 keys := []string{}
46 values := [][]byte{}
47 for i := range cnt {
48 key := fmt.Sprintf("%02d", i)
49 value := make([]byte, len(key))
50 copy(value, key)
51 keys = append(keys, key)
52 values = append(values, value)
53 }
54 c.Store(ctx, keys, values)
55 require.Len(t, c.entries, cnt)
56
57 assert.Equal(t, testutil.ToFloat64(c.entriesAdded), float64(1))
58 assert.Equal(t, testutil.ToFloat64(c.entriesAddedNew), float64(cnt))
59 assert.Equal(t, testutil.ToFloat64(c.entriesEvicted), float64(0))
60 assert.Equal(t, testutil.ToFloat64(c.entriesCurrent), float64(cnt))
61 assert.Equal(t, testutil.ToFloat64(c.entriesCurrent), float64(len(c.entries)))
62 assert.Equal(t, testutil.ToFloat64(c.entriesCurrent), float64(c.lru.Len()))
63 assert.Equal(t, testutil.ToFloat64(c.totalGets), float64(0))
64 assert.Equal(t, testutil.ToFloat64(c.totalMisses), float64(0))
65 assert.Equal(t, testutil.ToFloat64(c.staleGets), float64(0))
66 assert.Equal(t, testutil.ToFloat64(c.memoryBytes), float64(cnt*sizeOf(itemTemplate)))
67
68 for i := range cnt {
69 key := fmt.Sprintf("%02d", i)
70 value, ok := c.Get(ctx, key)
71 require.True(t, ok)
72 require.Equal(t, []byte(key), value)
73 }

Callers

nothing calls this directly

Calls 7

StoreMethod · 0.95
GetMethod · 0.95
StopMethod · 0.95
sizeOfFunction · 0.85
NewFifoCacheFunction · 0.85
EqualMethod · 0.65
LenMethod · 0.45

Tested by

no test coverage detected