-
Notifications
You must be signed in to change notification settings - Fork 2
/
mcache_test.go
314 lines (243 loc) · 6.67 KB
/
mcache_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
package incache
import (
"fmt"
"testing"
"time"
)
func TestSet(t *testing.T) {
c := NewManual[string, string](10, 0)
c.Set("key1", "value1")
if c.m["key1"].value != "value1" {
t.Errorf("Set failed")
}
}
func TestNotFoundSet(t *testing.T) {
c := NewManual[string, string](10, 0)
key := "key1"
value := "value1"
ok := c.NotFoundSet(key, value)
if !ok {
t.Error("Expected NotFoundSet to return true for a new key")
}
v, ok := c.Get(key)
if !ok || v != value {
t.Error("Expected value to be added using NotFoundSet")
}
ok = c.NotFoundSet(key, value)
if ok {
t.Error("Expected NotFoundSet to return false for an existing key")
}
}
func TestNotFoundSetWithTimeout(t *testing.T) {
c := NewManual[string, string](10, 0)
key := "key1"
value := "value1"
timeout := time.Second
ok := c.NotFoundSetWithTimeout(key, value, timeout)
if !ok {
t.Error("Expected NotFoundSetWithTimeout to return true for a new key")
}
v, ok := c.Get(key)
if !ok || v != value {
t.Error("Expected value to be added using NotFoundSetWithTimeout")
}
ok = c.NotFoundSetWithTimeout(key, value, timeout)
if ok {
t.Error("Expected NotFoundSetWithTimeout to return false for an existing key")
}
ok = c.NotFoundSetWithTimeout("key2", "value2", timeout)
if !ok {
t.Error("Expected NotFoundSetWithTimeout to return true for a new key with timeout")
}
time.Sleep(c.timeInterval + timeout)
_, ok = c.Get("key2")
if ok {
t.Error("Expected value to be expired and removed after the specified timeout")
}
ok = c.NotFoundSetWithTimeout("key3", "value3", -time.Second)
if !ok {
t.Error("Expected NotFoundSetWithTimeout to return true for a new key with negative timeout")
}
}
func TestSetWithTimeout(t *testing.T) {
c := NewManual[string, string](10, 0)
key := "test"
value := "test value"
timeout := time.Second
c.SetWithTimeout(key, value, timeout)
v, ok := c.Get(key)
if value != v || !ok {
t.Errorf("SetWithTimeout failed")
}
time.Sleep(time.Second)
v, ok = c.Get(key)
if v != "" || ok {
t.Errorf("SetWithTimeout failed")
}
}
func TestGet(t *testing.T) {
c := NewManual[string, string](10, 0)
c.Set("key1", "value1")
value, ok := c.Get("key1")
if !ok || value != "value1" {
t.Errorf("Get returned unexpected value for key1")
}
_, ok = c.Get("nonexistent")
if ok {
t.Errorf("Get returned true for a non-existent key")
}
}
func TestGetAll(t *testing.T) {
c := NewManual[string, string](10, 0)
c.Set("key1", "value1")
c.Set("key2", "value2")
c.SetWithTimeout("key3", "value3", time.Millisecond)
if m := c.GetAll(); len(m) != 3 {
t.Errorf("GetAll returned unexpected number of keys: %d", len(m))
}
time.Sleep(time.Millisecond * 2)
if m := c.GetAll(); len(m) != 2 {
t.Errorf("GetAll returned unexpected number of keys: %d", len(m))
}
}
func TestDelete(t *testing.T) {
c := NewManual[string, string](10, 0)
c.Set("key1", "value1")
c.Delete("key1")
_, ok := c.Get("key1")
if ok {
t.Errorf("Get returned true for a deleted key")
}
}
func TestTransferTo(t *testing.T) {
src := NewManual[string, string](10, 0)
dst := NewManual[string, string](10, 0)
src.Set("key1", "value1")
src.TransferTo(dst)
value, ok := dst.Get("key1")
if !ok || value != "value1" {
t.Errorf("TransferTo did not transfer data correctly")
}
_, ok = src.Get("key1")
if ok {
t.Errorf("TransferTo did not clear source database")
}
}
func TestCopyTo(t *testing.T) {
src := NewManual[string, string](10, 0)
dst := NewManual[string, string](10, 0)
src.Set("key1", "value1")
src.CopyTo(dst)
value, ok := dst.Get("key1")
if !ok || value != "value1" {
t.Errorf("CopyTo did not copy data correctly")
}
value, ok = src.Get("key1")
if !ok || value != "value1" {
t.Errorf("CopyTo modified the source database")
}
}
func TestKeys(t *testing.T) {
c := NewManual[string, string](10, 0)
c.Set("key1", "value1")
c.Set("key2", "value2")
c.SetWithTimeout("key3", "value3", 1)
c.SetWithTimeout("key4", "value4", 1)
c.SetWithTimeout("key5", "value5", 1)
c.Set("key6", "value6")
keys := c.Keys()
if len(keys) != 3 {
t.Errorf("Unexpected number of keys returned")
}
expectedKeys := map[string]bool{"key1": true, "key2": true, "key6": true}
for _, key := range keys {
if !expectedKeys[key] {
t.Errorf("Unexpected key %s returned", key)
}
}
}
func TestPurge(t *testing.T) {
c := NewManual[string, string](10, time.Millisecond*100)
c.Set("key1", "value1")
c.Set("key2", "value2")
c.SetWithTimeout("key3", "value3", 1)
c.SetWithTimeout("key4", "value4", 1)
c.SetWithTimeout("key5", "value5", 1)
c.Set("key6", "value6")
c.Purge()
select {
case _, ok := <-c.stopCh:
if ok {
t.Errorf("Close: expiration goroutine did not stop as expected")
}
default:
t.Errorf("Close: expiration goroutine did not stop as expected")
}
if len(c.m) != 0 {
t.Errorf("Close: database map is not cleaned up")
}
}
func TestCount(t *testing.T) {
c := NewManual[int, string](10, 0)
c.Set(1, "one")
c.Set(2, "two")
c.SetWithTimeout(3, "three", time.Millisecond*100)
c.SetWithTimeout(4, "four", time.Millisecond*100)
c.SetWithTimeout(5, "five", time.Millisecond*100)
count := c.Count()
if count != 5 {
t.Errorf("Count: expected: %d, got: %d", 5, count)
}
time.Sleep(time.Millisecond * 300)
count = c.Count()
if count != 2 {
t.Errorf("Count: expected: %d, got: %d", 2, count)
}
c = NewManual[int, string](10, 0)
c.Set(1, "one")
c.Set(2, "two")
c.SetWithTimeout(3, "three", time.Millisecond*100)
c.SetWithTimeout(4, "four", time.Millisecond*100)
c.SetWithTimeout(5, "five", time.Millisecond*100)
if count := c.Count(); count != 5 {
t.Errorf("Count: expected: %d, got: %d", 5, count)
}
time.Sleep(time.Millisecond * 300)
if count := c.Count(); count != 2 {
t.Errorf("Count: expected: %d, got: %d", 2, count)
}
}
func TestLen(t *testing.T) {
c := NewManual[string, string](10, 0)
c.Set("1", "one")
c.Set("2", "two")
c.SetWithTimeout("3", "three", time.Millisecond*100)
c.SetWithTimeout("4", "four", time.Millisecond*100)
if l := c.Len(); l != 4 {
t.Errorf("Len: expected: %d, got: %d", 4, l)
}
c = NewManual[string, string](10, time.Millisecond*100)
c.Set("1", "one")
c.Set("2", "two")
c.SetWithTimeout("3", "three", time.Millisecond*50)
c.SetWithTimeout("4", "four", time.Millisecond*50)
time.Sleep(time.Millisecond * 150)
if l := c.Len(); l != 2 {
t.Errorf("Len: expected: %d, got: %d", 2, l)
}
}
func TestEvict(t *testing.T) {
c := NewManual[string, string](10, 0)
c.Set("1", "one")
c.Set("2", "two")
c.Set("3", "three")
c.Set("4", "four")
fmt.Println(c.Keys())
if count := c.Count(); count != 4 {
t.Errorf("Count: expected: %d, got: %d", 4, count)
}
c.evict(3)
if count := c.Count(); count != 1 {
t.Errorf("Count: expected: %d, got: %d", 1, count)
}
}