Debian Codegraph @

cache/dynacache/dynacache_test.go

raw
1 // Copyright 2024 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package dynacache
15
16 import (
17 "errors"
18 "fmt"
19 "path/filepath"
20 "testing"
21 "time"
22
23 qt "github.com/frankban/quicktest"
24 "github.com/gohugoio/hugo/common/loggers"
25 "github.com/gohugoio/hugo/identity"
26 "github.com/gohugoio/hugo/resources/resource"
27 )
28
29 var (
30 _ resource.StaleInfo = (*testItem)(nil)
31 _ identity.Identity = (*testItem)(nil)
32 )
33
34 type testItem struct {
35 name string
36 staleVersion uint32
37 }
38
39 func (t testItem) StaleVersion() uint32 {
40 return t.staleVersion
41 }
42
43 func (t testItem) IdentifierBase() string {
44 return t.name
45 }
46
47 func TestCache(t *testing.T) {
48 t.Parallel()
49 c := qt.New(t)
50
51 cache := New(Options{
52 Log: loggers.NewDefault(),
53 })
54
55 c.Cleanup(func() {
56 cache.Stop()
57 })
58
59 opts := OptionsPartition{Weight: 30}
60
61 c.Assert(cache, qt.Not(qt.IsNil))
62
63 p1 := GetOrCreatePartition[string, testItem](cache, "/aaaa/bbbb", opts)
64 c.Assert(p1, qt.Not(qt.IsNil))
65
66 p2 := GetOrCreatePartition[string, testItem](cache, "/aaaa/bbbb", opts)
67
68 c.Assert(func() { GetOrCreatePartition[string, testItem](cache, "foo bar", opts) }, qt.PanicMatches, ".*invalid partition name.*")
69 c.Assert(func() { GetOrCreatePartition[string, testItem](cache, "/aaaa/cccc", OptionsPartition{Weight: 1234}) }, qt.PanicMatches, ".*invalid Weight.*")
70
71 c.Assert(p2, qt.Equals, p1)
72
73 p3 := GetOrCreatePartition[string, testItem](cache, "/aaaa/cccc", opts)
74 c.Assert(p3, qt.Not(qt.IsNil))
75 c.Assert(p3, qt.Not(qt.Equals), p1)
76
77 c.Assert(func() { New(Options{}) }, qt.PanicMatches, ".*nil Log.*")
78 }
79
80 func TestCalculateMaxSizePerPartition(t *testing.T) {
81 t.Parallel()
82 c := qt.New(t)
83
84 c.Assert(calculateMaxSizePerPartition(1000, 500, 5), qt.Equals, 200)
85 c.Assert(calculateMaxSizePerPartition(1000, 250, 5), qt.Equals, 400)
86 c.Assert(func() { calculateMaxSizePerPartition(1000, 250, 0) }, qt.PanicMatches, ".*must be > 0.*")
87 c.Assert(func() { calculateMaxSizePerPartition(1000, 0, 1) }, qt.PanicMatches, ".*must be > 0.*")
88 }
89
90 func TestCleanKey(t *testing.T) {
91 c := qt.New(t)
92
93 c.Assert(CleanKey("a/b/c"), qt.Equals, "/a/b/c")
94 c.Assert(CleanKey("/a/b/c"), qt.Equals, "/a/b/c")
95 c.Assert(CleanKey("a/b/c/"), qt.Equals, "/a/b/c")
96 c.Assert(CleanKey(filepath.FromSlash("/a/b/c/")), qt.Equals, "/a/b/c")
97 }
98
99 func newTestCache(t *testing.T) *Cache {
100 cache := New(
101 Options{
102 Log: loggers.NewDefault(),
103 },
104 )
105
106 p1 := GetOrCreatePartition[string, testItem](cache, "/aaaa/bbbb", OptionsPartition{Weight: 30, ClearWhen: ClearOnRebuild})
107 p2 := GetOrCreatePartition[string, testItem](cache, "/aaaa/cccc", OptionsPartition{Weight: 30, ClearWhen: ClearOnChange})
108
109 p1.GetOrCreate("clearOnRebuild", func(string) (testItem, error) {
110 return testItem{}, nil
111 })
112
113 p2.GetOrCreate("clearBecauseStale", func(string) (testItem, error) {
114 return testItem{
115 staleVersion: 32,
116 }, nil
117 })
118
119 p2.GetOrCreate("clearBecauseIdentityChanged", func(string) (testItem, error) {
120 return testItem{
121 name: "changed",
122 }, nil
123 })
124
125 p2.GetOrCreate("clearNever", func(string) (testItem, error) {
126 return testItem{
127 staleVersion: 0,
128 }, nil
129 })
130
131 t.Cleanup(func() {
132 cache.Stop()
133 })
134
135 return cache
136 }
137
138 func TestClear(t *testing.T) {
139 t.Parallel()
140 c := qt.New(t)
141
142 predicateAll := func(string) bool {
143 return true
144 }
145
146 cache := newTestCache(t)
147
148 c.Assert(cache.Keys(predicateAll), qt.HasLen, 4)
149
150 cache.ClearOnRebuild(nil)
151
152 // Stale items are always cleared.
153 c.Assert(cache.Keys(predicateAll), qt.HasLen, 2)
154
155 cache = newTestCache(t)
156 cache.ClearOnRebuild(nil, identity.StringIdentity("changed"))
157
158 c.Assert(cache.Keys(nil), qt.HasLen, 1)
159
160 cache = newTestCache(t)
161
162 cache.ClearMatching(nil, func(k, v any) bool {
163 return k.(string) == "clearOnRebuild"
164 })
165
166 c.Assert(cache.Keys(predicateAll), qt.HasLen, 3)
167
168 cache.adjustCurrentMaxSize()
169 }
170
171 func TestPanicInCreate(t *testing.T) {
172 t.Parallel()
173 c := qt.New(t)
174 cache := newTestCache(t)
175
176 p1 := GetOrCreatePartition[string, testItem](cache, "/aaaa/bbbb", OptionsPartition{Weight: 30, ClearWhen: ClearOnRebuild})
177
178 willPanic := func(i int) func() {
179 return func() {
180 p1.GetOrCreate(fmt.Sprintf("panic-%d", i), func(key string) (testItem, error) {
181 panic(errors.New(key))
182 })
183 }
184 }
185
186 // GetOrCreateWitTimeout needs to recover from panics in the create func.
187 willErr := func(i int) error {
188 _, err := p1.GetOrCreateWitTimeout(fmt.Sprintf("error-%d", i), 10*time.Second, func(key string) (testItem, error) {
189 return testItem{}, errors.New(key)
190 })
191 return err
192 }
193
194 for i := range 3 {
195 for range 3 {
196 c.Assert(willPanic(i), qt.PanicMatches, fmt.Sprintf("panic-%d", i))
197 c.Assert(willErr(i), qt.ErrorMatches, fmt.Sprintf("error-%d", i))
198 }
199 }
200
201 // Test the same keys again without the panic.
202 for i := range 3 {
203 for range 3 {
204 v, err := p1.GetOrCreate(fmt.Sprintf("panic-%d", i), func(key string) (testItem, error) {
205 return testItem{
206 name: key,
207 }, nil
208 })
209 c.Assert(err, qt.IsNil)
210 c.Assert(v.name, qt.Equals, fmt.Sprintf("panic-%d", i))
211
212 v, err = p1.GetOrCreateWitTimeout(fmt.Sprintf("error-%d", i), 10*time.Second, func(key string) (testItem, error) {
213 return testItem{
214 name: key,
215 }, nil
216 })
217 c.Assert(err, qt.IsNil)
218 c.Assert(v.name, qt.Equals, fmt.Sprintf("error-%d", i))
219 }
220 }
221 }
222
223 func TestAdjustCurrentMaxSize(t *testing.T) {
224 t.Parallel()
225 c := qt.New(t)
226 cache := newTestCache(t)
227 alloc := cache.stats.memstatsCurrent.Alloc
228 cache.adjustCurrentMaxSize()
229 c.Assert(cache.stats.memstatsCurrent.Alloc, qt.Not(qt.Equals), alloc)
230 }