Debian Codegraph @

sieve/sieve_test.go

raw symbols SCIP
1 package sieve
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 TestGetAndSet(t *testing.T) {
15 items := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
16 cache := New[int, int](10, noEvictionTTL)
17
18 for _, v := range items {
19 cache.Set(v, v*10)
20 }
21
22 for _, v := range items {
23 val, ok := cache.Get(v)
24 assert.True(t, ok)
25 assert.Equal(t, v*10, val)
26 }
27
28 cache.Close()
29 }
30
31 func TestRemove(t *testing.T) {
32 cache := New[int, int](10, noEvictionTTL)
33 cache.Set(1, 10)
34
35 val, ok := cache.Get(1)
36 assert.True(t, ok)
37 assert.Equal(t, 10, val)
38
39 // After removing the key, it should not be found
40 removed := cache.Remove(1)
41 assert.True(t, removed)
42
43 _, ok = cache.Get(1)
44 assert.False(t, ok)
45
46 // This should not panic
47 removed = cache.Remove(-1)
48 assert.False(t, removed)
49
50 cache.Close()
51 }
52
53 func TestSievePolicy(t *testing.T) {
54 cache := New[int, int](10, noEvictionTTL)
55 oneHitWonders := []int{1, 2, 3, 4, 5}
56 popularObjects := []int{6, 7, 8, 9, 10}
57
58 // add objects to the cache
59 for _, v := range oneHitWonders {
60 cache.Set(v, v)
61 }
62 for _, v := range popularObjects {
63 cache.Set(v, v)
64 }
65
66 // hit popular objects
67 for _, v := range popularObjects {
68 _, ok := cache.Get(v)
69 assert.True(t, ok)
70 }
71
72 // add another objects to the cache
73 for _, v := range oneHitWonders {
74 cache.Set(v*10, v*10)
75 }
76
77 // check popular objects are not evicted
78 for _, v := range popularObjects {
79 _, ok := cache.Get(v)
80 assert.True(t, ok)
81 }
82
83 cache.Close()
84 }
85
86 func TestContains(t *testing.T) {
87 cache := New[string, string](10, noEvictionTTL)
88 assert.False(t, cache.Contains("hello"))
89
90 cache.Set("hello", "world")
91 assert.True(t, cache.Contains("hello"))
92
93 cache.Close()
94 }
95
96 func TestLen(t *testing.T) {
97 cache := New[int, int](10, noEvictionTTL)
98 assert.Equal(t, 0, cache.Len())
99
100 cache.Set(1, 1)
101 assert.Equal(t, 1, cache.Len())
102
103 // duplicated keys only update the recent-ness of the key and value
104 cache.Set(1, 1)
105 assert.Equal(t, 1, cache.Len())
106
107 cache.Set(2, 2)
108 assert.Equal(t, 2, cache.Len())
109
110 cache.Close()
111 }
112
113 func TestPurge(t *testing.T) {
114 cache := New[int, int](10, noEvictionTTL)
115 cache.Set(1, 1)
116 cache.Set(2, 2)
117 assert.Equal(t, 2, cache.Len())
118
119 cache.Purge()
120 assert.Equal(t, 0, cache.Len())
121
122 cache.Close()
123 }
124
125 func TestTimeToLive(t *testing.T) {
126 ttl := time.Second
127 cache := New[int, int](10, ttl)
128 numberOfEntries := 10
129
130 for num := 1; num <= numberOfEntries; num++ {
131 cache.Set(num, num)
132 val, ok := cache.Get(num)
133 assert.True(t, ok)
134 assert.Equal(t, num, val)
135 }
136
137 time.Sleep(ttl * 2)
138
139 // check all entries are evicted
140 for num := 1; num <= numberOfEntries; num++ {
141 _, ok := cache.Get(num)
142 assert.False(t, ok)
143 }
144 }
145
146 func TestEvictionCallback(t *testing.T) {
147 cache := New[int, int](10, noEvictionTTL)
148 evicted := make(map[int]int)
149
150 cache.SetOnEvicted(func(key int, value int, _ types.EvictReason) {
151 evicted[key] = value
152 })
153
154 // add objects to the cache
155 for i := 1; i <= 10; i++ {
156 cache.Set(i, i)
157 }
158
159 // add another object to the cache
160 cache.Set(11, 11)
161
162 // check the first object is evicted
163 _, ok := cache.Get(1)
164 assert.False(t, ok)
165 assert.Equal(t, 1, evicted[1])
166
167 cache.Close()
168 }
169
170 func TestEvictionCallbackWithTTL(t *testing.T) {
171 var mu sync.Mutex
172 cache := New[int, int](10, time.Second)
173 evicted := make(map[int]int)
174 cache.SetOnEvicted(func(key int, value int, _ types.EvictReason) {
175 mu.Lock()
176 evicted[key] = value
177 mu.Unlock()
178 })
179
180 // add objects to the cache
181 for i := 1; i <= 10; i++ {
182 cache.Set(i, i)
183 }
184
185 timeout := time.After(5 * time.Second)
186 ticker := time.NewTicker(100 * time.Millisecond)
187 for {
188 select {
189 case <-timeout:
190 t.Fatal("timeout")
191 case <-ticker.C:
192 mu.Lock()
193 if len(evicted) == 10 {
194 for i := 1; i <= 10; i++ {
195 assert.Equal(t, i, evicted[i])
196 }
197 return
198 }
199 mu.Unlock()
200 }
201 }
202 }
203
204 func TestLargerWorkloadsThanCacheSize(t *testing.T) {
205 type value struct {
206 bytes []byte
207 }
208
209 cache := New[int32, value](512, time.Millisecond)
210 workload := int32(10240)
211 for i := int32(0); i < workload; i++ {
212 val := value{
213 bytes: make([]byte, 10),
214 }
215 cache.Set(i, val)
216
217 v, ok := cache.Get(i)
218 assert.True(t, ok)
219 assert.Equal(t, v, val)
220 }
221 }