-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathmanaged_storage_test.go
More file actions
301 lines (253 loc) · 7.65 KB
/
Copy pathmanaged_storage_test.go
File metadata and controls
301 lines (253 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package payloadstorage
import (
"bytes"
"context"
"errors"
"sync"
"testing"
"time"
)
type mockStorage struct {
mu sync.Mutex
data map[string][]byte
pruned []uint64
storeCb func(epochId uint64)
pruneCb func(epochId uint64) error
}
func newMockStorage() *mockStorage {
return &mockStorage{data: make(map[string][]byte)}
}
func (m *mockStorage) Store(ctx context.Context, inferenceId string, epochId uint64, prompt, response []byte) error {
m.mu.Lock()
defer m.mu.Unlock()
m.data[inferenceId] = append(prompt, response...)
if m.storeCb != nil {
m.storeCb(epochId)
}
return nil
}
func (m *mockStorage) Retrieve(ctx context.Context, inferenceId string, epochId uint64) ([]byte, []byte, error) {
m.mu.Lock()
defer m.mu.Unlock()
d, ok := m.data[inferenceId]
if !ok {
return nil, nil, ErrNotFound
}
half := len(d) / 2
return d[:half], d[half:], nil
}
func (m *mockStorage) PruneEpoch(ctx context.Context, epochId uint64) error {
m.mu.Lock()
defer m.mu.Unlock()
if m.pruneCb != nil {
if err := m.pruneCb(epochId); err != nil {
return err
}
}
m.pruned = append(m.pruned, epochId)
return nil
}
func (m *mockStorage) getPruned() []uint64 {
m.mu.Lock()
defer m.mu.Unlock()
result := make([]uint64, len(m.pruned))
copy(result, m.pruned)
return result
}
func TestManagedStorage_CacheHit(t *testing.T) {
mock := newMockStorage()
ms := NewManagedStorageWithSize(mock, 3, time.Minute, 100)
ctx := context.Background()
if err := mock.Store(ctx, "inf-1", 1, []byte("prompt"), []byte("response")); err != nil {
t.Fatalf("Store failed: %v", err)
}
// First retrieve - cache miss
p1, r1, err := ms.Retrieve(ctx, "inf-1", 1)
if err != nil {
t.Fatalf("Retrieve failed: %v", err)
}
// Modify underlying storage
mock.mu.Lock()
mock.data["inf-1"] = []byte("modifiedmodified")
mock.mu.Unlock()
// Second retrieve - should hit cache, return original data
p2, r2, err := ms.Retrieve(ctx, "inf-1", 1)
if err != nil {
t.Fatalf("Retrieve failed: %v", err)
}
if !bytes.Equal(p1, p2) || !bytes.Equal(r1, r2) {
t.Errorf("cache should return same data: got %q/%q, want %q/%q", p2, r2, p1, r1)
}
}
func TestManagedStorage_CacheExpiration(t *testing.T) {
mock := newMockStorage()
ms := NewManagedStorageWithSize(mock, 3, 10*time.Millisecond, 100)
ctx := context.Background()
if err := mock.Store(ctx, "inf-1", 1, []byte("prompt"), []byte("response")); err != nil {
t.Fatalf("Store failed: %v", err)
}
// First retrieve
_, _, err := ms.Retrieve(ctx, "inf-1", 1)
if err != nil {
t.Fatalf("Retrieve failed: %v", err)
}
// Modify underlying storage
mock.mu.Lock()
mock.data["inf-1"] = []byte("newdatnewdat")
mock.mu.Unlock()
// Wait for cache to expire
time.Sleep(15 * time.Millisecond)
// Retrieve should get new data
p, r, err := ms.Retrieve(ctx, "inf-1", 1)
if err != nil {
t.Fatalf("Retrieve failed: %v", err)
}
if string(p) != "newdat" || string(r) != "newdat" {
t.Errorf("expired cache should fetch fresh data: got %q/%q", p, r)
}
}
func TestManagedStorage_StoreTracksMaxEpoch(t *testing.T) {
mock := newMockStorage()
ms := NewManagedStorageWithSize(mock, 3, time.Minute, 100)
ctx := context.Background()
// Store in various epochs
ms.Store(ctx, "inf-1", 5, []byte("p"), []byte("r"))
ms.Store(ctx, "inf-2", 3, []byte("p"), []byte("r"))
ms.Store(ctx, "inf-3", 10, []byte("p"), []byte("r"))
ms.Store(ctx, "inf-4", 7, []byte("p"), []byte("r"))
ms.mu.RLock()
maxEpoch := ms.maxEpoch
ms.mu.RUnlock()
if maxEpoch != 10 {
t.Errorf("maxEpoch should be 10, got %d", maxEpoch)
}
}
func TestManagedStorage_AutoPruneTriggersInCleanup(t *testing.T) {
mock := newMockStorage()
ms := NewManagedStorageWithSize(mock, 2, time.Minute, 100)
ctx := context.Background()
// Store enough to trigger pruning (retainCount=2, so epochs 0-7 should be pruned when maxEpoch=10)
for i := uint64(0); i <= 10; i++ {
ms.Store(ctx, "inf-"+string(rune('a'+i)), i, []byte("p"), []byte("r"))
}
// Trigger cleanup manually; pruning runs synchronously
ms.cleanup()
pruned := mock.getPruned()
// threshold = 10 - 2 = 8
// minPruned starts at 0, but only last 10 should be pruned
// so epochs 0-7 should be pruned (8 epochs)
if len(pruned) != 8 {
t.Errorf("expected 8 epochs pruned, got %d: %v", len(pruned), pruned)
}
}
func TestManagedStorage_AutoPruneSkipsOldEpochs(t *testing.T) {
mock := newMockStorage()
ms := NewManagedStorageWithSize(mock, 2, time.Minute, 100)
ctx := context.Background()
// Jump straight to epoch 100 (simulating restart with existing data)
ms.Store(ctx, "inf-1", 100, []byte("p"), []byte("r"))
// Trigger cleanup
ms.cleanup()
pruned := mock.getPruned()
// threshold = 100 - 2 = 98
// minPruned=0, but 0 + 10 < 98, so minPruned should jump to 98 - 10 = 88
// Only epochs 88-97 should be pruned (10 epochs max)
if len(pruned) > maxPruneLookback {
t.Errorf("should prune at most %d epochs, got %d: %v", maxPruneLookback, len(pruned), pruned)
}
// Verify we're pruning recent epochs, not from 0
for _, e := range pruned {
if e < 88 {
t.Errorf("should not prune epoch %d (too old, should skip)", e)
}
}
}
func TestManagedStorage_AutoPruneStopsAtFailedEpoch(t *testing.T) {
mock := newMockStorage()
pruneErr := errors.New("db connection lost")
mock.pruneCb = func(epochId uint64) error {
if epochId == 3 {
return pruneErr
}
return nil
}
ms := NewManagedStorageWithSize(mock, 2, time.Minute, 100)
ctx := context.Background()
for i := uint64(0); i <= 10; i++ {
ms.Store(ctx, "inf-"+string(rune('a'+i)), i, []byte("p"), []byte("r"))
}
// threshold = 10 - 2 = 8; epoch 3 fails, so only 0-2 should be pruned
ms.cleanup()
pruned := mock.getPruned()
if len(pruned) != 3 {
t.Errorf("expected 3 epochs pruned before failure, got %d: %v", len(pruned), pruned)
}
for _, e := range pruned {
if e >= 3 {
t.Errorf("epoch %d should not be pruned past the failed epoch 3", e)
}
}
ms.mu.RLock()
minPruned := ms.minPruned
ms.mu.RUnlock()
if minPruned != 3 {
t.Errorf("minPruned should stop at failed epoch 3, got %d", minPruned)
}
}
func TestManagedStorage_AutoPruneRetriesFailedEpoch(t *testing.T) {
mock := newMockStorage()
failOnce := true
mock.pruneCb = func(epochId uint64) error {
if epochId == 3 && failOnce {
failOnce = false
return errors.New("transient failure")
}
return nil
}
ms := NewManagedStorageWithSize(mock, 2, time.Minute, 100)
ctx := context.Background()
for i := uint64(0); i <= 10; i++ {
ms.Store(ctx, "inf-"+string(rune('a'+i)), i, []byte("p"), []byte("r"))
}
// First cleanup: prunes 0-2, fails at 3
ms.cleanup()
// Second cleanup: retries 3, then continues through 7
ms.cleanup()
pruned := mock.getPruned()
// threshold = 8: epochs 0-7 all pruned across the two ticks, none skipped
if len(pruned) != 8 {
t.Errorf("expected 8 epochs pruned after retry, got %d: %v", len(pruned), pruned)
}
seen := make(map[uint64]bool)
for _, e := range pruned {
if seen[e] {
t.Errorf("epoch %d pruned more than once", e)
}
seen[e] = true
}
for e := uint64(0); e < 8; e++ {
if !seen[e] {
t.Errorf("epoch %d was never pruned", e)
}
}
ms.mu.RLock()
minPruned := ms.minPruned
ms.mu.RUnlock()
if minPruned != 8 {
t.Errorf("minPruned should reach threshold 8 after retry, got %d", minPruned)
}
}
func TestManagedStorage_NoPruneWhenBelowRetainCount(t *testing.T) {
mock := newMockStorage()
ms := NewManagedStorageWithSize(mock, 5, time.Minute, 100)
ctx := context.Background()
// Store in epochs 0-4 (maxEpoch=4, retainCount=5)
for i := uint64(0); i <= 4; i++ {
ms.Store(ctx, "inf-"+string(rune('a'+i)), i, []byte("p"), []byte("r"))
}
ms.cleanup()
pruned := mock.getPruned()
if len(pruned) != 0 {
t.Errorf("should not prune when maxEpoch <= retainCount, got %v", pruned)
}
}