-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhypercache_test.go
More file actions
127 lines (105 loc) · 3.57 KB
/
hypercache_test.go
File metadata and controls
127 lines (105 loc) · 3.57 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
package hypercache
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/hyp3rd/hypercache/internal/constants"
"github.com/hyp3rd/hypercache/pkg/backend"
)
func TestHyperCache_New(t *testing.T) {
t.Parallel()
// Test that an error is returned when the capacity is negative
_, err := NewInMemoryWithDefaults(context.TODO(), -1)
if err == nil {
t.Error("Expected an error when capacity is negative, got nil")
}
// Test that a new HyperCache is returned when the capacity is 0
cache, err := NewInMemoryWithDefaults(context.TODO(), 0)
if err != nil {
t.Errorf("Unexpected error when capacity is 0: %v", err)
}
if cache == nil {
t.Error("Expected a new HyperCache when capacity is 0, got nil")
}
// Test that a new HyperCache is returned when the capacity is positive
cache, err = NewInMemoryWithDefaults(context.TODO(), 10)
if err != nil {
t.Errorf("Unexpected error when capacity is positive: %v", err)
}
if cache == nil {
t.Error("Expected a new HyperCache when capacity is positive, got nil")
}
}
func TestHyperCache_WithStatsCollector(t *testing.T) {
t.Parallel()
// Test with default stats collector
cache, err := NewInMemoryWithDefaults(context.TODO(), 10)
require.NoError(t, err)
require.NotNil(t, cache.StatsCollector)
}
func TestHyperCache_WithExpirationInterval(t *testing.T) {
t.Parallel()
// Test with default expiration interval
cache, err := NewInMemoryWithDefaults(context.TODO(), 10)
require.NoError(t, err)
require.Equal(t, 30*time.Minute, cache.expirationInterval)
config := &Config[backend.InMemory]{
BackendType: constants.InMemoryBackend,
HyperCacheOptions: []Option[backend.InMemory]{
WithExpirationInterval[backend.InMemory](1 * time.Hour),
},
InMemoryOptions: []backend.Option[backend.InMemory]{
backend.WithCapacity[backend.InMemory](10),
},
}
// Test with custom expiration interval
hcm := GetDefaultManager()
cache, err = New(context.TODO(), hcm, config)
require.NoError(t, err)
require.Equal(t, 1*time.Hour, cache.expirationInterval)
}
func TestHyperCache_WithEvictionInterval(t *testing.T) {
t.Parallel()
// Test with default eviction interval
cache, err := NewInMemoryWithDefaults(context.TODO(), 10)
require.NoError(t, err)
require.Equal(t, 10*time.Minute, cache.evictionInterval)
// Test with custom eviction interval
config := &Config[backend.InMemory]{
BackendType: constants.InMemoryBackend,
HyperCacheOptions: []Option[backend.InMemory]{
WithEvictionInterval[backend.InMemory](1 * time.Hour),
},
InMemoryOptions: []backend.Option[backend.InMemory]{
backend.WithCapacity[backend.InMemory](10),
},
}
hcm := GetDefaultManager()
// Test with custom eviction interval
cache, err = New(context.TODO(), hcm, config)
require.NoError(t, err)
require.Equal(t, 1*time.Hour, cache.evictionInterval)
}
func TestHyperCache_WithMaxEvictionCount(t *testing.T) {
t.Parallel()
// Test with default max eviction count
cache, err := NewInMemoryWithDefaults(context.TODO(), 10)
require.NoError(t, err)
require.Equal(t, uint(10), cache.maxEvictionCount)
// Test with custom max eviction count
config := &Config[backend.InMemory]{
BackendType: constants.InMemoryBackend,
HyperCacheOptions: []Option[backend.InMemory]{
WithEvictionInterval[backend.InMemory](1 * time.Hour),
WithMaxEvictionCount[backend.InMemory](5),
},
InMemoryOptions: []backend.Option[backend.InMemory]{
backend.WithCapacity[backend.InMemory](10),
},
}
hcm := GetDefaultManager()
cache, err = New(context.TODO(), hcm, config)
require.NoError(t, err)
require.Equal(t, uint(5), cache.maxEvictionCount)
}