forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
212 lines (205 loc) · 4.88 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
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
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cache
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewCache(t *testing.T) {
type args struct {
size int
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "create a new Cache with length 10",
args: args{
size: 10,
},
wantErr: false,
},
{
name: "create a new Cache with length -1",
args: args{
size: -1,
},
wantErr: true,
},
{
name: "create a new Cache with length 0",
args: args{
size: 0,
},
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
_, err := NewCache[string, string](tt.args.size)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
})
}
}
func TestCache_Get(t *testing.T) {
tests := []struct {
name string
lruCache func() *Cache[string, string]
evictedItems map[string]string
key string
wantValue string
wantOk bool
}{
{
name: "if key is not found in LRUCache, will get key from evictedItems",
lruCache: func() *Cache[string, string] {
cache, _ := NewCache[string, string](1)
cache.evictedItems["key"] = "val"
return cache
},
key: "key",
wantValue: "val",
wantOk: true,
},
{
name: "if key is found in LRUCache, return the found item",
lruCache: func() *Cache[string, string] {
cache, _ := NewCache[string, string](1)
cache.Add("key", "val_from_LRU")
cache.evictedItems["key"] = "val_from_evicted_items"
return cache
},
key: "key",
wantValue: "val_from_LRU",
wantOk: true,
},
{
name: "if key is not found either in LRUCache or evicted items, return nothing",
lruCache: func() *Cache[string, string] {
cache, _ := NewCache[string, string](1)
return cache
},
key: "key",
wantValue: "",
wantOk: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
c := tt.lruCache()
gotValue, gotOk := c.Get(tt.key)
if !assert.Equal(t, gotValue, tt.wantValue) {
t.Errorf("Get() gotValue = %v, want %v", gotValue, tt.wantValue)
}
if gotOk != tt.wantOk {
t.Errorf("Get() gotOk = %v, want %v", gotOk, tt.wantOk)
}
})
}
}
func TestCache_RemoveEvictedItems(t *testing.T) {
tests := []struct {
name string
lruCache func() (*Cache[string, string], error)
}{
{
name: "no panic when there is no evicted item to remove",
lruCache: func() (*Cache[string, string], error) {
return NewCache[string, string](1)
},
},
{
name: "evicted items should be removed",
lruCache: func() (*Cache[string, string], error) {
cache, err := NewCache[string, string](1)
if err != nil {
return nil, err
}
cache.evictedItems["key0"] = "val0"
cache.evictedItems["key1"] = "val1"
return cache, nil
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cache, err := tt.lruCache()
assert.NoError(t, err)
cache.RemoveEvictedItems()
assert.Empty(t, cache.evictedItems)
})
}
}
func TestCache_PurgeItems(t *testing.T) {
tests := []struct {
name string
lruCache func() (*Cache[string, string], error)
}{
{
name: "no panic when there is no item to remove",
lruCache: func() (*Cache[string, string], error) {
return NewCache[string, string](1)
},
},
{
name: "remove items from the lru cache",
lruCache: func() (*Cache[string, string], error) {
cache, err := NewCache[string, string](1)
if err != nil {
return nil, err
}
cache.evictedItems["key0"] = "val0"
cache.evictedItems["key1"] = "val1"
return cache, nil
},
},
{
name: "remove all the items from lru cache and the evicted items",
lruCache: func() (*Cache[string, string], error) {
cache, err := NewCache[string, string](10)
if err != nil {
return nil, err
}
cache.Add("key", "val")
cache.Add("key2", "val2")
cache.evictedItems["key0"] = "val0"
cache.evictedItems["key1"] = "val1"
return cache, nil
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cache, err := tt.lruCache()
assert.NoError(t, err)
cache.Purge()
assert.Zero(t, cache.Len())
assert.Empty(t, cache.evictedItems)
})
}
}