Debian Codegraph @

s3fifo/s3fifo_test.go

raw symbols SCIP
1 package s3fifo
2
3 import (
4 "sync"
5 "testing"
6 "time"
7
8 "fortio.org/assert"
9 "github.com/scalalang2/golang-fifo/types"
10 )
11
12 const noEvictionTTL = 0
13
14 func TestSetAndGet(t *testing.T) {
15 cache := New[string, string](10, noEvictionTTL)
16 cache.Set("hello", "world")
17
18 value, ok := cache.Get("hello")
19 assert.True(t, ok)
20 assert.Equal(t, "world", value)
21 }
22
23 func TestRemove(t *testing.T) {
24 cache := New[int, int](10, noEvictionTTL)
25 cache.Set(1, 10)
26
27 val, ok := cache.Get(1)
28 assert.True(t, ok)
29 assert.Equal(t, 10, val)
30
31 // After removing the key, it should not be found
32 removed := cache.Remove(1)
33 assert.True(t, removed)
34
35 _, ok = cache.Get(1)
36 assert.False(t, ok)
37
38 // This should not panic
39 removed = cache.Remove(-1)
40 assert.False(t, removed)
41 }
42
43 func TestEvictOneHitWonders(t *testing.T) {
44 cache := New[int, int](10, noEvictionTTL)
45 oneHitWonders := []int{1, 2}
46 popularObjects := []int{3, 4, 5, 6, 7, 8, 9, 10}
47
48 // add objects to the cache
49 for _, v := range oneHitWonders {
50 cache.Set(v, v)
51 }
52 for _, v := range popularObjects {
53 cache.Set(v, v)
54 }
55
56 // hit one-hit wonders only once
57 for _, v := range oneHitWonders {
58 _, ok := cache.Get(v)
59 assert.True(t, ok)
60 }
61
62 // hit the popular objects
63 for i := 0; i < 3; i++ {
64 for _, v := range popularObjects {
65 _, ok := cache.Get(v)
66 assert.True(t, ok)
67 }
68 }
69
70 // add more objects to the cache
71 // this should evict the one hit wonders
72 for i := 11; i < 20; i++ {
73 cache.Set(i, i)
74 }
75
76 for _, v := range oneHitWonders {
77 _, ok := cache.Get(v)
78 assert.False(t, ok)
79 }
80
81 // popular objects should still be in the cache
82 for _, v := range popularObjects {
83 _, ok := cache.Get(v)
84 assert.True(t, ok)
85 }
86 }
87
88 func TestPeek(t *testing.T) {
89 cache := New[int, int](5, noEvictionTTL)
90 entries := []int{1, 2, 3, 4, 5}
91
92 for _, v := range entries {
93 cache.Set(v, v*10)
94 }
95
96 // peek each entry 10 times
97 // this should not change the recent-ness of the entry.
98 for i := 0; i < 10; i++ {
99 for _, v := range entries {
100 value, exist := cache.Peek(v)
101 assert.True(t, exist)
102 assert.Equal(t, v*10, value)
103 }
104 }
105
106 // add more entries to the cache
107 // this should evict the first 5 entries
108 for i := 6; i <= 10; i++ {
109 cache.Set(i, i*10)
110 }
111
112 // peek the first 5 entries
113 // they should not exist in the cache
114 for _, v := range entries {
115 _, exist := cache.Peek(v)
116 assert.False(t, exist)
117 }
118 }
119
120 func TestContains(t *testing.T) {
121 cache := New[int, int](5, noEvictionTTL)
122 entries := []int{1, 2, 3, 4, 5}
123
124 for _, v := range entries {
125 cache.Set(v, v*10)
126 }
127
128 // check if each entry exists in the cache
129 for _, v := range entries {
130 assert.True(t, cache.Contains(v))
131 }
132
133 for i := 6; i <= 10; i++ {
134 assert.False(t, cache.Contains(i))
135 }
136 }
137
138 func TestLength(t *testing.T) {
139 cache := New[string, string](10, noEvictionTTL)
140
141 cache.Set("hello", "world")
142 assert.Equal(t, 1, cache.Len())
143
144 cache.Set("hello2", "world")
145 cache.Set("hello", "changed")
146 assert.Equal(t, 2, cache.Len())
147
148 value, ok := cache.Get("hello")
149 assert.True(t, ok)
150 assert.Equal(t, "changed", value)
151 }
152
153 func TestClean(t *testing.T) {
154 cache := New[int, int](10, noEvictionTTL)
155 entries := []int{1, 2, 3, 4, 5}
156
157 for _, v := range entries {
158 cache.Set(v, v*10)
159 }
160 assert.Equal(t, 5, cache.Len())
161 cache.Purge()
162
163 // check if each entry exists in the cache
164 for _, v := range entries {
165 _, exist := cache.Peek(v)
166 assert.False(t, exist)
167 }
168 assert.Equal(t, 0, cache.Len())
169 }
170
171 func TestTimeToLive(t *testing.T) {
172 ttl := time.Second
173 cache := New[int, int](10, ttl)
174 numberOfEntries := 10
175
176 for num := 1; num <= numberOfEntries; num++ {
177 cache.Set(num, num)
178 val, ok := cache.Get(num)
179 assert.True(t, ok)
180 assert.Equal(t, num, val)
181 }
182
183 time.Sleep(ttl * 2)
184
185 // check all entries are evicted
186 for num := 1; num <= numberOfEntries; num++ {
187 _, ok := cache.Get(num)
188 assert.False(t, ok)
189 }
190 }
191
192 func TestEvictionCallback(t *testing.T) {
193 cache := New[int, int](10, noEvictionTTL)
194 evicted := make(map[int]int)
195
196 cache.SetOnEvicted(func(key int, value int, _ types.EvictReason) {
197 evicted[key] = value
198 })
199
200 // add objects to the cache
201 for i := 1; i <= 10; i++ {
202 cache.Set(i, i)
203 }
204
205 // add another object to the cache
206 cache.Set(11, 11)
207
208 // check the first object is evicted
209 _, ok := cache.Get(1)
210 assert.False(t, ok)
211 assert.Equal(t, 1, evicted[1])
212
213 cache.Close()
214 }
215
216 func TestEvictionCallbackWithTTL(t *testing.T) {
217 var mu sync.Mutex
218 cache := New[int, int](10, time.Second)
219 evicted := make(map[int]int)
220 cache.SetOnEvicted(func(key int, value int, _ types.EvictReason) {
221 mu.Lock()
222 evicted[key] = value
223 mu.Unlock()
224 })
225
226 // add objects to the cache
227 for i := 1; i <= 10; i++ {
228 cache.Set(i, i)
229 }
230
231 timeout := time.After(5 * time.Second)
232 ticker := time.NewTicker(100 * time.Millisecond)
233 for {
234 select {
235 case <-timeout:
236 t.Fatal("timeout")
237 case <-ticker.C:
238 mu.Lock()
239 if len(evicted) == 10 {
240 for i := 1; i <= 10; i++ {
241 assert.Equal(t, i, evicted[i])
242 }
243 return
244 }
245 mu.Unlock()
246 }
247 }
248 }