-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache_test.go
115 lines (102 loc) · 3.02 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
package cache
import (
"context"
"fmt"
"testing"
"time"
)
func Test_TTL(t *testing.T) {
testcaces := map[string]struct {
policy evictionPolicy
evictedKey string
}{
`LRU`: {policy: LRU, evictedKey: `k2`},
`LFU`: {policy: LFU, evictedKey: `k1`},
`ARC`: {policy: ARC, evictedKey: `k2`},
}
for name, tc := range testcaces {
tc := tc
t.Run(fmt.Sprintf(`cache(%s) eviction expired items`, name), func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cache := NewCache[string, string](ctx, 1, WithEvictionPolicy(tc.policy), WithTTLEpochGranularity(10*time.Millisecond))
cache.SetNX(`test`, `string`, 10*time.Millisecond)
<-time.After(5 * time.Millisecond)
value, ok := cache.Get(`test`)
if !ok {
fail(t, `expected key not expired`)
}
if value != `string` {
fail(t, `unexpected value %v`, value)
}
<-time.After(20 * time.Millisecond)
_, ok = cache.Get(`test`)
if ok {
fail(t, `expected key expired`)
}
})
t.Run(fmt.Sprintf(`cache(%s) update expiration`, name), func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cache := NewCache[string, string](ctx, 10, WithEvictionPolicy(tc.policy), WithTTLEpochGranularity(10*time.Millisecond))
cache.SetNX(`test`, `string`, 20*time.Millisecond)
<-time.After(10 * time.Millisecond)
cache.SetNX(`test`, `new string`, 20*time.Millisecond)
value, ok := cache.Get(`test`)
if !ok {
fail(t, `expected key not expired`)
}
if value != `new string` {
fail(t, `unexpected value %v`, value)
}
<-time.After(10 * time.Millisecond)
value, ok = cache.Get(`test`)
if !ok {
fail(t, `expected key not expired`)
}
if value != `new string` {
fail(t, `unexpected value %v`, value)
}
})
t.Run(fmt.Sprintf(`cache(%s) eviction policy and expiration`, name), func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cache := NewCache[string, string](ctx, 2, WithEvictionPolicy(tc.policy), WithTTLEpochGranularity(5*time.Millisecond))
cache.SetNX(`k1`, `v1`, 10*time.Millisecond)
cache.SetNX(`k2`, `v2`, 10*time.Millisecond)
_, ok := cache.Get(`k1`)
if !ok {
fail(t, `expected key dont evicted by policy`)
}
cache.SetNX(`k3`, `v3`, 30*time.Millisecond)
_, ok = cache.Get(`k3`)
if !ok {
fail(t, `expected key dont evicted by policy`)
}
_, ok = cache.Get(tc.evictedKey)
if ok {
fail(t, `expected key evicted by policy`)
}
<-time.After(12 * time.Millisecond)
cache.SetNX(`k4`, `v4`, 30*time.Millisecond) // TODO: debug
value3, ok := cache.Get(`k3`)
if !ok {
fail(t, `expected key(k3) not expired`)
}
if value3 != `v3` {
fail(t, `unexpected value %v`, value3)
}
value4, ok := cache.Get(`k4`)
if !ok {
fail(t, `expected key(k4) not expired`)
}
if value4 != `v4` {
fail(t, `unexpected value %v`, value4)
}
})
}
}
func fail(t *testing.T, msg string, args ...any) {
t.Logf(msg, args...)
t.FailNow()
}