-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_test.go
More file actions
282 lines (241 loc) · 8.04 KB
/
cache_test.go
File metadata and controls
282 lines (241 loc) · 8.04 KB
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
package cache
import (
"testing"
"time"
)
func TestCache_Set(t *testing.T) {
cache := New(DefaultExpiration, 0)
t.Run("Set item with default expiration", func(t *testing.T) {
cache.Set("key1", "value1", DefaultExpiration)
if v, found := cache.Get("key1"); !found || v != "value1" {
t.Errorf("Expected to find key1 with value 'value1', got %v", v)
}
})
t.Run("Set item with no expiration", func(t *testing.T) {
cache.Set("key2", "value2", NoExpiration)
if v, found := cache.Get("key2"); !found || v != "value2" {
t.Errorf("Expected to find key2 with value 'value2', got %v", v)
}
})
t.Run("Set item with custom expiration", func(t *testing.T) {
cache.Set("key3", "value3", 100*time.Millisecond)
if v, found := cache.Get("key3"); !found || v != "value3" {
t.Errorf("Expected to find key3 with value 'value3', got %v", v)
}
// Wait for key3 to expire
time.Sleep(200 * time.Millisecond)
if _, found := cache.Get("key3"); found {
t.Error("Expected key3 to be expired")
}
})
t.Run("Overwrite existing item", func(t *testing.T) {
cache.Set("key1", "newValue1", DefaultExpiration)
if v, found := cache.Get("key1"); !found || v != "newValue1" {
t.Errorf("Expected to find key1 with value 'newValue1', got %v", v)
}
})
}
func TestCache_Add(t *testing.T) {
cache := New(DefaultExpiration, 0)
t.Run("Add item that doesn't exist", func(t *testing.T) {
err := cache.Add("key1", "value1", DefaultExpiration)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if v, found := cache.Get("key1"); !found || v != "value1" {
t.Errorf("Expected to find key1 with value 'value1', got %v", v)
}
})
t.Run("Add item that already exists", func(t *testing.T) {
err := cache.Add("key1", "newValue1", DefaultExpiration)
if err == nil {
t.Error("Expected an error, got nil")
}
if v, found := cache.Get("key1"); !found || v != "value1" {
t.Errorf("Expected to find key1 with value 'value1', got %v", v)
}
})
t.Run("Add item with no expiration", func(t *testing.T) {
err := cache.Add("key2", "value2", NoExpiration)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if v, found := cache.Get("key2"); !found || v != "value2" {
t.Errorf("Expected to find key2 with value 'value2', got %v", v)
}
})
t.Run("Add item with custom expiration", func(t *testing.T) {
err := cache.Add("key3", "value3", 100*time.Millisecond)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if v, found := cache.Get("key3"); !found || v != "value3" {
t.Errorf("Expected to find key3 with value 'value3', got %v", v)
}
// Wait for key3 to expire
time.Sleep(200 * time.Millisecond)
if _, found := cache.Get("key3"); found {
t.Error("Expected key3 to be expired")
}
})
}
func TestCache_Replace(t *testing.T) {
cache := New(DefaultExpiration, 0)
t.Run("Replace non-existing item", func(t *testing.T) {
err := cache.Replace("key1", "value1", DefaultExpiration)
if err == nil {
t.Error("Expected error when replacing non-existing item, got nil")
}
})
t.Run("Replace existing item", func(t *testing.T) {
cache.Set("key2", "value2", DefaultExpiration)
err := cache.Replace("key2", "newValue2", DefaultExpiration)
if err != nil {
t.Errorf("Expected no error when replacing existing item, got %v", err)
}
if v, found := cache.Get("key2"); !found || v != "newValue2" {
t.Errorf("Expected to find key2 with value 'newValue2', got %v", v)
}
})
t.Run("Replace expired item", func(t *testing.T) {
cache.Set("key3", "value3", 100*time.Millisecond)
time.Sleep(200 * time.Millisecond)
err := cache.Replace("key3", "newValue3", DefaultExpiration)
if err == nil {
t.Error("Expected error when replacing expired item, got nil")
}
})
}
func TestCache_Get(t *testing.T) {
cache := New(DefaultExpiration, 0)
t.Run("Get non-existing item", func(t *testing.T) {
_, found := cache.Get("key1")
if found {
t.Error("Expected not to find a non-existing item, but found one")
}
})
t.Run("Get existing item", func(t *testing.T) {
cache.Set("key2", "value2", DefaultExpiration)
v, found := cache.Get("key2")
if !found || v != "value2" {
t.Errorf("Expected to find key2 with value 'value2', got %v", v)
}
})
t.Run("Get expired item", func(t *testing.T) {
cache.Set("key3", "value3", 100*time.Millisecond)
time.Sleep(200 * time.Millisecond)
_, found := cache.Get("key3")
if found {
t.Error("Expected not to find an expired item, but found one")
}
})
}
func TestCache_GetWithExpiration(t *testing.T) {
cache := New(DefaultExpiration, 0)
t.Run("GetWithExpiration non-existing item", func(t *testing.T) {
_, _, found := cache.GetWithExpiration("key1")
if found {
t.Error("Expected not to find a non-existing item, but found one")
}
})
t.Run("GetWithExpiration existing item", func(t *testing.T) {
cache.Set("key2", "value2", DefaultExpiration)
v, exp, found := cache.GetWithExpiration("key2")
if !found || v != "value2" || !exp.IsZero() {
t.Errorf("Expected to find key2 with value 'value2' and zero expiration, got %v, %v", v, exp)
}
})
t.Run("GetWithExpiration expired item", func(t *testing.T) {
cache.Set("key3", "value3", 100*time.Millisecond)
time.Sleep(200 * time.Millisecond)
_, _, found := cache.GetWithExpiration("key3")
if found {
t.Error("Expected not to find an expired item, but found one")
}
})
t.Run("GetWithExpiration item with expiration", func(t *testing.T) {
cache.Set("key4", "value4", 5*time.Second)
v, exp, found := cache.GetWithExpiration("key4")
if !found || v != "value4" || exp.IsZero() {
t.Errorf("Expected to find key4 with value 'value4' and non-zero expiration, got %v, %v", v, exp)
}
})
}
func TestCache_Delete(t *testing.T) {
cache := New(DefaultExpiration, 0)
t.Run("Delete non-existing item", func(t *testing.T) {
cache.Delete("key1")
_, found := cache.Get("key1")
if found {
t.Error("Expected not to find a non-existing item, but found one")
}
})
t.Run("Delete existing item", func(t *testing.T) {
cache.Set("key2", "value2", DefaultExpiration)
cache.Delete("key2")
_, found := cache.Get("key2")
if found {
t.Error("Expected not to find the deleted item, but found one")
}
})
}
func TestCache_DeleteExpired(t *testing.T) {
cache := New(DefaultExpiration, 0)
t.Run("DeleteExpired with no expired items", func(t *testing.T) {
cache.Set("key1", "value1", DefaultExpiration)
cache.DeleteExpired()
_, found := cache.Get("key1")
if !found {
t.Error("Expected to find key1, but did not find it")
}
})
t.Run("DeleteExpired with expired items", func(t *testing.T) {
cache.Set("key2", "value2", 100*time.Millisecond)
time.Sleep(200 * time.Millisecond)
cache.DeleteExpired()
_, found := cache.Get("key2")
if found {
t.Error("Expected not to find the expired item, but found one")
}
})
t.Run("DeleteExpired with onEvicted callback", func(t *testing.T) {
evictedKeys := []string{}
cache.OnEvicted(func(k string, v any) {
evictedKeys = append(evictedKeys, k)
})
cache.Set("key3", "value3", 100*time.Millisecond)
cache.Set("key4", "value4", 100*time.Millisecond)
time.Sleep(200 * time.Millisecond)
cache.DeleteExpired()
if len(evictedKeys) != 2 {
t.Errorf("Expected 2 evicted keys, got %d", len(evictedKeys))
}
})
}
func TestCache_OnEvicted(t *testing.T) {
cache := New(DefaultExpiration, 0)
t.Run("Set OnEvicted handler", func(t *testing.T) {
var evictedKey string
var evictedValue any
cache.OnEvicted(func(k string, v any) {
evictedKey = k
evictedValue = v
})
cache.Set("key1", "value1", DefaultExpiration)
cache.Delete("key1")
if evictedKey != "key1" || evictedValue != "value1" {
t.Errorf("Expected evicted key to be 'key1' and value 'value1', got %v and %v", evictedKey, evictedValue)
}
})
}
func TestCache_Flush(t *testing.T) {
cache := New(DefaultExpiration, 0)
t.Run("Flush cache", func(t *testing.T) {
cache.Set("key1", "value1", DefaultExpiration)
cache.Set("key2", "value2", DefaultExpiration)
cache.Flush()
if n := cache.ItemCount(); n != 0 {
t.Errorf("Expected item count to be 0 after flush, got %d", n)
}
})
}