-
Notifications
You must be signed in to change notification settings - Fork 4
/
cache_test.go
157 lines (144 loc) · 3.46 KB
/
cache_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
package cache2go
import (
"fmt"
"sync"
"testing"
"time"
)
type myStruct struct {
data string
}
func TestCache(t *testing.T) {
cache := New(0, time.Second)
a := &myStruct{data: "mama are mere"}
cache.Set("mama", a)
b, ok := cache.Get("mama")
if !ok || b == nil || b != a {
t.Error("Error retriving data from cache", b)
}
}
func TestCacheExpire(t *testing.T) {
cache := New(0, 1*time.Millisecond)
a := &myStruct{data: "mama are mere"}
cache.Set("mama", a)
b, ok := cache.Get("mama")
if !ok || b == nil || b != a {
t.Error("Error retriving data from cache", b)
}
time.Sleep(20 * time.Millisecond)
b, ok = cache.Get("mama")
if ok || b != nil {
t.Error("Error expiring data from cache", b)
}
}
func TestLRU(t *testing.T) {
cache := New(32, 0)
for i := 0; i < 100000; i++ {
cache.Set(fmt.Sprintf("%d", i), i)
}
if cache.Len() != 32 {
t.Error("error dicarding least recently used entry: ", cache.Len())
}
last := cache.lruIndex.Back().Value.(*entry).value.(int)
if last != 99968 {
t.Error("error dicarding least recently used entry: ", last)
}
}
func TestLRUandExpire(t *testing.T) {
cache := New(32, 5*time.Millisecond)
for i := 0; i < 100000; i++ {
cache.Set(fmt.Sprintf("%d", i), i)
}
if cache.Len() != 32 {
t.Error("error dicarding least recently used entries: ", cache.Len())
}
cache.RLock()
last := cache.lruIndex.Back().Value.(*entry).value.(int)
cache.RUnlock()
if last != 99968 {
t.Error("error dicarding least recently used entry: ", last)
}
time.Sleep(30 * time.Millisecond)
if cache.Len() != 0 {
t.Error("error dicarding expired entries: ", cache.Len())
}
for i := 0; i < 100000; i++ {
cache.Set(fmt.Sprintf("%d", i), i)
}
if cache.Len() != 32 {
t.Error("error dicarding least recently used entries: ", cache.Len())
}
}
func TestLRUParallel(t *testing.T) {
cache := New(32, 0)
wg := sync.WaitGroup{}
for i := 0; i < 40; i++ {
wg.Add(1)
go func(x int) {
defer wg.Done()
cache.Set(fmt.Sprintf("%d", x), x)
}(i)
}
wg.Wait()
if cache.Len() != 32 {
t.Error("error dicarding least recently used entry: ", cache.Len())
}
}
func TestFlush(t *testing.T) {
cache := New(0, 5*time.Millisecond)
a := &myStruct{data: "mama are mere"}
cache.Set("mama", a)
time.Sleep(5 * time.Millisecond)
cache.Flush()
b, ok := cache.Get("mama")
if ok || b != nil {
t.Error("Error expiring data")
}
}
func TestRemoveElementTTLIndex(t *testing.T) {
cache := New(32, 10*time.Hour)
wg := sync.WaitGroup{}
for i := 0; i < 40; i++ {
wg.Add(1)
go func(x int) {
defer wg.Done()
cache.Set(fmt.Sprintf("%d", x), x)
}(i)
}
wg.Wait()
if cache.Len() != 32 {
t.Error("error dicarding least recently used entry: ", cache.Len())
}
}
func TestFlushNoTimeout(t *testing.T) {
cache := New(0, 5*time.Millisecond)
a := &myStruct{data: "mama are mere"}
cache.Set("mama", a)
cache.Flush()
b, ok := cache.Get("mama")
if ok || b != nil {
t.Error("Error expiring data")
}
}
func TestRemKey(t *testing.T) {
cache := New(10, 0)
cache.Set("t11_mm", "test")
if t1, ok := cache.Get("t11_mm"); !ok || t1 != "test" {
t.Error("Error setting cache")
}
cache.Delete("t11_mm")
if t1, ok := cache.Get("t11_mm"); ok || t1 == "test" {
t.Error("Error removing cached key")
}
}
func TestCount(t *testing.T) {
cache := New(0, 10*time.Millisecond)
cache.Set("dst_A1", "1")
cache.Set("dst_A2", "2")
cache.Set("rpf_A3", "3")
cache.Set("dst_A4", "4")
cache.Set("dst_A5", "5")
if cache.Len() != 5 {
t.Error("Error countiong entries: ", cache.Len())
}
}