-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypercache_test.go
110 lines (98 loc) · 3.19 KB
/
hypercache_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
package hypercache
import (
"testing"
"time"
"github.com/hyp3rd/hypercache/backend"
"github.com/longbridgeapp/assert"
)
func TestHyperCache_New(t *testing.T) {
// Test that an error is returned when the capacity is negative
_, err := NewInMemoryWithDefaults(-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(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(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) {
// Test with default stats collector
cache, err := NewInMemoryWithDefaults(10)
assert.Nil(t, err)
assert.NotNil(t, cache.StatsCollector)
}
func TestHyperCache_WithExpirationInterval(t *testing.T) {
// Test with default expiration interval
cache, err := NewInMemoryWithDefaults(10)
assert.Nil(t, err)
assert.Equal(t, 30*time.Minute, cache.expirationInterval)
config := &Config[backend.InMemory]{
BackendType: "in-memory",
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(hcm, config)
assert.Nil(t, err)
assert.Equal(t, 1*time.Hour, cache.expirationInterval)
}
func TestHyperCache_WithEvictionInterval(t *testing.T) {
// Test with default eviction interval
cache, err := NewInMemoryWithDefaults(10)
assert.Nil(t, err)
assert.Equal(t, 10*time.Minute, cache.evictionInterval)
// Test with custom eviction interval
config := &Config[backend.InMemory]{
BackendType: "in-memory",
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(hcm, config)
assert.Nil(t, err)
assert.Equal(t, 1*time.Hour, cache.evictionInterval)
}
func TestHyperCache_WithMaxEvictionCount(t *testing.T) {
// Test with default max eviction count
cache, err := NewInMemoryWithDefaults(10)
assert.Nil(t, err)
assert.Equal(t, uint(10), cache.maxEvictionCount)
// Test with custom max eviction count
config := &Config[backend.InMemory]{
BackendType: "in-memory",
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(hcm, config)
assert.Nil(t, err)
assert.Equal(t, uint(5), cache.maxEvictionCount)
}