-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcache_composition_test.go
More file actions
249 lines (208 loc) · 8.21 KB
/
cache_composition_test.go
File metadata and controls
249 lines (208 loc) · 8.21 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
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
249
package hot
import (
"testing"
"github.com/samber/hot/pkg/arc"
"github.com/samber/hot/pkg/base"
"github.com/samber/hot/pkg/fifo"
"github.com/samber/hot/pkg/lfu"
"github.com/samber/hot/pkg/lru"
"github.com/samber/hot/pkg/metrics"
"github.com/samber/hot/pkg/safe"
"github.com/samber/hot/pkg/sharded"
"github.com/samber/hot/pkg/twoqueue"
"github.com/stretchr/testify/assert"
)
func mockCollectorBuilder(shard int) metrics.Collector {
return &metrics.NoOpCollector{}
}
func TestComposeInternalCache(t *testing.T) {
is := assert.New(t)
t.Parallel()
// Test LRU with locking
cache := composeInternalCache[string, int](true, LRU, 42, 0, -1, nil, nil, nil)
is.Equal(42, cache.Capacity())
is.Equal("lru", cache.Algorithm())
_, ok := cache.(*safe.SafeInMemoryCache[string, *item[int]])
is.True(ok)
// Test LFU with locking
cache = composeInternalCache[string, int](true, LFU, 42, 0, -1, nil, nil, nil)
is.Equal(42, cache.Capacity())
is.Equal("lfu", cache.Algorithm())
_, ok = cache.(*safe.SafeInMemoryCache[string, *item[int]])
is.True(ok)
// Test TwoQueue with locking
cache = composeInternalCache[string, int](true, TwoQueue, 42, 0, -1, nil, nil, nil)
is.Equal(42, cache.Capacity())
is.Equal("2q", cache.Algorithm())
_, ok = cache.(*safe.SafeInMemoryCache[string, *item[int]])
is.True(ok)
// Test ARC with locking
cache = composeInternalCache[string, int](true, ARC, 42, 0, -1, nil, nil, nil)
is.Equal(42, cache.Capacity())
is.Equal("arc", cache.Algorithm())
_, ok = cache.(*safe.SafeInMemoryCache[string, *item[int]])
is.True(ok)
// Test FIFO with locking
cache = composeInternalCache[string, int](true, FIFO, 42, 0, -1, nil, nil, nil)
is.Equal(42, cache.Capacity())
is.Equal("fifo", cache.Algorithm())
_, ok = cache.(*safe.SafeInMemoryCache[string, *item[int]])
is.True(ok)
// Test invalid capacity (should panic)
is.Panics(func() {
_ = composeInternalCache[string, int](true, ARC, 0, 0, -1, nil, nil, nil)
})
// Test LRU without locking
cache = composeInternalCache[string, int](false, LRU, 42, 0, -1, nil, nil, nil)
is.Equal(42, cache.Capacity())
is.Equal("lru", cache.Algorithm())
_, ok = cache.(*safe.SafeInMemoryCache[string, *item[int]])
is.False(ok)
_, ok = cache.(*lru.LRUCache[string, *item[int]])
is.True(ok)
// Test LFU without locking
cache = composeInternalCache[string, int](false, LFU, 42, 0, -1, nil, nil, nil)
is.Equal(42, cache.Capacity())
is.Equal("lfu", cache.Algorithm())
_, ok = cache.(*safe.SafeInMemoryCache[string, *item[int]])
is.False(ok)
_, ok = cache.(*lfu.LFUCache[string, *item[int]])
is.True(ok)
// Test TwoQueue without locking
cache = composeInternalCache[string, int](false, TwoQueue, 42, 0, -1, nil, nil, nil)
is.Equal(42, cache.Capacity())
is.Equal("2q", cache.Algorithm())
_, ok = cache.(*safe.SafeInMemoryCache[string, *item[int]])
is.False(ok)
_, ok = cache.(*twoqueue.TwoQueueCache[string, *item[int]])
is.True(ok)
// Test ARC without locking
cache = composeInternalCache[string, int](false, ARC, 42, 0, -1, nil, nil, nil)
is.Equal(42, cache.Capacity())
is.Equal("arc", cache.Algorithm())
_, ok = cache.(*safe.SafeInMemoryCache[string, *item[int]])
is.False(ok)
_, ok = cache.(*arc.ARCCache[string, *item[int]])
is.True(ok)
// Test FIFO without locking
cache = composeInternalCache[string, int](false, FIFO, 42, 0, -1, nil, nil, nil)
is.Equal(42, cache.Capacity())
is.Equal("fifo", cache.Algorithm())
_, ok = cache.(*safe.SafeInMemoryCache[string, *item[int]])
is.False(ok)
_, ok = cache.(*fifo.FIFOCache[string, *item[int]])
is.True(ok)
// Test invalid capacity without locking (should panic)
is.Panics(func() {
_ = composeInternalCache[string, int](false, ARC, 0, 0, -1, nil, nil, nil)
})
}
func TestComposeInternalCacheWithSharding(t *testing.T) {
is := assert.New(t)
t.Parallel()
hashFn := func(key string) uint64 { return uint64(len(key)) }
shards := uint64(4)
capacity := 42
// Test sharded cache with locking
cache := composeInternalCache[string, int](true, LRU, capacity, shards, -1, hashFn, nil, nil)
is.Equal(capacity*int(shards), cache.Capacity())
is.Equal("lru", cache.Algorithm())
_, ok := cache.(*sharded.ShardedInMemoryCache[string, *item[int]])
is.True(ok)
// Test sharded cache without locking
cache = composeInternalCache[string, int](false, LRU, capacity, shards, -1, hashFn, nil, nil)
is.Equal(capacity*int(shards), cache.Capacity())
is.Equal("lru", cache.Algorithm())
_, ok = cache.(*sharded.ShardedInMemoryCache[string, *item[int]])
is.True(ok)
// Test invalid sharding configuration (should panic)
is.Panics(func() {
_ = composeInternalCache[string, int](true, LRU, capacity, shards, -1, nil, nil, nil)
})
}
func TestComposeInternalCacheWithEvictionCallback(t *testing.T) {
is := assert.New(t)
t.Parallel()
evictionCallback := func(reason base.EvictionReason, key string, value int) {
// Callback implementation for testing
}
// Test with eviction callback
cache := composeInternalCache[string, int](false, LRU, 42, 0, -1, nil, evictionCallback, nil)
is.Equal(42, cache.Capacity())
is.Equal("lru", cache.Algorithm())
// Test that the callback is properly wrapped
item := &item[int]{value: 123}
cache.Set("test-key", item)
cache.Delete("test-key") // This should trigger eviction callback
// Note: The actual eviction callback behavior depends on the underlying cache implementation
// We're just testing that the cache is created successfully with the callback
}
func TestComposeInternalCacheWithMetrics(t *testing.T) {
is := assert.New(t)
t.Parallel()
// Test with metrics collector
cache := composeInternalCache[string, int](false, LRU, 42, 0, 0, nil, nil, mockCollectorBuilder)
is.Equal(42, cache.Capacity())
is.Equal("lru", cache.Algorithm())
// Should be an InstrumentedCache
_, isInstrumented := cache.(*metrics.InstrumentedCache[string, *item[int]])
is.True(isInstrumented)
// Test with metrics and locking
cache = composeInternalCache[string, int](true, LRU, 42, 0, 0, nil, nil, mockCollectorBuilder)
is.Equal(42, cache.Capacity())
is.Equal("lru", cache.Algorithm())
_, isSafe := cache.(*safe.SafeInMemoryCache[string, *item[int]])
is.False(isSafe)
_, isInstrumented = cache.(*metrics.InstrumentedCache[string, *item[int]])
is.True(isInstrumented)
}
func TestComposeInternalCacheWithShardingAndMetrics(t *testing.T) {
is := assert.New(t)
t.Parallel()
hashFn := func(key string) uint64 { return uint64(len(key)) }
shards := uint64(4)
capacity := 42
// Test sharded cache with metrics
cache := composeInternalCache[string, int](false, LRU, capacity, shards, -1, hashFn, nil, mockCollectorBuilder)
is.Equal(capacity*int(shards), cache.Capacity())
is.Equal("lru", cache.Algorithm())
_, ok := cache.(*sharded.ShardedInMemoryCache[string, *item[int]])
is.True(ok)
// Test sharded cache with metrics and locking
cache = composeInternalCache[string, int](true, LRU, capacity, shards, -1, hashFn, nil, mockCollectorBuilder)
is.Equal(capacity*int(shards), cache.Capacity())
is.Equal("lru", cache.Algorithm())
_, ok = cache.(*sharded.ShardedInMemoryCache[string, *item[int]])
is.True(ok)
}
func TestComposeInternalCacheUnknownAlgorithm(t *testing.T) {
is := assert.New(t)
t.Parallel()
// Test unknown algorithm (should panic)
is.Panics(func() {
_ = composeInternalCache[string, int](false, "unknown", 42, 0, -1, nil, nil, nil)
})
}
func TestComposeInternalCacheEdgeCases(t *testing.T) {
is := assert.New(t)
t.Parallel()
// Test with negative capacity (should panic)
is.Panics(func() {
_ = composeInternalCache[string, int](false, LRU, -1, 0, -1, nil, nil, nil)
})
// Test with zero shards (should work)
cache := composeInternalCache[string, int](false, LRU, 42, 0, -1, nil, nil, nil)
is.Equal(42, cache.Capacity())
is.Equal("lru", cache.Algorithm())
// Test with one shard (should work, treated as no sharding)
cache = composeInternalCache[string, int](false, LRU, 42, 1, -1, nil, nil, nil)
is.Equal(42, cache.Capacity())
is.Equal("lru", cache.Algorithm())
_, ok := cache.(*sharded.ShardedInMemoryCache[string, *item[int]])
is.False(ok)
// Test with shards > 1 and shardingFn provided (should work)
hashFn := func(key string) uint64 { return uint64(len(key)) }
cache = composeInternalCache[string, int](false, LRU, 10, 3, -1, hashFn, nil, nil)
is.Equal(30, cache.Capacity())
is.Equal("lru", cache.Algorithm())
}