-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompression_test.go
More file actions
645 lines (556 loc) · 18.8 KB
/
compression_test.go
File metadata and controls
645 lines (556 loc) · 18.8 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
package gencache
import (
"bytes"
"context"
"errors"
"fmt"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// mockCacheAny is for interface{} value type, used in compression tests
type mockCacheAny[K comparable] struct {
store map[K]any
mu sync.RWMutex
stats *Stats
}
func newMockCacheAny[K comparable]() *mockCacheAny[K] {
return &mockCacheAny[K]{store: make(map[K]any), stats: &Stats{}}
}
func (m *mockCacheAny[K]) Get(key K) (any, error) {
m.mu.RLock()
defer m.mu.RUnlock()
val, ok := m.store[key]
if !ok {
return nil, errors.New("not found")
}
return val, nil
}
func (m *mockCacheAny[K]) Set(key K, value any, ttl time.Duration) error {
m.mu.Lock()
defer m.mu.Unlock()
m.store[key] = value
return nil
}
func (m *mockCacheAny[K]) Delete(key K) error {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.store, key)
return nil
}
func (m *mockCacheAny[K]) Capacity() int64 {
m.mu.RLock()
defer m.mu.RUnlock()
return int64(len(m.store))
}
func (m *mockCacheAny[K]) Clear() error {
m.mu.Lock()
defer m.mu.Unlock()
m.store = make(map[K]any)
return nil
}
func (m *mockCacheAny[K]) Size() int {
m.mu.RLock()
defer m.mu.RUnlock()
return len(m.store)
}
func (m *mockCacheAny[K]) Close() error {
return nil
}
func (m *mockCacheAny[K]) ClearWithContext(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
m.mu.Lock()
m.store = make(map[K]any)
m.mu.Unlock()
return nil
}
}
func (m *mockCacheAny[K]) DeleteMany(ctx context.Context, keys []K) error {
for _, key := range keys {
select {
case <-ctx.Done():
return ctx.Err()
default:
m.mu.Lock()
delete(m.store, key)
m.mu.Unlock()
}
}
return nil
}
func (m *mockCacheAny[K]) DeleteWithContext(ctx context.Context, key K) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
m.mu.Lock()
delete(m.store, key)
m.mu.Unlock()
return nil
}
}
func (m *mockCacheAny[K]) GetMany(ctx context.Context, keys []K) map[K]any {
result := make(map[K]any)
for _, key := range keys {
select {
case <-ctx.Done():
return result
default:
m.mu.RLock()
if val, ok := m.store[key]; ok {
result[key] = val
}
m.mu.RUnlock()
}
}
return result
}
func (m *mockCacheAny[K]) GetWithContext(ctx context.Context, key K) (any, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
return m.Get(key)
}
}
func (m *mockCacheAny[K]) MaxMemory() int64 {
return 0 // Mock implementation returns 0 for unlimited memory
}
func (m *mockCacheAny[K]) MemoryUsage() int64 {
return 0 // Mock implementation returns 0 for memory usage
}
func (m *mockCacheAny[K]) ResetStats() {
// Mock implementation does nothing
}
func (m *mockCacheAny[K]) SetMany(ctx context.Context, entries map[K]any, ttl time.Duration) error {
for key, value := range entries {
select {
case <-ctx.Done():
return ctx.Err()
default:
m.mu.Lock()
m.store[key] = value
m.mu.Unlock()
}
}
return nil
}
func (m *mockCacheAny[K]) SetWithContext(ctx context.Context, key K, value any, ttl time.Duration) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
m.mu.Lock()
m.store[key] = value
m.mu.Unlock()
return nil
}
}
func (m *mockCacheAny[K]) GetAll() map[K]any {
m.mu.RLock()
defer m.mu.RUnlock()
result := make(map[K]any, len(m.store))
for k, v := range m.store {
result[k] = v
}
return result
}
func (m *mockCacheAny[K]) GetStats() StatsSnapshot {
return StatsSnapshot{
Size: int64(len(m.store)),
Capacity: 0,
Hits: 0,
Misses: 0,
Evictions: 0,
PanicCount: 0,
LastPanic: time.Time{},
LastOperationDuration: 0,
}
}
func (m *mockCacheAny[K]) SetCapacity(capacity int64) {
// No-op for mock cache
}
func (m *mockCacheAny[K]) OnEvent(callback CacheCallback[K, any]) {
// No-op for mock cache
}
func (m *mockCacheAny[K]) RemoveEventCallback(callback CacheCallback[K, any]) {
// No-op for mock cache
}
func (m *mockCacheAny[K]) Stats() *Stats {
return m.stats
}
func TestNewCompressedCache(t *testing.T) {
mock := newMockCacheAny[string]()
config := DefaultCompressionConfig()
cc := NewCompressedCache[string, any](mock, config)
require.NotNil(t, cc, "NewCompressedCache should not return nil")
require.Implements(t, (*CompressedCache[string, any])(nil), cc, "Should implement CompressedCache interface")
}
func TestCompressionAlgorithms(t *testing.T) {
tests := []struct {
name string
algorithm CompressionAlgorithm
data []byte
}{
{
name: "Gzip Compression",
algorithm: GzipCompression,
data: bytes.Repeat([]byte("test data for compression"), 100),
},
{
name: "Zlib Compression",
algorithm: ZlibCompression,
data: bytes.Repeat([]byte("test data for compression"), 100),
},
{
name: "LZW Compression",
algorithm: LZWCompression,
data: bytes.Repeat([]byte("test data for compression"), 100),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mock := newMockCacheAny[string]()
config := DefaultCompressionConfig()
config.Algorithm = tt.algorithm
cc := NewCompressedCache[string, any](mock, config)
// Test compression
err := cc.Set("test", tt.data, time.Hour)
require.NoError(t, err, "Set should not return error")
value, err := mock.Get("test")
require.NoError(t, err, "Compressed data should be stored")
_, isCompressed := extractCompressedValue(value)
require.True(t, isCompressed, "Compressed data should be stored as compressedValue")
// Test decompression
decompressed, err := cc.Get("test")
require.NoError(t, err, "Should be able to retrieve data")
require.True(t, bytes.Equal(tt.data, decompressed.([]byte)), "Decompressed data should match original")
})
}
}
func TestCompressionSizeLimits(t *testing.T) {
mock := newMockCacheAny[string]()
config := DefaultCompressionConfig()
config.MinSize = 100
config.MaxSize = 1000
cc := NewCompressedCache[string, []byte](mock, config)
// Test data smaller than MinSize
smallData := bytes.Repeat([]byte("small"), 10)
err := cc.Set("small", smallData, time.Hour)
require.NoError(t, err, "Set should not return error")
value, err := mock.Get("small")
require.NoError(t, err, "Get should not return error")
require.True(t, bytes.Equal(smallData, value.([]byte)), "Small data should not be compressed")
// Test data within size limits
mediumData := bytes.Repeat([]byte("medium"), 100) // 6 bytes * 100 = 600 bytes
err = cc.Set("medium", mediumData, time.Hour)
require.NoError(t, err, "Set should not return error")
value, err = mock.Get("medium")
require.NoError(t, err, "Get should not return error")
// Check if the value is compressed
cv, isCompressed := extractCompressedValue(value)
require.True(t, isCompressed, "Medium data should be compressed")
require.NotNil(t, cv.Data, "Compressed data should not be nil")
require.Equal(t, GzipCompression, cv.Algorithm, "Should use default compression algorithm")
// Verify we can get the original data back
decompressed, err := cc.Get("medium")
require.NoError(t, err, "Should be able to retrieve data")
require.True(t, bytes.Equal(mediumData, decompressed), "Decompressed data should match original")
// Test data larger than MaxSize
largeData := bytes.Repeat([]byte("large"), 2000)
err = cc.Set("large", largeData, time.Hour)
require.NoError(t, err, "Set should not return error")
value, err = mock.Get("large")
require.NoError(t, err, "Get should not return error")
require.True(t, bytes.Equal(largeData, value.([]byte)), "Large data should not be compressed")
}
func TestCompressionStats(t *testing.T) {
// Use a real cache to preserve types for compression stats
base := New[string, any](WithMaxSize[string, any](100))
config := DefaultCompressionConfig()
config.StatsEnabled = true
config.MinSize = 10 // Set a small min size for testing
cc := NewCompressedCache[string, []byte](base, config)
// Perform some operations
data := bytes.Repeat([]byte("test data for compression"), 100)
err := cc.Set("test1", data, time.Hour)
require.NoError(t, err, "Set should not return error")
err = cc.Set("test2", data, time.Hour)
require.NoError(t, err, "Set should not return error")
err = cc.Set("test", data, time.Hour)
require.NoError(t, err)
_, err = cc.Get("test")
require.NoError(t, err)
_, err = cc.Get("test1")
require.NoError(t, err)
_, err = cc.Get("test2")
require.NoError(t, err)
// Add a small delay to allow stats to be updated
time.Sleep(100 * time.Millisecond)
// Check stats
stats := cc.GetCompressionStats()
require.Equal(t, int64(3), stats.TotalCompressed.Load(), "Should record 3 compression operations")
require.Equal(t, int64(3), stats.TotalDecompressed.Load(), "Should record 3 decompression operations")
require.Greater(t, stats.TotalBytesIn.Load(), int64(0), "Should record input bytes")
require.Greater(t, stats.TotalBytesOut.Load(), int64(0), "Should record output bytes")
// Force update of compression ratio for test determinism
if stats.TotalBytesIn.Load() > 0 {
stats.CompressionRatio.Store(float64(stats.TotalBytesOut.Load()) / float64(stats.TotalBytesIn.Load()))
}
require.Greater(t, stats.CompressionRatio.Load().(float64), float64(0), "Should calculate compression ratio")
// Test concurrent stats updates with timeout
const goroutines = 10
const iterations = 100
var wg sync.WaitGroup
done := make(chan struct{})
wg.Add(goroutines)
for i := 0; i < goroutines; i++ {
go func() {
defer wg.Done()
for j := 0; j < iterations; j++ {
select {
case <-done:
return
default:
err := cc.Set("test", data, time.Hour)
require.NoError(t, err)
_, err = cc.Get("test")
require.NoError(t, err)
}
}
}()
}
// Add timeout for concurrent operations
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
// Test completed successfully
case <-time.After(5 * time.Second):
t.Fatal("Test timed out after 5 seconds")
}
// Verify final stats
stats = cc.GetCompressionStats()
expectedCompressed := int64(goroutines * iterations)
expectedDecompressed := int64(goroutines * iterations)
require.GreaterOrEqual(t, stats.TotalCompressed.Load(), expectedCompressed, "Should record correct number of compression operations")
require.LessOrEqual(t, stats.TotalCompressed.Load(), expectedCompressed+10, "Should not exceed expected compression operations by much")
require.GreaterOrEqual(t, stats.TotalDecompressed.Load(), expectedDecompressed, "Should record correct number of decompression operations")
require.LessOrEqual(t, stats.TotalDecompressed.Load(), expectedDecompressed+10, "Should not exceed expected decompression operations by much")
// Test stats reset
cc.ResetCompressionStats()
stats = cc.GetCompressionStats()
require.Equal(t, int64(0), stats.TotalCompressed.Load(), "Stats should be reset")
require.Equal(t, int64(0), stats.TotalDecompressed.Load(), "Stats should be reset")
require.Equal(t, int64(0), stats.TotalBytesIn.Load(), "Stats should be reset")
require.Equal(t, int64(0), stats.TotalBytesOut.Load(), "Stats should be reset")
}
func TestCompressionConfigUpdate(t *testing.T) {
mock := newMockCacheAny[string]()
config := DefaultCompressionConfig()
config.MinSize = 200
config.MaxSize = 2000
cc := NewCompressedCache[string, any](mock, config)
// Test with data below new MinSize
smallData := bytes.Repeat([]byte("small"), 100)
err := cc.Set("test", smallData, time.Hour)
require.NoError(t, err, "Set should not return error")
value, err := mock.Get("test")
require.NoError(t, err, "Get should not return error")
_, isCompressed := extractCompressedValue(value)
require.True(t, isCompressed, "Data should be compressed (above new MinSize)")
// Verify we can get the original data back
decompressed, err := cc.Get("test")
require.NoError(t, err, "Should be able to retrieve data")
require.True(t, bytes.Equal(smallData, decompressed.([]byte)), "Small data should not be compressed")
// Test with data above new MinSize
largeData := bytes.Repeat([]byte("large"), 300)
err = cc.Set("test2", largeData, time.Hour)
require.NoError(t, err, "Set should not return error")
value, err = mock.Get("test2")
require.NoError(t, err, "Get should not return error")
_, isCompressed = extractCompressedValue(value)
require.True(t, isCompressed, "Data should be compressed (above new MinSize)")
// Verify we can get the original data back
decompressed, err = cc.Get("test2")
require.NoError(t, err, "Should be able to retrieve data")
require.True(t, bytes.Equal(largeData, decompressed.([]byte)), "Decompressed data should match original")
}
func TestCompressionWithContext(t *testing.T) {
mock := newMockCacheAny[string]()
config := DefaultCompressionConfig()
cc := NewCompressedCache[string, any](mock, config)
// Test with context
ctx := context.Background()
data := bytes.Repeat([]byte("test data for compression"), 100)
// Test SetWithContext
err := cc.SetWithContext(ctx, "test", data, time.Hour)
require.NoError(t, err, "SetWithContext should not return error")
value, err := mock.Get("test")
require.NoError(t, err, "Data should be stored")
_, isCompressed := extractCompressedValue(value)
require.True(t, isCompressed, "Data should be compressed")
// Test GetWithContext
decompressed, err := cc.GetWithContext(ctx, "test")
require.NoError(t, err, "Should be able to retrieve data")
require.True(t, bytes.Equal(data, decompressed.([]byte)), "Decompressed data should match original")
// Test with cancelled context
ctx, cancel := context.WithCancel(context.Background())
cancel()
err = cc.SetWithContext(ctx, "test2", data, time.Hour)
require.Error(t, err, "SetWithContext should fail with cancelled context")
_, err = mock.Get("test2")
require.Error(t, err, "Data should not be stored with cancelled context")
}
func TestCompressionErrorHandling(t *testing.T) {
mock := newMockCacheAny[string]()
config := DefaultCompressionConfig()
cc := NewCompressedCache[string, any](mock, config)
// Test with invalid compression level
config.Level = 10 // Invalid level
cc.SetCompressionConfig(config)
data := bytes.Repeat([]byte("test data for compression"), 100)
err := cc.Set("test", data, time.Hour)
require.NoError(t, err, "Data should be stored")
_, err = mock.Get("test")
require.NoError(t, err, "Data should not be compressed with invalid level")
// Test with corrupted compressed data
corruptedData := []byte("corrupted compressed data")
err = mock.Set("corrupted", corruptedData, time.Hour)
require.NoError(t, err, "Set should not return error")
_, err = cc.Get("corrupted")
require.Error(t, err, "Should not be able to decompress corrupted data")
}
func TestCompressedCache(t *testing.T) {
// Create a base cache
baseCache := New[string, any](WithMaxSize[string, any](100))
// Test cases for different compression algorithms
algorithms := []CompressionAlgorithm{
GzipCompression,
ZlibCompression,
LZWCompression,
}
for _, algo := range algorithms {
t.Run(fmt.Sprintf("Algorithm_%d", algo), func(t *testing.T) {
// Create compression config
config := CompressionConfig{
Algorithm: algo,
Level: 6,
MinSize: 100, // Small size for testing
MaxSize: 1024, // Small size for testing
StatsEnabled: true,
}
// Create compressed cache
cache := NewCompressedCache[string, []byte](baseCache, config)
// Test data
testData := []byte("This is a test string that should be compressed. " +
"It needs to be long enough to trigger compression. " +
"Adding more text to ensure it exceeds the minimum size threshold.")
// Test Set and Get
err := cache.Set("test_key", testData, time.Minute)
if err != nil {
t.Errorf("Set failed: %v", err)
}
// Get the value back
value, err := cache.Get("test_key")
if err != nil {
t.Errorf("Get failed: %v", err)
}
// Verify the value
if !bytes.Equal(value, testData) {
t.Errorf("Expected %s, got %s", string(testData), string(value))
}
// Test compression stats
stats := cache.GetCompressionStats()
if stats.TotalCompressed.Load() == 0 {
t.Error("Expected compression stats to be recorded")
}
// Test context operations
ctx := context.Background()
_, err = cache.GetWithContext(ctx, "test_key")
if err != nil {
t.Errorf("GetWithContext failed: %v", err)
}
// Test compression config update
newConfig := config
newConfig.Level = 9
cache.SetCompressionConfig(newConfig)
currentConfig := cache.GetCompressionConfig()
if currentConfig.Level != 9 {
t.Errorf("Expected compression level 9, got %d", currentConfig.Level)
}
// Test reset stats
cache.ResetCompressionStats()
stats = cache.GetCompressionStats()
if stats.TotalCompressed.Load() != 0 {
t.Error("Expected stats to be reset")
}
})
}
}
func TestCompressedCacheWithStruct(t *testing.T) {
// Create a base cache
baseCache := New[string, any](WithMaxSize[string, any](100))
// Test struct
type TestStruct struct {
Name string
Value int
Data []byte
}
// Create compression config
config := DefaultCompressionConfig()
config.MinSize = 100 // Small size for testing
// Create compressed cache
cache := NewCompressedCache[string, TestStruct](baseCache, config)
// Test data
testData := TestStruct{
Name: "test",
Value: 42,
Data: []byte("This is a test string that should be compressed. " +
"It needs to be long enough to trigger compression. " +
"Adding more text to ensure it exceeds the minimum size threshold."),
}
// Test Set and Get
err := cache.Set("test_key", testData, time.Minute)
if err != nil {
t.Errorf("Set failed: %v", err)
}
// Get the value back
value, err := cache.Get("test_key")
if err != nil {
t.Errorf("Get failed: %v", err)
}
// Verify the value
if value.Name != testData.Name || value.Value != testData.Value || !bytes.Equal(value.Data, testData.Data) {
t.Errorf("Expected %+v, got %+v", testData, value)
}
}
func TestCompressedCacheWithSmallData(t *testing.T) {
// Create a base cache
baseCache := New[string, any](WithMaxSize[string, any](100))
// Create compression config with high minimum size
config := DefaultCompressionConfig()
config.MinSize = 1000 // Set high minimum size
// Create compressed cache
cache := NewCompressedCache[string, []byte](baseCache, config)
// Test data that's too small to compress
smallData := []byte("small data")
// Test Set and Get
err := cache.Set("test_key", smallData, time.Minute)
require.NoError(t, err, "Set should not return error")
// Get the value back
value, err := cache.Get("test_key")
require.NoError(t, err, "Get should not return error")
// Verify the value
require.Equal(t, string(smallData), string(value), "Value should match original data")
// Verify no compression occurred
stats := cache.GetCompressionStats()
require.Equal(t, int64(0), stats.TotalCompressed.Load(), "Should not compress small data")
require.Equal(t, int64(0), stats.TotalDecompressed.Load(), "Should not decompress small data")
}