MCPcopy
hub / github.com/grafana/dskit / TestLRUCache_SetAdd

Function TestLRUCache_SetAdd

cache/lru_test.go:100–131  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

98}
99
100func TestLRUCache_SetAdd(t *testing.T) {
101 const maxItems = 10
102
103 ctx := context.Background()
104 reg := prometheus.NewPedanticRegistry()
105 lru, err := WrapWithLRUCache(NewMockCache(), "test", reg, maxItems, 2*time.Hour, log.NewNopLogger())
106 require.NoError(t, err)
107
108 // Trying to .Add() a key that already exists should result in an error
109 require.NoError(t, lru.Set(ctx, "key_1", []byte("value_1"), time.Minute))
110 require.NoError(t, lru.Set(ctx, "key_2", []byte("value_2"), time.Minute))
111 require.NoError(t, lru.Set(ctx, "key_3", []byte("value_3"), time.Minute))
112 require.ErrorIs(t, lru.Add(ctx, "key_1", []byte("value_1_2"), time.Minute), ErrNotStored)
113
114 require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
115 # HELP cache_memory_items_count Total number of items currently in the in-memory cache.
116 # TYPE cache_memory_items_count gauge
117 cache_memory_items_count{name="test"} 3
118 `), "cache_memory_items_count"))
119
120 result := lru.GetMulti(ctx, []string{"key_1", "key_2", "key_3"})
121 require.Equal(t, map[string][]byte{
122 "key_1": []byte("value_1"),
123 "key_2": []byte("value_2"),
124 "key_3": []byte("value_3"),
125 }, result)
126
127 // Ensure we cache back entries from the underlying cache.
128 item, ok := lru.lru.Get("key_1")
129 require.True(t, ok, "expected to fetch %s from inner LRU cache, got %+v", "key_1", item)
130 require.Equal(t, []byte("value_1"), item.Data)
131}
132
133func TestLRUCache_GetMultiWithError(t *testing.T) {
134 mock := NewMockCache()

Callers

nothing calls this directly

Calls 7

WrapWithLRUCacheFunction · 0.85
NewMockCacheFunction · 0.85
SetMethod · 0.65
AddMethod · 0.65
GetMultiMethod · 0.65
GetMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected