Debian Codegraph @

cache/filecache/filecache_pruner_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright 2018 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 filecache_test
15
16 import (
17 "fmt"
18 "testing"
19 "testing/synctest"
20 "time"
21
22 "github.com/gohugoio/hugo/cache/filecache"
23 "github.com/gohugoio/hugo/htesting"
24 "github.com/spf13/afero"
25
26 qt "github.com/frankban/quicktest"
27 )
28
29 // A cache entry created before Hugo started lowercasing content paths in v0.123
30 // (e.g. _gen/images/MyBundle) is on a case-insensitive filesystem the same file as
31 // the lowercased cache key used today, and must not be pruned.
32 // See issue 15101.
33 func TestPruneCacheEntryWithOtherCase(t *testing.T) {
34 t.Parallel()
35 c := qt.New(t)
36
37 dir := t.TempDir()
38 if isCaseInsensitive, err := htesting.IsCaseInsensitiveFs(dir); err != nil {
39 t.Fatal(err)
40 } else if !isCaseInsensitive {
41 t.Skip("skip test on case-sensitive filesystem")
42 }
43
44 fs := afero.NewBasePathFs(afero.NewOsFs(), dir)
45 newCache := func() *filecache.Cache {
46 return filecache.NewCache(fs, filecache.FileCacheConfig{Dir: "cache", MaxAge: -1})
47 }
48
49 c.Assert(newCache().SetBytes("MyBundle/i1", []byte("abc")), qt.IsNil)
50
51 cache := newCache()
52 _, b, err := cache.GetOrCreateBytes("mybundle/i1", func() ([]byte, error) {
53 return []byte("def"), nil
54 })
55 c.Assert(err, qt.IsNil)
56 c.Assert(string(b), qt.Equals, "abc")
57
58 count, err := cache.Prune(false)
59 c.Assert(err, qt.IsNil)
60 c.Assert(count, qt.Equals, 0)
61 c.Assert(cache.GetString("MyBundle/i1"), qt.Equals, "abc")
62 }
63
64 // On a case-sensitive filesystem the entries above are distinct files,
65 // and the one not used in this build should be pruned.
66 func TestPruneCacheEntryWithOtherCaseCaseSensitiveFs(t *testing.T) {
67 t.Parallel()
68 c := qt.New(t)
69
70 fs := afero.NewMemMapFs()
71 cache := filecache.NewCache(fs, filecache.FileCacheConfig{Dir: "cache", MaxAge: -1})
72
73 c.Assert(cache.SetBytes("MyBundle/i1", []byte("abc")), qt.IsNil)
74
75 cache = filecache.NewCache(fs, filecache.FileCacheConfig{Dir: "cache", MaxAge: -1})
76 _, b, err := cache.GetOrCreateBytes("mybundle/i1", func() ([]byte, error) {
77 return []byte("def"), nil
78 })
79 c.Assert(err, qt.IsNil)
80 c.Assert(string(b), qt.Equals, "def")
81
82 count, err := cache.Prune(false)
83 c.Assert(err, qt.IsNil)
84 c.Assert(count, qt.Equals, 1)
85 c.Assert(cache.GetString("MyBundle/i1"), qt.Equals, "")
86 c.Assert(cache.GetString("mybundle/i1"), qt.Equals, "def")
87 }
88
89 func TestPrune(t *testing.T) {
90 t.Parallel()
91
92 synctest.Test(t, func(t *testing.T) {
93 c := qt.New(t)
94
95 configStr := `
96 resourceDir = "myresources"
97 contentDir = "content"
98 dataDir = "data"
99 i18nDir = "i18n"
100 layoutDir = "layouts"
101 assetDir = "assets"
102 archeTypedir = "archetypes"
103
104 [caches]
105 [caches.misc]
106 maxAge = "200ms"
107 dir = "/cache/c"
108 [caches.assets]
109 maxAge = "200ms"
110 dir = ":resourceDir/_gen"
111 [caches.images]
112 maxAge = "200ms"
113 dir = ":resourceDir/_gen"
114 `
115
116 for _, name := range []string{filecache.CacheKeyAssets, filecache.CacheKeyImages} {
117 msg := qt.Commentf("cache: %s", name)
118 fs := afero.NewMemMapFs()
119 p := newPathsSpec(t, fs, configStr)
120 fileCachConfig := p.Cfg.GetConfigSection("caches").(filecache.Configs)
121 caches, err := filecache.NewCaches(fileCachConfig, fs)
122 c.Assert(err, qt.IsNil)
123 caches.SetResourceFs(fs)
124 cache := caches[name]
125 for i := range 10 {
126 id := fmt.Sprintf("i%d", i)
127 cache.GetOrCreateBytes(id, func() ([]byte, error) {
128 return []byte("abc"), nil
129 })
130 if i == 4 {
131 // This will expire the first 5
132 time.Sleep(201 * time.Millisecond)
133 }
134 }
135
136 count, err := caches.Prune()
137 c.Assert(err, qt.IsNil)
138 c.Assert(count, qt.Equals, 5, msg)
139
140 for i := range 10 {
141 id := fmt.Sprintf("i%d", i)
142 v := cache.GetString(id)
143 if i < 5 {
144 c.Assert(v, qt.Equals, "")
145 } else {
146 c.Assert(v, qt.Equals, "abc")
147 }
148 }
149
150 caches, err = filecache.NewCaches(fileCachConfig, fs)
151 c.Assert(err, qt.IsNil)
152 caches.SetResourceFs(fs)
153 cache = caches[name]
154 // Touch one and then prune.
155 cache.GetOrCreateBytes("i5", func() ([]byte, error) {
156 return []byte("abc"), nil
157 })
158
159 count, err = caches.Prune()
160 c.Assert(err, qt.IsNil)
161 c.Assert(count, qt.Equals, 4)
162
163 // Now only the i5 should be left.
164 for i := range 10 {
165 id := fmt.Sprintf("i%d", i)
166 v := cache.GetString(id)
167 if i != 5 {
168 c.Assert(v, qt.Equals, "")
169 } else {
170 c.Assert(v, qt.Equals, "abc")
171 }
172 }
173
174 }
175 })
176 }