-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFIFO_test.go
74 lines (56 loc) · 1.29 KB
/
FIFO_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
package cacheutils
import (
"fmt"
"math/rand"
"strconv"
"testing"
"time"
)
func TestFIFO(t *testing.T) {
fifo := NewFIFO[string](20)
fifo.Set("a", "s111_a")
fifo.Set("b", "s111_b")
v, err := fifo.Get("a")
if err != nil || v != "s111_a" {
t.Error(err)
}
r := rand.New(rand.NewSource(time.Now().UnixMilli()))
testData := make(map[string]string)
for i := 0; i < 2100; i++ {
k := strconv.Itoa(i)
v := strconv.Itoa(i + r.Int())
fifo.Set(k, v)
testData[k] = v
// t.Log("size: ", fifo.GetSize())
}
for _, k := range fifo.Keys() {
result, err := fifo.Get(k)
if testData[k] != result || err != nil {
t.Error("error", err)
}
}
for i := 0; i < 20; i++ {
k := strconv.Itoa(i)
v := strconv.Itoa(i + r.Int())
// expire after 10 second
if i >= 10 {
fifo.SetWithTimeout(k, v, time.Now().Add(1*time.Second))
} else {
fifo.SetWithTimeout(k, v, time.Now().Add(3*time.Second))
}
testData[k] = v
}
fmt.Println("before size: ", fifo.GetSize())
time.Sleep(1 * time.Second)
fifo.Prune()
fmt.Println("after prune size: ", fifo.GetSize())
if fifo.GetSize() != 10 {
t.Error("timeout test error")
}
time.Sleep(2 * time.Second)
fifo.Prune()
fmt.Println("after prune size: ", fifo.GetSize())
if fifo.GetSize() != 0 {
t.Error("0, timeout test error")
}
}