Skip to content

Commit

Permalink
test: Update chunk/cache/mock to have a GetKeys() function (#12122)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul1r authored Mar 4, 2024
1 parent 17988d5 commit 97ae790
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
12 changes: 12 additions & 0 deletions pkg/storage/chunk/cache/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type MockCache interface {
NumKeyUpdates() int
GetInternal() map[string][]byte
KeysRequested() int
GetKeys() []string
}

type mockCache struct {
Expand Down Expand Up @@ -62,6 +63,17 @@ func (m *mockCache) GetInternal() map[string][]byte {
return m.cache
}

func (m *mockCache) GetKeys() []string {
m.Lock()
defer m.Unlock()

keys := make([]string, 0, len(m.cache))
for key := range m.cache {
keys = append(keys, key)
}
return keys
}

func (m *mockCache) KeysRequested() int {
return m.keysRequested
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/storage/chunk/fetcher/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ func Test(t *testing.T) {
chks, err := f.FetchChunks(context.Background(), test.fetch)
assert.NoError(t, err)
assertChunks(t, test.fetch, chks)
l1actual, err := makeChunksFromMap(c1.GetInternal())
l1actual, err := makeChunksFromMapKeys(c1.GetKeys())
assert.NoError(t, err)
assert.Equal(t, test.l1KeysRequested, c1.KeysRequested())
assertChunks(t, test.l1End, l1actual)
l2actual, err := makeChunksFromMap(c2.GetInternal())
l2actual, err := makeChunksFromMapKeys(c2.GetKeys())
assert.NoError(t, err)
assert.Equal(t, test.l2KeysRequested, c2.KeysRequested())
assertChunks(t, test.l2End, l2actual)
Expand Down Expand Up @@ -340,9 +340,9 @@ func makeChunks(now time.Time, tpls ...c) []chunk.Chunk {
return chks
}

func makeChunksFromMap(m map[string][]byte) ([]chunk.Chunk, error) {
chks := make([]chunk.Chunk, 0, len(m))
for k := range m {
func makeChunksFromMapKeys(keys []string) ([]chunk.Chunk, error) {
chks := make([]chunk.Chunk, 0, len(keys))
for _, k := range keys {
c, err := chunk.ParseExternalKey("fake", k)
if err != nil {
return nil, err
Expand Down

0 comments on commit 97ae790

Please sign in to comment.