-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
377 lines (307 loc) · 7.12 KB
/
main_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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package gocollections
import (
"errors"
"fmt"
"math/rand"
"strconv"
"testing"
"time"
)
func TestTimeExpiredList(t *testing.T) {
t.Parallel()
want := "value1"
tlist := NewTimeExpiredList[string](1 * time.Second)
defer tlist.Discard()
tlist.Add(want)
size := tlist.Size()
if size != 1 {
t.Fatalf("Expext one element in collection. But size is: %d", size)
}
got, err := tlist.Get(0)
if err != nil {
t.Fatal(err)
}
if got != want {
t.Fatalf("want: %s, got: %s", want, got)
}
time.Sleep(5 * time.Second)
size = tlist.Size()
if size != 0 {
t.Fatalf("Expecting no element in collection. But got size: %d", size)
}
}
func TestTimeExpiredList_GetAll(t *testing.T) {
t.Parallel()
tlist := NewTimeExpiredList[string](5 * time.Second)
defer tlist.Discard()
tlist.Add("value1")
tlist.Add("value2")
tlist.Add("value3")
tlist.Add("value4")
tlist.Add("value5")
values := tlist.GetAll()
if len(values) != 5 {
t.Fatalf("Expected 5 values. Got: %d", len(values))
}
}
func TestTimeExpiredList_Del(t *testing.T) {
t.Parallel()
tlist := NewTimeExpiredList[string](600 * time.Second)
//defer tlist.Discard()
tlist.Add("value1")
tlist.Add("value2")
tlist.Add("value3")
tlist.Add("value4")
tlist.Add("value5")
// remove value3 element
_ = tlist.Del(2)
size := len(tlist.GetAll())
if size != 4 {
t.Fatalf("Expect size to be 4. But got: %d", size)
}
for _, v := range tlist.GetAll() {
if v == "value3" {
t.Fatalf("We expecte 'value3' is removed, but it was present")
}
}
// remove first element value1
_ = tlist.Del(0)
size = len(tlist.GetAll())
if size != 3 {
t.Fatalf("Expect size to be 4. But got: %d", size)
}
for _, v := range tlist.GetAll() {
if v == "value1" {
t.Fatalf("We expecte 'value3' is removed, but it was present")
}
}
// remove last element value5
_ = tlist.Del(tlist.Size() - 1)
size = len(tlist.GetAll())
if size != 2 {
t.Fatalf("Expect size to be 4. But got: %d", size)
}
for _, v := range tlist.GetAll() {
if v == "value5" {
t.Fatalf("We expecte 'value3' is removed, but it was present")
}
}
// remove last element
_ = tlist.Del(tlist.Size() - 1)
size = len(tlist.GetAll())
if size != 1 {
t.Fatalf("Expect size to be 4. But got: %d", size)
}
// remove last element
_ = tlist.Del(tlist.Size() - 1)
size = len(tlist.GetAll())
if size != 0 {
t.Fatalf("Expect size to be 4. But got: %d", size)
}
// remove last element from empty list
err := tlist.Del(0)
if !errors.Is(err, ErrIndexOutOfBound) {
t.Fatalf("Expect ErrIndexOutOfBound but got %s", err.Error())
}
}
func TestTimeExpiredList_Clear(t *testing.T) {
t.Parallel()
tlist := NewTimeExpiredList[string](600 * time.Second)
defer tlist.Discard()
// Add items to the list.
tlist.Add("value1")
tlist.Add("value2")
tlist.Add("value3")
tlist.Add("value4")
tlist.Add("value5")
if tlist.Size() != 5 {
t.Errorf("Size should be 5, but was: %d", tlist.Size())
}
tlist.Clear()
if tlist.Size() != 0 {
t.Errorf("List size after clear should be 0, but was: %d", tlist.Size())
}
}
func TestTimeExpiredList_ExpiredElChan(t *testing.T) {
t.Parallel()
tlist := NewTimeExpiredList[string](100*time.Millisecond, Config{
CleanJobInterval: 200 * time.Millisecond,
ExpiredElChanSize: 1,
})
defer tlist.Discard()
// Add item
tlist.Add("value_1")
timeout := time.After(1 * time.Second)
exElChan := tlist.ExpiredElChan()
select {
case el := <-exElChan:
fmt.Println("Element", el)
case <-timeout:
t.Error("No expired element in timeout")
return
}
}
func TestTimeExpiredMap(t *testing.T) {
t.Parallel()
var want int
var got int
var key = "1"
var value = "test 1"
tmap := NewTimeExpiredMap[string, string](1 * time.Second)
defer tmap.Discard()
t.Run("Size", func(t *testing.T) {
want = 0
got = tmap.Size()
if want != got {
t.Errorf("want: %d, got: %d", want, got)
}
})
t.Run("Add", func(t *testing.T) {
tmap.Add(key, value)
want = 1
got = tmap.Size()
if want != got {
t.Errorf("want: %d, got: %d", want, got)
}
})
t.Run("Contains", func(t *testing.T) {
c := tmap.Contains(key)
if !c {
t.Errorf("key is not in the map")
}
})
t.Run("Get", func(t *testing.T) {
val, err := tmap.Get(key)
if err != nil {
t.Error(err)
}
if value != val {
t.Errorf("want: %s, got: %s", value, val)
}
})
t.Run("Expired", func(t *testing.T) {
time.Sleep(2 * time.Second)
want = 0
got = tmap.Size()
if want != got {
t.Errorf("want: %d, got: %d", want, got)
}
c := tmap.Contains(key)
if c {
t.Error("contains, `key is in the map, but should expire")
}
})
}
func TestLoad(t *testing.T) {
t.Parallel()
var count = 10000
tmap := NewTimeExpiredMap[string, string](2 * time.Second)
defer tmap.Discard()
for i := 1; i < count+1; i++ {
tmap.Add(strconv.Itoa(i), fmt.Sprintf("TEST %d", i))
}
if tmap.Size() != count {
t.Fatalf("We expect %d number of elemets, got: %d", count, tmap.Size())
}
time.Sleep(4 * time.Second)
if tmap.Size() != 0 {
t.Fatalf("We expect all elements expired but size of map is %d", tmap.Size())
}
}
func TestTimeExpiredMap_Del(t *testing.T) {
t.Parallel()
var want int
var got int
tmap := NewTimeExpiredMap[string, string](2 * time.Second)
defer tmap.Discard()
tmap.Add("1", "test 1")
want = 1
got = tmap.Size()
if want != got {
t.Errorf("want: %d, got: %d", want, got)
}
err := tmap.Del("1")
if err != nil {
t.Fatal(err)
}
want = 0
got = tmap.Size()
if want != got {
t.Errorf("want: %d, got: %d", want, got)
}
}
func TestTimeExpiredMap_AddWithDuration(t *testing.T) {
t.Parallel()
var want int
var got int
tmap := NewTimeExpiredMap[string, string](1 * time.Second)
defer tmap.Discard()
tmap.AddWithDuration("1", "test 1", 5*time.Second)
want = 1
got = tmap.Size()
if want != got {
t.Errorf("want: %d, got: %d", want, got)
}
time.Sleep(2 * time.Second)
want = 1
got = tmap.Size()
if want != got {
t.Errorf("want: %d, got: %d", want, got)
}
time.Sleep(5 * time.Second)
want = 0
got = tmap.Size()
if want != got {
t.Errorf("want: %d, got: %d", want, got)
}
}
func TestTimeExpiredMap_Clear(t *testing.T) {
t.Parallel()
var want int
var got int
tmap := NewTimeExpiredMap[string, string](10 * time.Second)
defer tmap.Discard()
tmap.Add("1", "test 1")
want = 1
got = tmap.Size()
if want != got {
t.Errorf("want: %d, got: %d", want, got)
}
tmap.Clear()
if tmap.Size() > 0 {
t.Errorf("map is not cleared.")
}
}
func TestTimeExpiredMap_ClearExeption(t *testing.T) {
t.Parallel()
tmap := NewTimeExpiredMap[int, int](1 * time.Second)
defer tmap.Discard()
startTime := time.Now()
i := 0
for {
tmap.Add(i, rand.Int())
i++
if time.Now().Sub(startTime) > 10*time.Second {
break
}
}
}
func TestTimeExpiredMap_ExpiredElChan(t *testing.T) {
t.Parallel()
tmap := NewTimeExpiredMap[string, string](100*time.Millisecond, Config{
CleanJobInterval: 200 * time.Millisecond,
ExpiredElChanSize: 100,
})
defer tmap.Discard()
// Add item
tmap.Add("key_1", "value_1")
timeout := time.After(1 * time.Second)
exElChan := tmap.ExpiredElChan()
select {
case el := <-exElChan:
fmt.Println("Element", el)
case <-timeout:
t.Error("No expired element in timeout")
return
}
}