-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcache_test.go
731 lines (577 loc) · 18.4 KB
/
cache_test.go
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
package lru_test
import (
"reflect"
"sync/atomic"
"testing"
"time"
"github.com/nathanjcochran/lru"
)
func TestAdd(t *testing.T) {
cache, err := lru.NewCache()
if err != nil {
t.Fatalf("Error creating cache: %s", err)
}
defer cache.Stop()
eviction := cache.Add("key1", "val1")
expectEviction(false, eviction, t)
eviction = cache.Add("key2", "val2")
expectEviction(false, eviction, t)
expectKeys([]interface{}{"key1", "key2"}, cache.Keys(), t)
val, ok := cache.Peek("key1")
expectExists(true, ok, t)
expectValue("val1", val, t)
val, ok = cache.Peek("key2")
expectExists(true, ok, t)
expectValue("val2", val, t)
}
func TestAddExisting(t *testing.T) {
cache, err := lru.NewCache()
if err != nil {
t.Fatalf("Error creating cache: %s", err)
}
defer cache.Stop()
eviction := cache.Add("key1", "val1")
expectEviction(false, eviction, t)
eviction = cache.Add("key1", "val2")
expectEviction(false, eviction, t)
expectKeys([]interface{}{"key1"}, cache.Keys(), t)
val, ok := cache.Peek("key1")
expectExists(true, ok, t)
expectValue("val2", val, t)
}
func TestAddEvict(t *testing.T) {
evicted, onEvict := expectOnEvict(t,
keyVal{"key1", "val1"},
)
cache, err := lru.NewCache(
lru.SetSize(2), // Third addition will cause eviction
lru.SetOnEvict(onEvict),
)
if err != nil {
t.Fatalf("Error creating cache: %s", err)
}
defer cache.Stop()
eviction := cache.Add("key1", "val1")
expectEviction(false, eviction, t)
expectOnEvictCalled(0, evicted, t)
eviction = cache.Add("key2", "val2")
expectEviction(false, eviction, t)
expectOnEvictCalled(0, evicted, t)
eviction = cache.Add("key3", "val3") // key1 is evicted
expectEviction(true, eviction, t)
expectOnEvictCalled(1, evicted, t)
expectKeys([]interface{}{"key2", "key3"}, cache.Keys(), t)
val, ok := cache.Peek("key1")
expectExists(false, ok, t)
expectValue(nil, val, t)
}
func TestAddExistingEvict(t *testing.T) {
evicted, onEvict := expectOnEvict(t,
keyVal{"key2", "val2"},
)
cache, err := lru.NewCache(
lru.SetSize(2), // Third addition will cause eviction
lru.SetOnEvict(onEvict),
)
if err != nil {
t.Fatalf("Error creating cache: %s", err)
}
defer cache.Stop()
eviction := cache.Add("key1", "val1")
expectEviction(false, eviction, t)
expectOnEvictCalled(0, evicted, t)
eviction = cache.Add("key2", "val2")
expectEviction(false, eviction, t)
expectOnEvictCalled(0, evicted, t)
eviction = cache.Add("key1", "val1") // key2 becomes least recently used
expectEviction(false, eviction, t)
expectOnEvictCalled(0, evicted, t)
eviction = cache.Add("key3", "val3") // key2 is evicted
expectEviction(true, eviction, t)
expectOnEvictCalled(1, evicted, t)
expectKeys([]interface{}{"key1", "key3"}, cache.Keys(), t)
val, ok := cache.Peek("key2")
expectExists(false, ok, t)
expectValue(nil, val, t)
}
func TestExpiration(t *testing.T) {
expired, onEvict := expectOnExpire(t,
keyVal{"key1", "val1"},
keyVal{"key2", "val2"},
)
cache, err := lru.NewCache(
lru.SetTTL(100*time.Millisecond), // Entries will expire after 100ms
lru.SetTTLMargin(10*time.Millisecond), // Entries can expire up to 10ms early (i.e. after 90ms)
lru.SetOnExpire(onEvict),
)
if err != nil {
t.Fatalf("Error creating cache: %s", err)
}
defer cache.Stop()
cache.Add("key1", "val1")
time.Sleep(50 * time.Millisecond) // 50ms (one added)
cache.Add("key2", "val2")
expectOnExpireCalled(0, expired, t)
expectKeys([]interface{}{"key1", "key2"}, cache.Keys(), t)
time.Sleep(75 * time.Millisecond) // 125ms (two added, one expired)
expectOnExpireCalled(1, expired, t)
expectKeys([]interface{}{"key2"}, cache.Keys(), t)
val, ok := cache.Peek("key1")
expectExists(false, ok, t)
expectValue(nil, val, t)
time.Sleep(50 * time.Millisecond) // 175ms (two expired)
expectOnExpireCalled(2, expired, t)
expectKeys([]interface{}{}, cache.Keys(), t)
val, ok = cache.Peek("key2")
expectExists(false, ok, t)
expectValue(nil, val, t)
}
func TestUpdate(t *testing.T) {
numWorkers := []int{0, 1, 2}
for _, workers := range numWorkers {
testUpdate(workers, t)
}
}
func testUpdate(workers int, t *testing.T) {
updated, updateFunc := expectUpdateFunc(t,
updateVal{"key1", "val4", lru.Update},
updateVal{"key2", nil, lru.Pass},
updateVal{"key3", nil, lru.Remove},
)
cache, err := lru.NewCache(
lru.SetWorkers(workers),
lru.SetTTL(150*time.Millisecond), // Entries will expire after 150ms
lru.SetTTLMargin(10*time.Millisecond), // Entries can expire up to 10ms early (i.e. after 140ms)
lru.SetUpdateFunc(updateFunc),
)
if err != nil {
t.Fatalf("Error creating cache: %s", err)
}
defer cache.Stop()
cache.Add("key1", "val1")
time.Sleep(50 * time.Millisecond) // 50ms (one added)
cache.Add("key2", "val2")
expectUpdateFuncCalled(0, updated, t)
expectKeys([]interface{}{"key1", "key2"}, cache.Keys(), t)
time.Sleep(50 * time.Millisecond) // 100ms (two added)
cache.Add("key3", "val3")
expectUpdateFuncCalled(0, updated, t)
expectKeys([]interface{}{"key1", "key2", "key3"}, cache.Keys(), t)
time.Sleep(75 * time.Millisecond) // 175ms (three added, one updated)
expectUpdateFuncCalled(1, updated, t)
expectKeys([]interface{}{"key1", "key2", "key3"}, cache.Keys(), t)
val, ok := cache.Peek("key1")
expectExists(true, ok, t)
expectValue("val4", val, t)
time.Sleep(50 * time.Millisecond) // 225ms (three added, one updated, one passed)
expectUpdateFuncCalled(2, updated, t)
expectKeys([]interface{}{"key1", "key2", "key3"}, cache.Keys(), t)
val, ok = cache.Peek("key2")
expectExists(true, ok, t)
expectValue("val2", val, t)
time.Sleep(50 * time.Millisecond) // 275ms (three added, one updated, one passed, one removed)
expectUpdateFuncCalled(3, updated, t)
expectKeys([]interface{}{"key1", "key2"}, cache.Keys(), t)
val, ok = cache.Peek("key3")
expectExists(false, ok, t)
expectValue(nil, val, t)
}
func TestUpdateThreshold(t *testing.T) {
numWorkers := []int{0, 1, 2}
for _, workers := range numWorkers {
testUpdateThreshold(workers, t)
}
}
func testUpdateThreshold(workers int, t *testing.T) {
expired, onExpire := expectOnExpire(t,
keyVal{"key3", "val3"},
)
updated, updateFunc := expectUpdateFunc(t,
updateVal{"key1", "val4", lru.Update},
updateVal{"key2", nil, lru.Pass},
)
cache, err := lru.NewCache(
lru.SetWorkers(workers),
lru.SetTTL(150*time.Millisecond), // Entries will expire after 150ms
lru.SetTTLMargin(10*time.Millisecond), // Entries can expire up to 10ms early (i.e. after 140ms)
lru.SetUpdateThreshold(1), // Entries will only be updated if they were fetched at least once
lru.SetOnExpire(onExpire),
lru.SetUpdateFunc(updateFunc),
)
if err != nil {
t.Fatalf("Error creating cache: %s", err)
}
defer cache.Stop()
cache.Add("key1", "val1")
time.Sleep(50 * time.Millisecond) // 50ms (one added)
cache.Get("key1")
cache.Add("key2", "val2")
expectOnExpireCalled(0, expired, t)
expectUpdateFuncCalled(0, updated, t)
expectKeys([]interface{}{"key1", "key2"}, cache.Keys(), t)
time.Sleep(50 * time.Millisecond) // 100ms (two added, one hit),
cache.Get("key2")
cache.Add("key3", "val3")
expectOnExpireCalled(0, expired, t)
expectUpdateFuncCalled(0, updated, t)
expectKeys([]interface{}{"key1", "key2", "key3"}, cache.Keys(), t)
time.Sleep(75 * time.Millisecond) // 175ms (three added, two hit, one updated)
expectOnExpireCalled(0, expired, t)
expectUpdateFuncCalled(1, updated, t)
expectKeys([]interface{}{"key1", "key2", "key3"}, cache.Keys(), t)
val, ok := cache.Peek("key1")
expectExists(true, ok, t)
expectValue("val4", val, t)
time.Sleep(50 * time.Millisecond) // 225ms (three added, two hit, one updated, one passed)
expectOnExpireCalled(0, expired, t)
expectUpdateFuncCalled(2, updated, t)
expectKeys([]interface{}{"key1", "key2", "key3"}, cache.Keys(), t)
val, ok = cache.Peek("key2")
expectExists(true, ok, t)
expectValue("val2", val, t)
time.Sleep(50 * time.Millisecond) // 275ms (three added, two hit, one updated, one passed, one expired)
expectOnExpireCalled(1, expired, t)
expectUpdateFuncCalled(2, updated, t)
expectKeys([]interface{}{"key1", "key2"}, cache.Keys(), t)
val, ok = cache.Peek("key3")
expectExists(false, ok, t)
expectValue(nil, val, t)
}
func TestUpdateWithConcurrentRemove(t *testing.T) {
numWorkers := []int{0, 1, 2}
for _, workers := range numWorkers {
testUpdateWithConcurrentRemove(workers, t)
}
}
func testUpdateWithConcurrentRemove(workers int, t *testing.T) {
updateFunc := func(key interface{}) (interface{}, lru.Status) {
time.Sleep(50 * time.Millisecond) // Updates take 50ms
return "val2", lru.Update
}
cache, err := lru.NewCache(
lru.SetWorkers(workers),
lru.SetTTL(50*time.Millisecond), // Entries will expire after 50ms
lru.SetTTLMargin(10*time.Millisecond), // Entries can expire up to 10ms early (i.e. after 40ms)
lru.SetUpdateFunc(updateFunc),
)
if err != nil {
t.Fatalf("Error creating cache: %s", err)
}
defer cache.Stop()
cache.Add("key1", "val1")
time.Sleep(75 * time.Millisecond) // 75ms (one added, one ongoing update)
cache.Remove("key1")
time.Sleep(50 * time.Millisecond) // 125ms (one added, one removed during update)
expectKeys([]interface{}{}, cache.Keys(), t)
val, ok := cache.Peek("key1")
expectExists(false, ok, t)
expectValue(nil, val, t)
}
func TestUpdateWithConcurrentAdd(t *testing.T) {
numWorkers := []int{0, 1, 2}
for _, workers := range numWorkers {
testUpdateWithConcurrentAdd(workers, t)
}
}
func testUpdateWithConcurrentAdd(workers int, t *testing.T) {
updateFunc := func(key interface{}) (interface{}, lru.Status) {
time.Sleep(50 * time.Millisecond) // Updates take 50ms
return "val3", lru.Update
}
cache, err := lru.NewCache(
lru.SetWorkers(workers),
lru.SetTTL(50*time.Millisecond), // Entries will expire after 50ms
lru.SetTTLMargin(10*time.Millisecond), // Entries can expire up to 10ms early (i.e. after 40ms)
lru.SetUpdateFunc(updateFunc),
)
if err != nil {
t.Fatalf("Error creating cache: %s", err)
}
defer cache.Stop()
cache.Add("key1", "val1")
time.Sleep(75 * time.Millisecond) // 75ms (one added, one ongoing update)
cache.Add("key1", "val2")
time.Sleep(50 * time.Millisecond) // 125ms (one added, one re-added during update)
keys := cache.Keys()
expectKeys([]interface{}{"key1"}, keys, t)
val, ok := cache.Peek("key1")
expectExists(true, ok, t)
expectValue("val3", val, t)
}
func TestUpdateWithFullBuffer(t *testing.T) {
updateFunc := func(key interface{}) (interface{}, lru.Status) {
time.Sleep(50 * time.Millisecond) // Updates take 50ms
return "val4", lru.Update
}
done := make(chan struct{})
onBufferFull := func(key, val interface{}) {
if key != "key3" {
t.Errorf("Expected key: %v. Actual: %v", "key3", key)
}
if val != "val3" {
t.Errorf("Expected val: %v. Actual: %v", "val3", key)
}
close(done)
}
cache, err := lru.NewCache(
lru.SetWorkers(1),
lru.SetTTL(50*time.Millisecond), // Entries will expire after 50ms
lru.SetTTLMargin(10*time.Millisecond), // Entries can expire up to 10ms early (i.e. after 40ms)
lru.SetUpdateFunc(updateFunc),
lru.SetBufferSize(1), // Buffer size is 1
lru.SetOnBufferFull(onBufferFull),
)
if err != nil {
t.Fatalf("Error creating cache: %s", err)
}
defer cache.Stop()
cache.Add("key1", "val1") // update is triggered
cache.Add("key2", "val2") // queues in buffer
cache.Add("key3", "val3") // overflows buffer
select {
case <-done:
case <-time.After(75 * time.Millisecond):
t.Errorf("Expected onBufferFull to be called")
}
}
func TestGet(t *testing.T) {
cache, err := lru.NewCache()
if err != nil {
t.Fatalf("Error creating cache: %s", err)
}
defer cache.Stop()
val, ok := cache.Get("key1")
expectExists(false, ok, t)
expectValue(nil, val, t)
cache.Add("key1", "val1")
val, ok = cache.Get("key1")
expectExists(true, ok, t)
expectValue("val1", val, t)
}
func TestGetEvict(t *testing.T) {
evicted, onEvict := expectOnEvict(t,
keyVal{"key2", "val2"},
)
cache, err := lru.NewCache(
lru.SetSize(2), // Third addition will cause eviction
lru.SetOnEvict(onEvict),
)
if err != nil {
t.Fatalf("Error creating cache: %s", err)
}
defer cache.Stop()
eviction := cache.Add("key1", "val1")
expectEviction(false, eviction, t)
expectOnEvictCalled(0, evicted, t)
expectKeys([]interface{}{"key1"}, cache.Keys(), t)
eviction = cache.Add("key2", "val2")
expectEviction(false, eviction, t)
expectOnEvictCalled(0, evicted, t)
expectKeys([]interface{}{"key1", "key2"}, cache.Keys(), t)
val, ok := cache.Get("key1") // key2 becomes least recently used
expectExists(true, ok, t)
expectValue("val1", val, t)
expectKeys([]interface{}{"key2", "key1"}, cache.Keys(), t)
eviction = cache.Add("key3", "val3") // key2 is evicted
expectEviction(true, eviction, t)
expectOnEvictCalled(1, evicted, t)
expectKeys([]interface{}{"key1", "key3"}, cache.Keys(), t)
val, ok = cache.Peek("key2")
expectExists(false, ok, t)
expectValue(nil, val, t)
}
func TestPeek(t *testing.T) {
cache, err := lru.NewCache()
if err != nil {
t.Fatalf("Error creating cache: %s", err)
}
defer cache.Stop()
val, ok := cache.Peek("key1")
expectExists(false, ok, t)
expectValue(nil, val, t)
cache.Add("key1", "val1")
val, ok = cache.Peek("key1")
expectExists(true, ok, t)
expectValue("val1", val, t)
}
func TestContains(t *testing.T) {
cache, err := lru.NewCache()
if err != nil {
t.Fatalf("Error creating cache: %s", err)
}
defer cache.Stop()
ok := cache.Contains("key1")
expectExists(false, ok, t)
cache.Add("key1", "val1")
ok = cache.Contains("key1")
expectExists(true, ok, t)
}
func TestRemove(t *testing.T) {
cache, err := lru.NewCache()
if err != nil {
t.Fatalf("Error creating cache: %s", err)
}
defer cache.Stop()
ok := cache.Remove("key1")
expectExists(false, ok, t)
cache.Add("key1", "val1")
ok = cache.Remove("key1")
expectExists(true, ok, t)
val, ok := cache.Peek("key1")
expectExists(false, ok, t)
expectValue(nil, val, t)
}
func TestKeys(t *testing.T) {
cache, err := lru.NewCache()
if err != nil {
t.Fatalf("Error creating cache: %s", err)
}
defer cache.Stop()
expectKeys([]interface{}{}, cache.Keys(), t)
cache.Add("key1", "val1")
expectKeys([]interface{}{"key1"}, cache.Keys(), t)
cache.Add("key1", "val1")
expectKeys([]interface{}{"key1"}, cache.Keys(), t)
cache.Add("key2", "val2")
expectKeys([]interface{}{"key1", "key2"}, cache.Keys(), t)
cache.Add("key3", "val3")
expectKeys([]interface{}{"key1", "key2", "key3"}, cache.Keys(), t)
cache.Get("key1")
expectKeys([]interface{}{"key2", "key3", "key1"}, cache.Keys(), t)
cache.Get("key3")
expectKeys([]interface{}{"key2", "key1", "key3"}, cache.Keys(), t)
cache.Remove("key1")
expectKeys([]interface{}{"key2", "key3"}, cache.Keys(), t)
cache.Remove("key1")
expectKeys([]interface{}{"key2", "key3"}, cache.Keys(), t)
cache.Remove("key2")
expectKeys([]interface{}{"key3"}, cache.Keys(), t)
cache.Remove("key3")
expectKeys([]interface{}{}, cache.Keys(), t)
}
func TestLen(t *testing.T) {
cache, err := lru.NewCache()
if err != nil {
t.Fatalf("Error creating cache: %s", err)
}
defer cache.Stop()
expectLen(0, cache.Len(), t)
cache.Add("key1", "val1")
expectLen(1, cache.Len(), t)
cache.Add("key1", "val1")
expectLen(1, cache.Len(), t)
cache.Add("key2", "val2")
expectLen(2, cache.Len(), t)
cache.Add("key3", "val3")
expectLen(3, cache.Len(), t)
cache.Get("key1")
expectLen(3, cache.Len(), t)
cache.Get("key3")
expectLen(3, cache.Len(), t)
cache.Remove("key1")
expectLen(2, cache.Len(), t)
cache.Remove("key1")
expectLen(2, cache.Len(), t)
cache.Remove("key2")
expectLen(1, cache.Len(), t)
cache.Remove("key3")
expectLen(0, cache.Len(), t)
}
func expectEviction(expected, actual bool, t *testing.T) {
if actual != expected {
t.Errorf("Expected eviction: %t. Actual: %t", expected, actual)
}
}
func expectExists(expected, actual bool, t *testing.T) {
if actual != expected {
t.Errorf("Expected entry to exist: %t. Actual: %t", expected, actual)
}
}
func expectValue(expected, actual interface{}, t *testing.T) {
if actual != expected {
t.Errorf("Expected value: %v. Actual: %v", expected, actual)
}
}
func expectKeys(expected, actual []interface{}, t *testing.T) {
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expected keys: %v. Actual: %v", expected, actual)
}
}
func expectLen(expected, actual int, t *testing.T) {
if actual != expected {
t.Errorf("Expected length: %d. Actual: %d", expected, actual)
}
}
type keyVal struct {
key, val interface{}
}
func expectOnEvict(t *testing.T, expected ...keyVal) (*int, lru.Callback) {
var evicted int
return &evicted, func(key, val interface{}) {
if len(expected) < evicted {
t.Errorf("Did not expect entry to be evicted. Key: %v. Value: %v", key, val)
return
}
kv := expected[evicted]
if key != kv.key {
t.Errorf("Expected key to be evicted: %v. Actual: %v", kv.key, key)
}
if val != kv.val {
t.Errorf("Expected val to be evicted: %v. Actual: %v", kv.val, val)
}
evicted++
}
}
func expectOnEvictCalled(expected int, actual *int, t *testing.T) {
if *actual != expected {
t.Errorf("Expected evicted: %d. Actual: %d", expected, *actual)
}
}
func expectOnExpire(t *testing.T, expected ...keyVal) (*int32, lru.Callback) {
var expired int32
return &expired, func(key, val interface{}) {
local := int(atomic.AddInt32(&expired, 1)) - 1
if len(expected) < local {
t.Errorf("Did not expect entry to expire. Key: %v. Value: %v", key, val)
return
}
kv := expected[local]
if key != kv.key {
t.Errorf("Expected key to expire: %v. Actual: %v", kv.key, key)
}
if val != kv.val {
t.Errorf("Expected val to expire: %v. Actual: %v", kv.val, val)
}
}
}
func expectOnExpireCalled(expected int, actual *int32, t *testing.T) {
local := atomic.LoadInt32(actual)
if int(local) != expected {
t.Errorf("Expected expired: %d. Actual: %d", expected, local)
}
}
type updateVal struct {
key, newVal interface{}
status lru.Status
}
func expectUpdateFunc(t *testing.T, expected ...updateVal) (*int32, lru.UpdateFunc) {
var expired int32
return &expired, func(key interface{}) (interface{}, lru.Status) {
local := int(atomic.AddInt32(&expired, 1)) - 1
if len(expected) <= local {
t.Errorf("Did not expect entry to expire. Key: %v", key)
return nil, lru.Remove
}
uv := expected[local]
if key != uv.key {
t.Errorf("Expected key to expire: %v. Actual: %v", uv.key, key)
}
return uv.newVal, uv.status
}
}
func expectUpdateFuncCalled(expected int, actual *int32, t *testing.T) {
local := atomic.LoadInt32(actual)
if int(local) != expected {
t.Errorf("Expected updated: %d. Actual: %d", expected, local)
}
}