Skip to content

Commit 181666b

Browse files
authored
Add InMemoryIndex unit tests (#86)
* fix Lookup in index interface Signed-off-by: sagiahrac <[email protected]> Signed-off-by: Sage Ahrac <[email protected]> * fix GetPodScores Signed-off-by: sagiahrac <[email protected]> Signed-off-by: Sage Ahrac <[email protected]> * fix InMemoryIndex Lookup Signed-off-by: sagiahrac <[email protected]> Signed-off-by: Sage Ahrac <[email protected]> * fix instrumentedIndex Lookup Signed-off-by: sagiahrac <[email protected]> Signed-off-by: Sage Ahrac <[email protected]> * update RedisIndex.Lookup Signed-off-by: sagiahrac <[email protected]> Signed-off-by: Sage Ahrac <[email protected]> * Migrate common test to new interface Signed-off-by: sagiahrac <[email protected]> Signed-off-by: Sage Ahrac <[email protected]> * lint redis index Signed-off-by: sagiahrac <[email protected]> Signed-off-by: Sage Ahrac <[email protected]> * Add unit tests for InMemoryIndex Signed-off-by: sagiahrac <[email protected]> Signed-off-by: Sage Ahrac <[email protected]> --------- Signed-off-by: sagiahrac <[email protected]> Signed-off-by: Sage Ahrac <[email protected]>
1 parent d5a8ca8 commit 181666b

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

pkg/kvcache/kvblock/in_memory_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,73 @@ func TestInMemoryAddBasic(t *testing.T) {
2929
assert.NoError(t, err)
3030
testAddBasic(t, index)
3131
}
32+
33+
func TestInMemoryIndexSize(t *testing.T) {
34+
// Test with small size to verify eviction
35+
cfg := &kvblock.InMemoryIndexConfig{
36+
Size: 2, // Only 2 keys max
37+
PodCacheSize: 1, // Pod cache size doesn't matter for this test
38+
}
39+
40+
index, err := kvblock.NewInMemoryIndex(cfg)
41+
assert.NoError(t, err)
42+
43+
ctx := t.Context()
44+
45+
// Add first key
46+
key1 := kvblock.Key{ModelName: "test-model", ChunkHash: 111}
47+
err = index.Add(ctx, []kvblock.Key{key1}, []kvblock.PodEntry{{PodIdentifier: "pod1", DeviceTier: "gpu"}})
48+
assert.NoError(t, err)
49+
50+
// Add second key
51+
key2 := kvblock.Key{ModelName: "test-model", ChunkHash: 222}
52+
err = index.Add(ctx, []kvblock.Key{key2}, []kvblock.PodEntry{{PodIdentifier: "pod2", DeviceTier: "gpu"}})
53+
assert.NoError(t, err)
54+
55+
// Add third key - should evict the first one due to LRU
56+
key3 := kvblock.Key{ModelName: "test-model", ChunkHash: 333}
57+
err = index.Add(ctx, []kvblock.Key{key3}, []kvblock.PodEntry{{PodIdentifier: "pod3", DeviceTier: "cpu"}})
58+
assert.NoError(t, err)
59+
60+
// Lookup should only return the last two keys
61+
podsPerKey, err := index.Lookup(ctx, []kvblock.Key{key1, key2, key3}, nil)
62+
assert.NoError(t, err)
63+
64+
assert.Len(t, podsPerKey, 2) // Only key2 and key3 should be present
65+
assert.Len(t, podsPerKey[key2], 1)
66+
assert.Len(t, podsPerKey[key3], 1)
67+
assert.Contains(t, podsPerKey[key2], "pod2")
68+
assert.Contains(t, podsPerKey[key3], "pod3")
69+
}
70+
71+
func TestInMemoryIndexPodCacheSize(t *testing.T) {
72+
// Test with small limits to verify enforcement
73+
cfg := &kvblock.InMemoryIndexConfig{
74+
Size: 1, // Only 1 key max
75+
PodCacheSize: 2, // Only 2 pods per key
76+
}
77+
78+
index, err := kvblock.NewInMemoryIndex(cfg)
79+
assert.NoError(t, err)
80+
81+
// Test PodCacheSize limit: add more pods than the limit for one key
82+
key := kvblock.Key{ModelName: "test-model", ChunkHash: 111}
83+
pods := []kvblock.PodEntry{
84+
{PodIdentifier: "pod1", DeviceTier: "gpu"},
85+
{PodIdentifier: "pod2", DeviceTier: "gpu"},
86+
{PodIdentifier: "pod3", DeviceTier: "cpu"}, // This should evict pod1 due to LRU
87+
}
88+
89+
ctx := t.Context()
90+
91+
err = index.Add(ctx, []kvblock.Key{key}, pods)
92+
assert.NoError(t, err)
93+
94+
// Lookup should only return 2 pods (pod2 and pod3), pod1 should be evicted
95+
podsPerKey, err := index.Lookup(ctx, []kvblock.Key{key}, nil)
96+
assert.NoError(t, err)
97+
assert.Len(t, podsPerKey, 1)
98+
assert.Len(t, podsPerKey[key], 2, "Should only have 2 pods due to PodCacheSize limit")
99+
assert.Contains(t, podsPerKey[key], "pod2")
100+
assert.Contains(t, podsPerKey[key], "pod3")
101+
}

0 commit comments

Comments
 (0)