Debian Codegraph @

tests/lru_test.go

raw
1 package tests_test
2
3 import (
4 "crypto/rand"
5 "fmt"
6 "math"
7 "math/big"
8 "reflect"
9 "sync"
10 "testing"
11 "time"
12
13 "gorm.io/gorm/internal/lru"
14 )
15
16 func TestLRU_Add_ExistingKey_UpdatesValueAndExpiresAt(t *testing.T) {
17 lru := lru.NewLRU[string, int](10, nil, time.Hour)
18 lru.Add("key1", 1)
19 lru.Add("key1", 2)
20
21 if value, ok := lru.Get("key1"); !ok || value != 2 {
22 t.Errorf("Expected value to be updated to 2, got %v", value)
23 }
24 }
25
26 func TestLRU_Add_NewKey_AddsEntry(t *testing.T) {
27 lru := lru.NewLRU[string, int](10, nil, time.Hour)
28 lru.Add("key1", 1)
29
30 if value, ok := lru.Get("key1"); !ok || value != 1 {
31 t.Errorf("Expected key1 to be added with value 1, got %v", value)
32 }
33 }
34
35 func TestLRU_Add_ExceedsSize_RemovesOldest(t *testing.T) {
36 lru := lru.NewLRU[string, int](2, nil, time.Hour)
37 lru.Add("key1", 1)
38 lru.Add("key2", 2)
39 lru.Add("key3", 3)
40
41 if _, ok := lru.Get("key1"); ok {
42 t.Errorf("Expected key1 to be removed, but it still exists")
43 }
44 }
45
46 func TestLRU_Add_UnlimitedSize_NoEviction(t *testing.T) {
47 lru := lru.NewLRU[string, int](0, nil, time.Hour)
48 lru.Add("key1", 1)
49 lru.Add("key2", 2)
50 lru.Add("key3", 3)
51
52 if _, ok := lru.Get("key1"); !ok {
53 t.Errorf("Expected key1 to exist, but it was evicted")
54 }
55 }
56
57 func TestLRU_Add_Eviction(t *testing.T) {
58 lru := lru.NewLRU[string, int](0, nil, time.Second*2)
59 lru.Add("key1", 1)
60 lru.Add("key2", 2)
61 lru.Add("key3", 3)
62 time.Sleep(time.Second * 3)
63 if lru.Cap() != 0 {
64 t.Errorf("Expected lru to be empty, but it was not")
65 }
66
67 }
68
69 func BenchmarkLRU_Rand_NoExpire(b *testing.B) {
70 l := lru.NewLRU[int64, int64](8192, nil, 0)
71
72 trace := make([]int64, b.N*2)
73 for i := 0; i < b.N*2; i++ {
74 trace[i] = getRand(b) % 32768
75 }
76
77 b.ResetTimer()
78
79 var hit, miss int
80 for i := 0; i < 2*b.N; i++ {
81 if i%2 == 0 {
82 l.Add(trace[i], trace[i])
83 } else {
84 if _, ok := l.Get(trace[i]); ok {
85 hit++
86 } else {
87 miss++
88 }
89 }
90 }
91 b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss))
92 }
93
94 func BenchmarkLRU_Freq_NoExpire(b *testing.B) {
95 l := lru.NewLRU[int64, int64](8192, nil, 0)
96
97 trace := make([]int64, b.N*2)
98 for i := 0; i < b.N*2; i++ {
99 if i%2 == 0 {
100 trace[i] = getRand(b) % 16384
101 } else {
102 trace[i] = getRand(b) % 32768
103 }
104 }
105
106 b.ResetTimer()
107
108 for i := 0; i < b.N; i++ {
109 l.Add(trace[i], trace[i])
110 }
111 var hit, miss int
112 for i := 0; i < b.N; i++ {
113 if _, ok := l.Get(trace[i]); ok {
114 hit++
115 } else {
116 miss++
117 }
118 }
119 b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss))
120 }
121
122 func BenchmarkLRU_Rand_WithExpire(b *testing.B) {
123 l := lru.NewLRU[int64, int64](8192, nil, time.Millisecond*10)
124
125 trace := make([]int64, b.N*2)
126 for i := 0; i < b.N*2; i++ {
127 trace[i] = getRand(b) % 32768
128 }
129
130 b.ResetTimer()
131
132 var hit, miss int
133 for i := 0; i < 2*b.N; i++ {
134 if i%2 == 0 {
135 l.Add(trace[i], trace[i])
136 } else {
137 if _, ok := l.Get(trace[i]); ok {
138 hit++
139 } else {
140 miss++
141 }
142 }
143 }
144 b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss))
145 }
146
147 func BenchmarkLRU_Freq_WithExpire(b *testing.B) {
148 l := lru.NewLRU[int64, int64](8192, nil, time.Millisecond*10)
149
150 trace := make([]int64, b.N*2)
151 for i := 0; i < b.N*2; i++ {
152 if i%2 == 0 {
153 trace[i] = getRand(b) % 16384
154 } else {
155 trace[i] = getRand(b) % 32768
156 }
157 }
158
159 b.ResetTimer()
160
161 for i := 0; i < b.N; i++ {
162 l.Add(trace[i], trace[i])
163 }
164 var hit, miss int
165 for i := 0; i < b.N; i++ {
166 if _, ok := l.Get(trace[i]); ok {
167 hit++
168 } else {
169 miss++
170 }
171 }
172 b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss))
173 }
174
175 func TestLRUNoPurge(t *testing.T) {
176 lc := lru.NewLRU[string, string](10, nil, 0)
177
178 lc.Add("key1", "val1")
179 if lc.Len() != 1 {
180 t.Fatalf("length differs from expected")
181 }
182
183 v, ok := lc.Peek("key1")
184 if v != "val1" {
185 t.Fatalf("value differs from expected")
186 }
187 if !ok {
188 t.Fatalf("should be true")
189 }
190
191 if !lc.Contains("key1") {
192 t.Fatalf("should contain key1")
193 }
194 if lc.Contains("key2") {
195 t.Fatalf("should not contain key2")
196 }
197
198 v, ok = lc.Peek("key2")
199 if v != "" {
200 t.Fatalf("should be empty")
201 }
202 if ok {
203 t.Fatalf("should be false")
204 }
205
206 if !reflect.DeepEqual(lc.Keys(), []string{"key1"}) {
207 t.Fatalf("value differs from expected")
208 }
209
210 if lc.Resize(0) != 0 {
211 t.Fatalf("evicted count differs from expected")
212 }
213 if lc.Resize(2) != 0 {
214 t.Fatalf("evicted count differs from expected")
215 }
216 lc.Add("key2", "val2")
217 if lc.Resize(1) != 1 {
218 t.Fatalf("evicted count differs from expected")
219 }
220 }
221
222 func TestLRUEdgeCases(t *testing.T) {
223 lc := lru.NewLRU[string, *string](2, nil, 0)
224
225 // Adding a nil value
226 lc.Add("key1", nil)
227
228 value, exists := lc.Get("key1")
229 if value != nil || !exists {
230 t.Fatalf("unexpected value or existence flag for key1: value=%v, exists=%v", value, exists)
231 }
232
233 // Adding an entry with the same key but different value
234 newVal := "val1"
235 lc.Add("key1", &newVal)
236
237 value, exists = lc.Get("key1")
238 if value != &newVal || !exists {
239 t.Fatalf("unexpected value or existence flag for key1: value=%v, exists=%v", value, exists)
240 }
241 }
242
243 func TestLRU_Values(t *testing.T) {
244 lc := lru.NewLRU[string, string](3, nil, 0)
245
246 lc.Add("key1", "val1")
247 lc.Add("key2", "val2")
248 lc.Add("key3", "val3")
249
250 values := lc.Values()
251 if !reflect.DeepEqual(values, []string{"val1", "val2", "val3"}) {
252 t.Fatalf("values differs from expected")
253 }
254 }
255
256 // func TestExpirableMultipleClose(_ *testing.T) {
257 // lc :=lru.NewLRU[string, string](10, nil, 0)
258 // lc.Close()
259 // // should not panic
260 // lc.Close()
261 // }
262
263 func TestLRUWithPurge(t *testing.T) {
264 var evicted []string
265 lc := lru.NewLRU(10, func(key string, value string) { evicted = append(evicted, key, value) }, 150*time.Millisecond)
266
267 k, v, ok := lc.GetOldest()
268 if k != "" {
269 t.Fatalf("should be empty")
270 }
271 if v != "" {
272 t.Fatalf("should be empty")
273 }
274 if ok {
275 t.Fatalf("should be false")
276 }
277
278 lc.Add("key1", "val1")
279
280 time.Sleep(100 * time.Millisecond) // not enough to expire
281 if lc.Len() != 1 {
282 t.Fatalf("length differs from expected")
283 }
284
285 v, ok = lc.Get("key1")
286 if v != "val1" {
287 t.Fatalf("value differs from expected")
288 }
289 if !ok {
290 t.Fatalf("should be true")
291 }
292
293 time.Sleep(200 * time.Millisecond) // expire
294 v, ok = lc.Get("key1")
295 if ok {
296 t.Fatalf("should be false")
297 }
298 if v != "" {
299 t.Fatalf("should be nil")
300 }
301
302 if lc.Len() != 0 {
303 t.Fatalf("length differs from expected")
304 }
305 if !reflect.DeepEqual(evicted, []string{"key1", "val1"}) {
306 t.Fatalf("value differs from expected")
307 }
308
309 // add new entry
310 lc.Add("key2", "val2")
311 if lc.Len() != 1 {
312 t.Fatalf("length differs from expected")
313 }
314
315 k, v, ok = lc.GetOldest()
316 if k != "key2" {
317 t.Fatalf("value differs from expected")
318 }
319 if v != "val2" {
320 t.Fatalf("value differs from expected")
321 }
322 if !ok {
323 t.Fatalf("should be true")
324 }
325
326 }
327
328 func TestLRUWithPurgeEnforcedBySize(t *testing.T) {
329 lc := lru.NewLRU[string, string](10, nil, time.Hour)
330
331 for i := 0; i < 100; i++ {
332 i := i
333 lc.Add(fmt.Sprintf("key%d", i), fmt.Sprintf("val%d", i))
334 v, ok := lc.Get(fmt.Sprintf("key%d", i))
335 if v != fmt.Sprintf("val%d", i) {
336 t.Fatalf("value differs from expected")
337 }
338 if !ok {
339 t.Fatalf("should be true")
340 }
341 if lc.Len() > 20 {
342 t.Fatalf("length should be less than 20")
343 }
344 }
345
346 if lc.Len() != 10 {
347 t.Fatalf("length differs from expected")
348 }
349 }
350
351 func TestLRUConcurrency(t *testing.T) {
352 lc := lru.NewLRU[string, string](0, nil, 0)
353 wg := sync.WaitGroup{}
354 wg.Add(1000)
355 for i := 0; i < 1000; i++ {
356 go func(i int) {
357 lc.Add(fmt.Sprintf("key-%d", i/10), fmt.Sprintf("val-%d", i/10))
358 wg.Done()
359 }(i)
360 }
361 wg.Wait()
362 if lc.Len() != 100 {
363 t.Fatalf("length differs from expected")
364 }
365 }
366
367 func TestLRUInvalidateAndEvict(t *testing.T) {
368 var evicted int
369 lc := lru.NewLRU(-1, func(_, _ string) { evicted++ }, 0)
370
371 lc.Add("key1", "val1")
372 lc.Add("key2", "val2")
373
374 val, ok := lc.Get("key1")
375 if !ok {
376 t.Fatalf("should be true")
377 }
378 if val != "val1" {
379 t.Fatalf("value differs from expected")
380 }
381 if evicted != 0 {
382 t.Fatalf("value differs from expected")
383 }
384
385 lc.Remove("key1")
386 if evicted != 1 {
387 t.Fatalf("value differs from expected")
388 }
389 val, ok = lc.Get("key1")
390 if val != "" {
391 t.Fatalf("should be empty")
392 }
393 if ok {
394 t.Fatalf("should be false")
395 }
396 }
397
398 func TestLoadingExpired(t *testing.T) {
399 lc := lru.NewLRU[string, string](0, nil, time.Millisecond*5)
400
401 lc.Add("key1", "val1")
402 if lc.Len() != 1 {
403 t.Fatalf("length differs from expected")
404 }
405
406 v, ok := lc.Peek("key1")
407 if v != "val1" {
408 t.Fatalf("value differs from expected")
409 }
410 if !ok {
411 t.Fatalf("should be true")
412 }
413
414 v, ok = lc.Get("key1")
415 if v != "val1" {
416 t.Fatalf("value differs from expected")
417 }
418 if !ok {
419 t.Fatalf("should be true")
420 }
421
422 for {
423 result, ok := lc.Get("key1")
424 if ok && result == "" {
425 t.Fatalf("ok should return a result")
426 }
427 if !ok {
428 break
429 }
430 }
431
432 time.Sleep(time.Millisecond * 100) // wait for expiration reaper
433 if lc.Len() != 0 {
434 t.Fatalf("length differs from expected")
435 }
436
437 v, ok = lc.Peek("key1")
438 if v != "" {
439 t.Fatalf("should be empty")
440 }
441 if ok {
442 t.Fatalf("should be false")
443 }
444
445 v, ok = lc.Get("key1")
446 if v != "" {
447 t.Fatalf("should be empty")
448 }
449 if ok {
450 t.Fatalf("should be false")
451 }
452 }
453
454 func TestLRURemoveOldest(t *testing.T) {
455 lc := lru.NewLRU[string, string](2, nil, 0)
456
457 if lc.Cap() != 2 {
458 t.Fatalf("expect cap is 2")
459 }
460
461 k, v, ok := lc.RemoveOldest()
462 if k != "" {
463 t.Fatalf("should be empty")
464 }
465 if v != "" {
466 t.Fatalf("should be empty")
467 }
468 if ok {
469 t.Fatalf("should be false")
470 }
471
472 ok = lc.Remove("non_existent")
473 if ok {
474 t.Fatalf("should be false")
475 }
476
477 lc.Add("key1", "val1")
478 if lc.Len() != 1 {
479 t.Fatalf("length differs from expected")
480 }
481
482 v, ok = lc.Get("key1")
483 if !ok {
484 t.Fatalf("should be true")
485 }
486 if v != "val1" {
487 t.Fatalf("value differs from expected")
488 }
489
490 if !reflect.DeepEqual(lc.Keys(), []string{"key1"}) {
491 t.Fatalf("value differs from expected")
492 }
493 if lc.Len() != 1 {
494 t.Fatalf("length differs from expected")
495 }
496
497 lc.Add("key2", "val2")
498 if !reflect.DeepEqual(lc.Keys(), []string{"key1", "key2"}) {
499 t.Fatalf("value differs from expected")
500 }
501 if lc.Len() != 2 {
502 t.Fatalf("length differs from expected")
503 }
504
505 k, v, ok = lc.RemoveOldest()
506 if k != "key1" {
507 t.Fatalf("value differs from expected")
508 }
509 if v != "val1" {
510 t.Fatalf("value differs from expected")
511 }
512 if !ok {
513 t.Fatalf("should be true")
514 }
515
516 if !reflect.DeepEqual(lc.Keys(), []string{"key2"}) {
517 t.Fatalf("value differs from expected")
518 }
519 if lc.Len() != 1 {
520 t.Fatalf("length differs from expected")
521 }
522 }
523
524 func getRand(tb testing.TB) int64 {
525 out, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64))
526 if err != nil {
527 tb.Fatal(err)
528 }
529 return out.Int64()
530 }