-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_factory_test.go
133 lines (123 loc) · 4.42 KB
/
cache_factory_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package cache
import (
"testing"
"time"
)
func TestNewCache(t *testing.T) {
t.Run("New cache with default expiration", func(t *testing.T) {
cache := newCache(DefaultExpiration, make(map[string]Item))
if cache.defaultExpiration != DefaultExpiration {
t.Errorf("Expected default expiration to be %v, got %v", DefaultExpiration, cache.defaultExpiration)
}
if cache.items == nil {
t.Error("Expected items map to be initialized")
}
})
t.Run("New cache with NoExpiration", func(t *testing.T) {
cache := newCache(NoExpiration, make(map[string]Item))
if cache.defaultExpiration != NoExpiration {
t.Errorf("Expected default expiration to be %v, got %v", NoExpiration, cache.defaultExpiration)
}
if cache.items == nil {
t.Error("Expected items map to be initialized")
}
})
}
func TestNewCacheWithJanitor(t *testing.T) {
t.Run("New cache with janitor", func(t *testing.T) {
cache := newCacheWithJanitor(DefaultExpiration, 1*time.Minute, make(map[string]Item))
if cache.defaultExpiration != DefaultExpiration {
t.Errorf("Expected default expiration to be %v, got %v", DefaultExpiration, cache.defaultExpiration)
}
if cache.items == nil {
t.Error("Expected items map to be initialized")
}
if cache.janitor == nil {
t.Error("Expected janitor to be initialized")
}
})
t.Run("New cache without janitor", func(t *testing.T) {
cache := newCacheWithJanitor(DefaultExpiration, 0, make(map[string]Item))
if cache.defaultExpiration != DefaultExpiration {
t.Errorf("Expected default expiration to be %v, got %v", DefaultExpiration, cache.defaultExpiration)
}
if cache.items == nil {
t.Error("Expected items map to be initialized")
}
if cache.janitor != nil {
t.Error("Expected janitor not to be initialized")
}
})
}
func TestNew(t *testing.T) {
t.Run("New cache with default expiration and cleanup interval", func(t *testing.T) {
cache := New(DefaultExpiration, 1*time.Minute)
if cache.defaultExpiration != DefaultExpiration {
t.Errorf("Expected default expiration to be %v, got %v", DefaultExpiration, cache.defaultExpiration)
}
if cache.items == nil {
t.Error("Expected items map to be initialized")
}
if cache.janitor == nil {
t.Error("Expected janitor to be initialized")
}
})
t.Run("New cache with NoExpiration and no cleanup interval", func(t *testing.T) {
cache := New(NoExpiration, 0)
if cache.defaultExpiration != NoExpiration {
t.Errorf("Expected default expiration to be %v, got %v", NoExpiration, cache.defaultExpiration)
}
if cache.items == nil {
t.Error("Expected items map to be initialized")
}
if cache.janitor != nil {
t.Error("Expected janitor not to be initialized")
}
})
}
func TestNewFrom(t *testing.T) {
t.Run("NewFrom with default expiration and cleanup interval", func(t *testing.T) {
items := make(map[string]Item)
cache := NewFrom(DefaultExpiration, 1*time.Minute, items)
if cache.defaultExpiration != DefaultExpiration {
t.Errorf("Expected default expiration to be %v, got %v", DefaultExpiration, cache.defaultExpiration)
}
if cache.items == nil || len(cache.items) != 0 {
t.Error("Expected items map to be initialized and empty")
}
if cache.janitor == nil {
t.Error("Expected janitor to be initialized")
}
})
t.Run("NewFrom with NoExpiration and no cleanup interval", func(t *testing.T) {
items := make(map[string]Item)
cache := NewFrom(NoExpiration, 0, items)
if cache.defaultExpiration != NoExpiration {
t.Errorf("Expected default expiration to be %v, got %v", NoExpiration, cache.defaultExpiration)
}
if cache.items == nil || len(cache.items) != 0 {
t.Error("Expected items map to be initialized and empty")
}
if cache.janitor != nil {
t.Error("Expected janitor not to be initialized")
}
})
t.Run("NewFrom with existing items map", func(t *testing.T) {
items := map[string]Item{
"key1": {Object: "value1", Expiration: 0},
}
cache := NewFrom(DefaultExpiration, 1*time.Minute, items)
if cache.defaultExpiration != DefaultExpiration {
t.Errorf("Expected default expiration to be %v, got %v", DefaultExpiration, cache.defaultExpiration)
}
if cache.items == nil || len(cache.items) != 1 {
t.Error("Expected items map to be initialized with one item")
}
if cache.janitor == nil {
t.Error("Expected janitor to be initialized")
}
if cache.items["key1"].Object != "value1" {
t.Errorf("Expected item with key 'key1' to have value 'value1', got %v", cache.items["key1"].Object)
}
})
}