-
Notifications
You must be signed in to change notification settings - Fork 14
/
ttlru_test.go
248 lines (196 loc) · 4.83 KB
/
ttlru_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
package ttlru
import (
"container/heap"
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestGeneral(t *testing.T) {
l := New(128, WithTTL(2*time.Second))
require.NotNil(t, l)
require.Equal(t, 0, l.Len())
require.Equal(t, 128, l.Cap())
for i := 0; i < 128; i++ {
require.False(t, l.Set(i, i))
}
require.Equal(t, 128, l.Len())
require.Equal(t, 128, l.Cap())
for i := 128; i < 256; i++ {
require.True(t, l.Set(i, i))
}
require.Equal(t, 128, l.Len())
require.Equal(t, 128, l.Cap())
for _, k := range l.Keys() {
v, ok := l.Get(k)
require.True(t, ok)
require.Equal(t, v, k)
}
for i := 0; i < 128; i++ {
val, ok := l.Get(i)
require.False(t, ok)
require.Nil(t, val)
}
for i := 128; i < 256; i++ {
val, ok := l.Get(i)
require.True(t, ok)
require.Equal(t, val, i)
}
for i := 128; i < 192; i++ {
require.True(t, l.Del(i))
val, ok := l.Get(i)
require.False(t, ok)
require.Nil(t, val)
}
done := make(chan interface{})
time.AfterFunc(3*time.Second, func() {
require.Equal(t, 0, l.Len())
require.Equal(t, 128, l.Cap())
require.Equal(t, 0, l.Len())
require.Equal(t, 128, l.Cap())
require.False(t, l.Set(0, 0))
require.Equal(t, 1, l.Len())
require.Equal(t, 128, l.Cap())
l.Purge()
require.Equal(t, 0, l.Len())
require.Equal(t, 128, l.Cap())
val, ok := l.Get(200)
require.False(t, ok)
require.Nil(t, val)
done <- true
})
<-done
}
func TestAddReturnsProperly(t *testing.T) {
l := New(1, WithTTL(2*time.Second))
require.NotNil(t, l)
require.Equal(t, 0, l.Len())
require.Equal(t, 1, l.Cap())
require.False(t, l.Set(1, 1))
require.Equal(t, 1, l.Len())
require.Equal(t, 1, l.Cap())
require.True(t, l.Set(2, 2))
require.Equal(t, 1, l.Len())
require.Equal(t, 1, l.Cap())
}
func TestInvalidCreation(t *testing.T) {
require.Nil(t, New(0, WithTTL(1)))
require.Nil(t, New(-1, WithTTL(1)))
require.Nil(t, New(1, WithTTL(-1)))
}
func TestSetShouldAlsoUpdate(t *testing.T) {
l := New(1, WithTTL(2*time.Second))
require.NotNil(t, l)
require.Equal(t, 0, l.Len())
require.Equal(t, 1, l.Cap())
require.False(t, l.Set(1, 1))
require.Equal(t, 1, l.Len())
require.Equal(t, 1, l.Cap())
v, ok := l.Get(1)
require.True(t, ok)
require.Equal(t, 1, v)
require.False(t, l.Set(1, 2))
require.Equal(t, 1, l.Len())
require.Equal(t, 1, l.Cap())
v, ok = l.Get(1)
require.True(t, ok)
require.Equal(t, 2, v)
}
func TestDeleteShouldReturnProperly(t *testing.T) {
l := New(1, WithTTL(2*time.Second))
require.NotNil(t, l)
require.Equal(t, 0, l.Len())
require.Equal(t, 1, l.Cap())
require.False(t, l.Set(1, 1))
require.Equal(t, 1, l.Len())
require.Equal(t, 1, l.Cap())
v, ok := l.Get(1)
require.True(t, ok)
require.Equal(t, 1, v)
require.True(t, l.Del(1))
require.False(t, l.Del(2))
}
func TestItemShouldExpireDespiteGet(t *testing.T) {
l := New(1, WithTTL(300*time.Millisecond), WithoutReset())
require.NotNil(t, l)
require.False(t, l.Set(1, 1))
done := make(chan interface{})
time.AfterFunc(200*time.Millisecond, func() {
v, ok := l.Get(1)
require.True(t, ok)
require.Equal(t, 1, v)
done <- true
})
<-done
time.AfterFunc(200*time.Millisecond, func() {
v, ok := l.Get(1)
require.False(t, ok)
require.Nil(t, v)
done <- true
})
<-done
}
func TestWithoutTTL(t *testing.T) {
l := New(2)
require.NotNil(t, l)
require.False(t, l.Set(1, 1))
v, ok := l.Get(1)
require.True(t, ok)
require.Equal(t, 1, v)
require.False(t, l.Set(2, 2))
v, ok = l.Get(2)
require.True(t, ok)
require.Equal(t, 2, v)
require.True(t, l.Set(3, 3))
v, ok = l.Get(3)
require.True(t, ok)
require.Equal(t, 3, v)
v, ok = l.Get(1)
require.False(t, ok)
require.Nil(t, v)
v, ok = l.Get(2)
require.True(t, ok)
require.Equal(t, 2, v)
}
func TestTTLAfterPurge(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
defer cancel()
l := New(1, WithTTL(10*time.Millisecond))
l.Set("bug", "foo")
l.Purge()
// the bug caused a panic here in a different goroutine, so it couldn't be
// recovered in the test.
// if the test completes successfully, then there was obviously no panic
<-ctx.Done()
}
func TestPopEmptyHeap(t *testing.T) {
var h ttlHeap
heap.Push(&h, &entry{value: 1})
heap.Pop(&h)
defer func() {
if r := recover(); r != nil {
t.Errorf("recovered from panic: %+v", r)
}
}()
heap.Pop(&h)
}
func TestFullCacheTTLEviction(t *testing.T) {
// Test that when an item with TTL is evicted, the TTL timer is cancelled
// Otherwise, the key will be deleted earlier than it should
l := New(5, WithTTL(1*time.Second))
for i := 0; i < 6; i++ {
l.Set(i, "test")
if i == 0 {
time.Sleep(500 * time.Millisecond)
}
}
// 0 should have been evicted
_, ok := l.Get(0)
require.False(t, ok)
// add 0 back in
l.Set(0, "test")
time.Sleep(500 * time.Millisecond)
// 0 should still be there
_, ok = l.Get(0)
require.True(t, ok)
}