-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_test.go
More file actions
193 lines (167 loc) · 4.76 KB
/
Copy pathcache_test.go
File metadata and controls
193 lines (167 loc) · 4.76 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
package filejsondb
import (
"bytes"
"testing"
)
func TestCollectionCacheCalcSize(t *testing.T) {
cache := NewCollectionCache(10, 1024)
if size := cache.CalcSize("ab", []byte("123")); size != 5 {
t.Fatalf("unexpected size: got %d want 5", size)
}
}
func TestCollectionCacheSetGetClonesBytes(t *testing.T) {
cache := NewCollectionCache(10, 1024)
in := []byte("value")
cache.Set("k", in)
in[0] = 'X'
out, ok := cache.Get("k")
if !ok {
t.Fatal("expected key in cache")
}
if string(out) != "value" {
t.Fatalf("expected stored value to be cloned, got %q", string(out))
}
out[0] = 'Y'
out2, ok := cache.Get("k")
if !ok {
t.Fatal("expected key in cache on second read")
}
if string(out2) != "value" {
t.Fatalf("expected returned value to be cloned, got %q", string(out2))
}
}
func TestCollectionCacheEvictsByObjectLimitLRU(t *testing.T) {
cache := NewCollectionCache(2, 0)
cache.Set("a", []byte("1"))
cache.Set("b", []byte("2"))
if _, ok := cache.Get("a"); !ok {
t.Fatal("expected a to exist")
}
cache.Set("c", []byte("3"))
if _, ok := cache.Get("b"); ok {
t.Fatal("expected b to be evicted as LRU")
}
if _, ok := cache.Get("a"); !ok {
t.Fatal("expected a to remain in cache")
}
if _, ok := cache.Get("c"); !ok {
t.Fatal("expected c to remain in cache")
}
}
func TestCollectionCacheEvictsByByteLimit(t *testing.T) {
cache := NewCollectionCache(0, 6)
cache.Set("a", []byte("11"))
cache.Set("b", []byte("22"))
cache.Set("c", []byte("33"))
if _, ok := cache.Get("a"); ok {
t.Fatal("expected a to be evicted by byte limit")
}
if _, ok := cache.Get("b"); !ok {
t.Fatal("expected b to remain")
}
if _, ok := cache.Get("c"); !ok {
t.Fatal("expected c to remain")
}
}
func TestCollectionCacheOversizedItemIsRejectedAndReplacesExisting(t *testing.T) {
cache := NewCollectionCache(10, 5)
cache.Set("k", []byte("1"))
if _, ok := cache.Get("k"); !ok {
t.Fatal("expected initial key to exist")
}
cache.Set("k", []byte("12345"))
if _, ok := cache.Get("k"); ok {
t.Fatal("expected oversized value to remove existing key")
}
}
func TestCollectionCacheSetUpdatesExistingEntry(t *testing.T) {
cache := NewCollectionCache(10, 1024)
cache.Set("k", []byte("1"))
cache.Set("k", []byte("22"))
if len(cache.items) != 1 {
t.Fatalf("expected a single cache entry, got %d", len(cache.items))
}
if cache.currentBytes != len("k")+len("22") {
t.Fatalf("unexpected byte count: got %d", cache.currentBytes)
}
out, ok := cache.Get("k")
if !ok {
t.Fatal("expected updated key to exist")
}
if string(out) != "22" {
t.Fatalf("unexpected updated value: %q", string(out))
}
}
func TestCollectionCacheWarmAndGetAllIfComplete(t *testing.T) {
cache := NewCollectionCache(10, 1024)
source := map[string][]byte{
"a": []byte("1"),
"b": []byte("2"),
}
cache.Warm(source)
all, ok := cache.GetAllIfComplete()
if !ok {
t.Fatal("expected full snapshot")
}
if len(all) != 2 {
t.Fatalf("expected 2 entries, got %d", len(all))
}
source["a"][0] = 'X'
if string(all["a"]) != "1" {
t.Fatalf("expected warm to clone source bytes, got %q", string(all["a"]))
}
all["b"][0] = 'Y'
again, ok := cache.GetAllIfComplete()
if !ok {
t.Fatal("expected full snapshot on second read")
}
if !bytes.Equal(again["b"], []byte("2")) {
t.Fatalf("expected snapshot reads to be cloned, got %q", string(again["b"]))
}
}
func TestCollectionCacheWarmIncompleteWhenEvictedOrOversized(t *testing.T) {
cache := NewCollectionCache(1, 10)
cache.Warm(map[string][]byte{
"a": []byte("1"),
"b": []byte("2"),
})
if _, ok := cache.GetAllIfComplete(); ok {
t.Fatal("expected incomplete snapshot when warm-up evicts")
}
cache2 := NewCollectionCache(10, 3)
cache2.Warm(map[string][]byte{"big": []byte("1234")})
if _, ok := cache2.GetAllIfComplete(); ok {
t.Fatal("expected incomplete snapshot when warm-up skips oversized item")
}
}
func TestCollectionCacheWarmReplacesExistingState(t *testing.T) {
cache := NewCollectionCache(10, 1024)
cache.Set("stale", []byte("x"))
cache.Warm(map[string][]byte{"fresh": []byte("y")})
if _, ok := cache.Get("stale"); ok {
t.Fatal("expected warm-up to purge stale entries first")
}
all, ok := cache.GetAllIfComplete()
if !ok {
t.Fatal("expected a complete snapshot after warm-up")
}
if len(all) != 1 || string(all["fresh"]) != "y" {
t.Fatalf("unexpected warmed snapshot: %+v", all)
}
}
func TestCollectionCacheDeleteAndPurge(t *testing.T) {
cache := NewCollectionCache(10, 1024)
cache.Set("a", []byte("1"))
cache.Set("b", []byte("2"))
cache.Delete("a")
if _, ok := cache.Get("a"); ok {
t.Fatal("expected a to be deleted")
}
cache.Purge()
if _, ok := cache.Get("b"); ok {
t.Fatal("expected cache to be empty after purge")
}
if _, ok := cache.GetAllIfComplete(); ok {
t.Fatal("expected full snapshot flag to be reset after purge")
}
}