This repository has been archived by the owner on Jul 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru_test.go
198 lines (164 loc) · 2.97 KB
/
lru_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
package lru
import (
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const (
size = 64
)
func TestLRU(t *testing.T) {
tests := []struct {
desc string
ttl int64
}{
{
desc: "with ttl of 1min",
ttl: int64((5 * time.Second) / time.Nanosecond),
},
{
desc: "with no ttl set",
ttl: 0,
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
l, err := NewLRU(size)
assert.NoError(t, err)
for i := 0; i < size; i++ {
l.Add(i, i, tt.ttl)
}
assert.Equal(t, l.Len(), size)
for i, k := range l.Keys() {
v, ok := l.Get(k)
assert.True(t, ok)
assert.Equal(t, v, k)
assert.NotEqual(t, v, i+size)
}
for i := 0; i < size; i++ {
_, ok := l.Get(i)
assert.True(t, ok)
}
for i := size; i < size*2; i++ {
_, ok := l.Get(i)
assert.False(t, ok)
}
for i := size; i < (size/2)+size; i++ {
ok := l.Remove(i)
assert.False(t, ok)
ok = l.Remove(i)
assert.False(t, ok)
_, ok = l.Get(i)
assert.False(t, ok)
}
l.Purge()
assert.Equal(t, 0, l.Len())
for i := 0; i < size; i++ {
l.Add(i, i, tt.ttl)
}
if tt.ttl > 0 {
time.Sleep(time.Duration(tt.ttl))
for i := 0; i < size; i++ {
_, ok := l.Get(i)
assert.False(t, ok)
}
}
})
}
}
func TestLRUFetch(t *testing.T) {
tests := []struct {
desc string
value interface{}
key interface{}
ttl int64
call func() (interface{}, error)
}{
{
desc: "should fetch new value without err and no ttl",
key: 1,
value: 1,
ttl: 0,
call: func() (interface{}, error) {
return 1, nil
},
},
{
desc: "should fetch new value with err and no ttl",
key: 1,
value: 1,
ttl: 0,
call: func() (interface{}, error) {
return nil, errors.New("no value")
},
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
l, err := NewLRU(1)
assert.NoError(t, err)
v, ok, err := l.Fetch(tt.key, tt.ttl, tt.call)
if err != nil {
assert.Error(t, err)
assert.Nil(t, v)
assert.False(t, ok)
return
}
assert.Equal(t, tt.value, v)
assert.False(t, ok)
assert.NoError(t, err)
})
}
}
func TestLRUAdd(t *testing.T) {
type item struct {
key interface{}
value interface{}
ttl int64
}
tests := []struct {
desc string
items []item
expectRemove bool
}{
{
desc: "should not be removed",
items: []item{
{
key: 1,
value: 1,
ttl: 0,
},
},
expectRemove: false,
},
{
desc: "should be removed",
items: []item{
{
key: 1,
value: 1,
ttl: 0,
},
{
key: 2,
value: 2,
ttl: 0,
},
},
expectRemove: true,
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
l, err := NewLRU(1)
assert.NoError(t, err)
var ok bool
for _, item := range tt.items {
ok = l.Add(item.key, item.value, item.ttl)
}
assert.Equal(t, tt.expectRemove, ok)
})
}
}