This repository was archived by the owner on Apr 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool_test.go
More file actions
375 lines (317 loc) · 10.3 KB
/
pool_test.go
File metadata and controls
375 lines (317 loc) · 10.3 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package gencache
import (
"context"
"errors"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
var errKeyNotFound = errors.New("key not found")
func TestObjectPool(t *testing.T) {
t.Run("Basic Operations", func(t *testing.T) {
pool := NewObjectPool(func() []byte {
return make([]byte, 1024)
})
// Test Get and Put
obj1 := pool.Get()
require.NotNil(t, obj1, "Get should return a non-nil object")
require.Len(t, obj1, 1024, "Object should have correct size")
pool.Put(obj1)
obj2 := pool.Get()
require.NotNil(t, obj2, "Get should return a non-nil object after Put")
require.Len(t, obj2, 1024, "Object should have correct size after Put")
})
t.Run("Concurrent Operations", func(t *testing.T) {
pool := NewObjectPool(func() []byte {
return make([]byte, 1024)
})
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
obj := pool.Get()
require.NotNil(t, obj, "Get should return a non-nil object")
pool.Put(obj)
}()
}
wg.Wait()
})
}
func TestEntryPool(t *testing.T) {
t.Run("Basic Operations", func(t *testing.T) {
pool := NewEntryPool[string]()
// Test Get and Put
entry1 := pool.Get()
require.Equal(t, int64(0), entry1.AccessCount, "New entry should have zero access count")
entry1.AccessCount = 5
pool.Put(entry1)
entry2 := pool.Get()
require.Equal(t, int64(0), entry2.AccessCount, "Reused entry should have reset access count")
})
t.Run("Concurrent Operations", func(t *testing.T) {
pool := NewEntryPool[string]()
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
entry := pool.Get()
entry.AccessCount++
pool.Put(entry)
}()
}
wg.Wait()
})
}
func TestBatchResultPool(t *testing.T) {
t.Run("Basic Operations", func(t *testing.T) {
pool := NewBatchResultPool[string, int]()
// Test Get and Put
result1 := pool.Get()
require.Nil(t, result1.Error, "New result should have nil error")
result1.Error = errKeyNotFound
pool.Put(result1)
result2 := pool.Get()
require.Nil(t, result2.Error, "Reused result should have reset error")
})
t.Run("Concurrent Operations", func(t *testing.T) {
pool := NewBatchResultPool[string, int]()
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
result := pool.Get()
result.Error = errKeyNotFound
pool.Put(result)
}()
}
wg.Wait()
})
}
// --- Mock cache for pooled cache tests ---
type pooledMockCache[K comparable, V any] struct {
data map[K]V
mu sync.RWMutex
}
func newPooledMockCache[K comparable, V any]() *pooledMockCache[K, V] {
return &pooledMockCache[K, V]{data: make(map[K]V)}
}
func (m *pooledMockCache[K, V]) Get(key K) (V, error) {
m.mu.RLock()
defer m.mu.RUnlock()
v, ok := m.data[key]
if !ok {
var zero V
return zero, errKeyNotFound
}
return v, nil
}
func (m *pooledMockCache[K, V]) Set(key K, value V, ttl time.Duration) error {
m.mu.Lock()
defer m.mu.Unlock()
m.data[key] = value
return nil
}
func (m *pooledMockCache[K, V]) Delete(key K) error {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.data, key)
return nil
}
func (m *pooledMockCache[K, V]) GetWithContext(ctx context.Context, key K) (V, error) {
return m.Get(key)
}
func (m *pooledMockCache[K, V]) SetWithContext(ctx context.Context, key K, value V, ttl time.Duration) error {
return m.Set(key, value, ttl)
}
func (m *pooledMockCache[K, V]) DeleteWithContext(ctx context.Context, key K) error {
return m.Delete(key)
}
func (m *pooledMockCache[K, V]) Clear() error {
m.mu.Lock()
defer m.mu.Unlock()
m.data = make(map[K]V)
return nil
}
func (m *pooledMockCache[K, V]) ClearWithContext(ctx context.Context) error {
return m.Clear()
}
func (m *pooledMockCache[K, V]) Stats() *Stats { return &Stats{} }
func (m *pooledMockCache[K, V]) OnEvent(cb CacheCallback[K, V]) {}
func (m *pooledMockCache[K, V]) Close() error {
return nil
}
func TestPooledCache(t *testing.T) {
t.Run("Basic Operations", func(t *testing.T) {
mock := newPooledMockCache[string, *int]()
config := DefaultPoolConfig()
pc := NewPooledCache[string, int](mock, config)
t.Cleanup(func() { pc.Close() })
// Test Set and Get
v1 := 100
err := pc.Set("key1", v1, time.Hour)
require.NoError(t, err)
value, err := pc.Get("key1")
require.NoError(t, err)
require.Equal(t, v1, value, "Value should match what was set")
// Test Delete
err = pc.Delete("key1")
require.NoError(t, err)
_, err = pc.Get("key1")
require.Error(t, err, "Value should be deleted")
})
t.Run("Pool Statistics", func(t *testing.T) {
mock := newPooledMockCache[string, *int]()
config := DefaultPoolConfig()
config.CleanupPeriod = 100 * time.Millisecond
pc := NewPooledCache[string, int](mock, config)
t.Cleanup(func() { pc.Close() })
// Test initial stats
stats := pc.GetPoolStats()
require.Equal(t, int64(config.MaxSize), stats.MaxSize.Load(), "MaxSize should match config")
require.Equal(t, int64(config.MinSize), stats.MinSize.Load(), "MinSize should match config")
require.Equal(t, int64(0), stats.TotalCreated.Load(), "Initial TotalCreated should be 0")
require.Equal(t, int64(0), stats.TotalDestroyed.Load(), "Initial TotalDestroyed should be 0")
// Test stats after operations
v1 := 100
v2 := 200
err := pc.Set("key1", v1, time.Hour)
require.NoError(t, err)
err = pc.Set("key2", v2, time.Hour)
require.NoError(t, err)
_, err = pc.Get("key1")
require.NoError(t, err)
_, err = pc.Get("key2")
require.NoError(t, err)
// Force the pool to create new objects
pool := pc.(interface{ TestGetFromPool() any })
for i := 0; i < 5; i++ {
pool.TestGetFromPool()
}
// Directly call the pool's Get method to ensure TotalCreated increases
for i := 0; i < 5; i++ {
_ = pool.TestGetFromPool()
time.Sleep(10 * time.Millisecond) // Small delay to avoid race conditions
stats = pc.GetPoolStats()
require.Greater(t, stats.TotalCreated.Load(), int64(0), "TotalCreated should increase")
}
// Wait for cleanup
time.Sleep(200 * time.Millisecond)
// Check final stats
stats = pc.GetPoolStats()
require.LessOrEqual(t, stats.CurrentSize.Load(), int64(config.MaxSize), "Pool size should be within limits")
})
t.Run("Cleanup", func(t *testing.T) {
mock := newPooledMockCache[string, *int]()
config := DefaultPoolConfig()
config.MaxSize = 5
config.MinSize = 2
config.CleanupPeriod = 100 * time.Millisecond // Shorter period
config.ShrinkFactor = 0.5
pc := NewPooledCache[string, int](mock, config)
// Fill the pool beyond max size
for i := 0; i < 8; i++ { // Reduced to 8 items
v := i
err := pc.Set("key"+string(rune(i)), v, time.Hour)
require.NoError(t, err)
}
// Get initial stats
initialStats := pc.GetPoolStats()
initialSize := initialStats.CurrentSize.Load()
require.Greater(t, initialSize, int64(config.MaxSize), "Initial size should be greater than max size")
// Wait for cleanup
time.Sleep(300 * time.Millisecond)
// Check if pool was cleaned up
stats := pc.GetPoolStats()
require.LessOrEqual(t, stats.CurrentSize.Load(), int64(config.MaxSize), "Pool size should be reduced after cleanup")
require.Less(t, stats.CurrentSize.Load(), initialSize, "Pool size should be smaller after cleanup")
// Close the cache
err := pc.Close()
require.NoError(t, err, "Close should not timeout")
// Verify final stats
finalStats := pc.GetPoolStats()
require.LessOrEqual(t, finalStats.CurrentSize.Load(), int64(config.MaxSize), "Final pool size should be within limits")
})
t.Run("Concurrent Operations", func(t *testing.T) {
mock := newPooledMockCache[string, *int]()
config := DefaultPoolConfig()
pc := NewPooledCache[string, int](mock, config)
t.Cleanup(func() { pc.Close() })
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
key := "key" + string(rune(i))
err := pc.Set(key, i, time.Hour)
require.NoError(t, err)
value, err := pc.Get(key)
require.NoError(t, err)
require.Equal(t, i, value, "Value should match what was set")
}(i)
}
wg.Wait()
})
}
func TestPoolStats(t *testing.T) {
mock := newPooledMockCache[string, *string]()
config := DefaultPoolConfig()
pc := NewPooledCache[string, string](mock, config)
// Perform some operations
v1 := "value1"
v2 := "value2"
err := pc.Set("test1", v1, time.Hour)
require.NoError(t, err)
err = pc.Set("test2", v2, time.Hour)
require.NoError(t, err)
_, err = pc.Get("test1")
require.NoError(t, err)
_, err = pc.Get("test2")
require.NoError(t, err)
// Check stats
// Explicitly exercise the pool to ensure stats are updated
pool := pc.(interface{ TestGetFromPool() any })
for i := 0; i < 5; i++ {
_ = pool.TestGetFromPool()
}
stats := pc.GetPoolStats()
require.Greater(t, stats.TotalCreated.Load(), int64(0), "Should record created objects")
require.Greater(t, stats.CurrentSize.Load(), int64(0), "Should record current size")
require.Equal(t, int64(config.MaxSize), stats.MaxSize.Load(), "Should record max size")
require.Equal(t, int64(config.MinSize), stats.MinSize.Load(), "Should record min size")
// Test concurrent stats updates
const goroutines = 10
const iterations = 1000
var wg sync.WaitGroup
wg.Add(goroutines)
for i := 0; i < goroutines; i++ {
go func() {
defer wg.Done()
for j := 0; j < iterations; j++ {
v := "value"
err := pc.Set("test", v, time.Hour)
require.NoError(t, err)
_, err = pc.Get("test")
require.NoError(t, err)
}
}()
}
wg.Wait()
// Verify final stats
stats = pc.GetPoolStats()
require.Greater(t, stats.TotalCreated.Load(), int64(0), "Should record created objects")
require.Greater(t, stats.CurrentSize.Load(), int64(0), "Should record current size")
require.Equal(t, int64(config.MaxSize), stats.MaxSize.Load(), "Should record max size")
require.Equal(t, int64(config.MinSize), stats.MinSize.Load(), "Should record min size")
// Test stats reset
pc.ResetPoolStats()
stats = pc.GetPoolStats()
require.Equal(t, int64(0), stats.TotalCreated.Load(), "Stats should be reset")
require.Equal(t, int64(0), stats.TotalDestroyed.Load(), "Stats should be reset")
require.Equal(t, int64(0), stats.CurrentSize.Load(), "Stats should be reset")
require.Equal(t, int64(config.MaxSize), stats.MaxSize.Load(), "Max size should be preserved")
require.Equal(t, int64(config.MinSize), stats.MinSize.Load(), "Min size should be preserved")
}