-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcache_test.go
More file actions
222 lines (179 loc) · 4.66 KB
/
Copy pathcache_test.go
File metadata and controls
222 lines (179 loc) · 4.66 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
package cache
import (
"math/rand"
"reflect"
"strconv"
"testing"
"time"
)
func TestSet(t *testing.T) {
c := New[int]()
c.Set("1", 1)
if result, expected := c.Get("1"), 1; !reflect.DeepEqual(result, expected) {
t.Errorf("Result was %#v, expected %#v", result, expected)
}
}
func TestSetExpire(t *testing.T) {
c := New[int]()
c.Set("1", 1, Expire[int](time.Millisecond))
if _, exists := c.GetOK("1"); !exists {
t.Errorf("Entry for key '1' should not have expired yet")
}
time.Sleep(time.Millisecond * 2)
if _, exists := c.GetOK("1"); exists {
t.Errorf("Entry for key '1' should have expired by now")
}
}
func TestClear(t *testing.T) {
c := New[int]()
for i := 0; i < 10; i++ {
c.Set(strconv.Itoa(i), i)
}
c.Clear()
if keys := c.Keys(); len(keys) != 0 {
t.Errorf("Cache should have been empty, had keys: %v", keys)
}
}
func TestDelete(t *testing.T) {
c := New[int]()
c.Set("1", 1)
c.Delete("1")
if _, exists := c.GetOK("1"); exists {
t.Errorf("Entry for key '1' should not exist")
}
}
func TestClearEvery(t *testing.T) {
c := New[int]()
for i := 0; i < 10; i++ {
c.Set(strconv.Itoa(i), i)
}
c.ClearEvery(time.Millisecond)
if keys := c.Keys(); len(keys) != 10 {
t.Errorf("Cache should have had 10 keys, but had keys: %v", keys)
}
time.Sleep(time.Millisecond * 2)
if keys := c.Keys(); len(keys) != 0 {
t.Errorf("Cache should have been empty, had keys: %v", keys)
}
}
func TestGet(t *testing.T) {
c := New[int]()
c.Set("1", 1)
if result, expected := c.Get("1"), 1; !reflect.DeepEqual(result, expected) {
t.Errorf("Result for entry '1' was %#v, expected %#v", result, expected)
}
if result := c.Get("2"); result != 0 {
t.Errorf("Result for entry '2' was %#v, expected 0", result)
}
}
func TestGetOK(t *testing.T) {
c := New[int]()
c.Set("1", 1)
result, exists := c.GetOK("1")
if !exists {
t.Error("Entry for key '1' should exist")
}
if expected := 1; !reflect.DeepEqual(result, expected) {
t.Errorf("Entry for key '1' was %#v, expected %#v", result, expected)
}
if _, exists := c.GetOK("2"); exists {
t.Errorf("Entry for key '2' should not exist")
}
}
func TestItems(t *testing.T) {
c := New[int]()
for i := 0; i < 5; i++ {
c.Set(strconv.Itoa(i), i)
}
expected := map[string]int{
"0": 0,
"1": 1,
"2": 2,
"3": 3,
"4": 4,
}
if result := c.Items(); !reflect.DeepEqual(result, expected) {
t.Errorf("Result was %#v, expected %#v", result, expected)
}
}
func TestKeys(t *testing.T) {
c := New[int]()
for i := 0; i < 5; i++ {
c.Set(strconv.Itoa(i), i)
}
expected := []string{"0", "1", "2", "3", "4"}
if result := c.Keys(); !reflect.DeepEqual(result, expected) {
t.Errorf("Result was %#v, expected %#v", result, expected)
}
}
func TestStressConcurrentAccess(t *testing.T) {
c := New[int]()
c.ClearEvery(time.Nanosecond * 10)
done := make(chan bool)
for i := 0; i < 1000; i++ {
go func() {
key := strconv.Itoa(rand.Int())
switch rand.Intn(8) {
case 0:
c.Set(key, rand.Int())
case 1:
c.Set(key, rand.Int(), Expire[int](time.Nanosecond*5))
case 2:
c.Clear()
case 3:
c.Delete(key)
case 4:
c.Get(key)
case 5:
c.GetOK(key)
case 6:
c.Items()
case 7:
c.Keys()
}
done <- true
}()
}
for i := 0; i < 1000; i++ {
<-done
}
}
func benchmarkSet(count int, b *testing.B) {
c := New[int]()
for n := 0; n < b.N; n++ {
for i := 0; i < count; i++ {
c.Set(strconv.Itoa(i), i)
}
}
}
func BenchmarkSet1(b *testing.B) { benchmarkSet(1, b) }
func BenchmarkSet10(b *testing.B) { benchmarkSet(10, b) }
func BenchmarkSet100(b *testing.B) { benchmarkSet(100, b) }
func BenchmarkSet1000(b *testing.B) { benchmarkSet(1000, b) }
func BenchmarkSet10000(b *testing.B) { benchmarkSet(10000, b) }
func benchmarkDelete(count int, b *testing.B) {
c := New[int]()
for n := 0; n < b.N; n++ {
for i := 0; i < count; i++ {
c.Delete(strconv.Itoa(i))
}
}
}
func BenchmarkDelete1(b *testing.B) { benchmarkDelete(1, b) }
func BenchmarkDelete10(b *testing.B) { benchmarkDelete(10, b) }
func BenchmarkDelete100(b *testing.B) { benchmarkDelete(100, b) }
func BenchmarkDelete1000(b *testing.B) { benchmarkDelete(1000, b) }
func BenchmarkDelete10000(b *testing.B) { benchmarkDelete(10000, b) }
func benchmarkGet(count int, b *testing.B) {
c := New[int]()
for n := 0; n < b.N; n++ {
for i := 0; i < count; i++ {
c.Get(strconv.Itoa(i))
}
}
}
func BenchmarkGet1(b *testing.B) { benchmarkGet(1, b) }
func BenchmarkGet10(b *testing.B) { benchmarkGet(10, b) }
func BenchmarkGet100(b *testing.B) { benchmarkGet(100, b) }
func BenchmarkGet1000(b *testing.B) { benchmarkGet(1000, b) }
func BenchmarkGet10000(b *testing.B) { benchmarkGet(10000, b) }