-
Notifications
You must be signed in to change notification settings - Fork 2
/
ttl_test.go
206 lines (166 loc) · 4.52 KB
/
ttl_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
package geche
import (
"context"
"strconv"
"testing"
"time"
)
func TestTTL(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := NewMapTTLCache[string, string](ctx, time.Second, time.Second)
c.Set("key", "value")
ts := time.Now()
// Check we can get the value.
val, err := c.Get("key")
if err != nil {
t.Errorf("unexpected error in Get: %v", err)
}
if val != "value" {
t.Errorf("expected value %q, but got %v", "value", val)
}
// Yep, accessing private variables in the test. So what?
c.mux.Lock()
c.now = func() time.Time {
return ts.Add(time.Second)
}
c.mux.Unlock()
// Check the value does not exist.
if _, err := c.Get("key"); err != ErrNotFound {
t.Errorf("expected error %v, but got %v", ErrNotFound, err)
}
// Outdated value is still in the cache until cleanup is called.
if len(c.data) != 1 {
t.Errorf("expected cache data len to be 1 but got %d", len(c.data))
}
}
// TestTTLSequence checks linked list works as expected.
func TestTTLSequence(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := NewMapTTLCache[string, string](ctx, time.Second, 0)
for i := 0; i < 10; i++ {
s := strconv.Itoa(i)
c.Set(s, s)
}
s := ""
for k := c.head; k != ""; k = c.data[k].next {
s = s + c.data[k].value
}
expected := "0123456789"
if s != expected {
t.Errorf("expected sequence %q, got %q", expected, s)
}
_ = c.Del("0")
_ = c.Del("5")
c.Set("7", "7")
s = ""
for k := c.head; k != ""; k = c.data[k].next {
s = s + c.data[k].value
}
expected = "12346897"
if s != expected {
t.Errorf("expected sequence %q, got %q", expected, s)
}
}
func TestTTLCleanup(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := NewMapTTLCache[string, string](ctx, time.Millisecond, time.Millisecond*5)
c.Set("key", "value")
// Check we can get the value.
val, err := c.Get("key")
if err != nil {
t.Errorf("unexpected error in Get: %v", err)
}
if val != "value" {
t.Errorf("expected value %q, but got %v", "value", val)
}
time.Sleep(time.Millisecond * 10)
// Check the value does not exist.
if _, err := c.Get("key"); err != ErrNotFound {
t.Errorf("expected error %v, but got %v", ErrNotFound, err)
}
// Cleanup should have purged the cache.
if len(c.data) != 0 {
t.Errorf("expected cache data len to be 0 but got %d", len(c.data))
}
}
func TestTTLScenario(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := NewMapTTLCache[string, string](ctx, time.Millisecond, time.Millisecond*5)
for i := 0; i < 10; i++ {
s := strconv.Itoa(i)
c.Set(s, s)
}
ts := time.Now()
// Yep, accessing private variables in the test. So what?
c.mux.Lock()
c.now = func() time.Time {
return ts.Add(time.Second)
}
c.mux.Unlock()
for i := 0; i < 10; i++ {
if i%2 == 0 {
continue
}
s := strconv.Itoa(i)
c.Set(s, s)
}
// Wait 10ms to be sure that cleanup goroutine runs
// at least once even on slow CI runner.
time.Sleep(time.Millisecond * 10)
c.mux.Lock()
// Half of the records should be removed by now.
if len(c.data) != 5 {
t.Errorf("expected cache data len to be 5 but got %d", len(c.data))
}
c.mux.Unlock()
// Check we can get odd values, but not even.
for i := 0; i < 10; i++ {
s := strconv.Itoa(i)
val, err := c.Get(s)
if i%2 == 0 {
if err != ErrNotFound {
t.Errorf("expected to get %v but got %v", ErrNotFound, err)
}
continue
}
if err != nil {
t.Errorf("unexpected error in Get: %v", err)
}
if val != s {
t.Errorf("expected value %q, but got %v", s, val)
}
}
time.Sleep(time.Millisecond * 5)
// Cleanup should remove all even values.
if len(c.data) != 5 {
t.Errorf("expected cache data len to be 5 but got %d", len(c.data))
}
}
func TestSetIfPresentResetsTTL(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := NewMapTTLCache[string, string](ctx, time.Second, time.Second)
ts := time.Now()
c.Set("key", "value")
c.mux.Lock()
c.now = func() time.Time { return ts.Add(time.Second) }
c.mux.Unlock()
old, inserted := c.SetIfPresent("key", "value2")
if !inserted {
t.Errorf("expected key to be set as it is present in the map, but SetIfPresent returned false")
}
if old != "value" {
t.Errorf("expected old value %q, but got %q", "value", old)
}
v, err := c.Get("key")
if err != nil {
t.Errorf("unexpected error in Get: %v", err)
}
if v != "value2" {
t.Errorf("value was not updated by SetIfPresent, expected %v, but got %v", "value2", v)
}
}