Debian Codegraph @

cache/filecache/filecache_pruner_test.go

raw ◀ ▶
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.Skip("Skip test until go.mod is supported by dh-golang")
91 t.Parallel()
92
93 synctest.Test(t, func(t *testing.T) {
94 c := qt.New(t)
95
96 configStr := `
97 resourceDir = "myresources"
98 contentDir = "content"
99 dataDir = "data"
100 i18nDir = "i18n"
101 layoutDir = "layouts"
102 assetDir = "assets"
103 archeTypedir = "archetypes"
104
105 [caches]
106 [caches.misc]
107 maxAge = "200ms"
108 dir = "/cache/c"
109 [caches.assets]
110 maxAge = "200ms"
111 dir = ":resourceDir/_gen"
112 [caches.images]
113 maxAge = "200ms"
114 dir = ":resourceDir/_gen"
115 `
116
117 for _, name := range []string{filecache.CacheKeyAssets, filecache.CacheKeyImages} {
118 msg := qt.Commentf("cache: %s", name)
119 fs := afero.NewMemMapFs()
120 p := newPathsSpec(t, fs, configStr)
121 fileCachConfig := p.Cfg.GetConfigSection("caches").(filecache.Configs)
122 caches, err := filecache.NewCaches(fileCachConfig, fs)
123 c.Assert(err, qt.IsNil)
124 caches.SetResourceFs(fs)
125 cache := caches[name]
126 for i := range 10 {
127 id := fmt.Sprintf("i%d", i)
128 cache.GetOrCreateBytes(id, func() ([]byte, error) {
129 return []byte("abc"), nil
130 })
131 if i == 4 {
132 // This will expire the first 5
133 time.Sleep(201 * time.Millisecond)
134 }
135 }
136
137 count, err := caches.Prune()
138 c.Assert(err, qt.IsNil)
139 c.Assert(count, qt.Equals, 5, msg)
140
141 for i := range 10 {
142 id := fmt.Sprintf("i%d", i)
143 v := cache.GetString(id)
144 if i < 5 {
145 c.Assert(v, qt.Equals, "")
146 } else {
147 c.Assert(v, qt.Equals, "abc")
148 }
149 }
150
151 caches, err = filecache.NewCaches(fileCachConfig, fs)
152 c.Assert(err, qt.IsNil)
153 caches.SetResourceFs(fs)
154 cache = caches[name]
155 // Touch one and then prune.
156 cache.GetOrCreateBytes("i5", func() ([]byte, error) {
157 return []byte("abc"), nil
158 })
159
160 count, err = caches.Prune()
161 c.Assert(err, qt.IsNil)
162 c.Assert(count, qt.Equals, 4)
163
164 // Now only the i5 should be left.
165 for i := range 10 {
166 id := fmt.Sprintf("i%d", i)
167 v := cache.GetString(id)
168 if i != 5 {
169 c.Assert(v, qt.Equals, "")
170 } else {
171 c.Assert(v, qt.Equals, "abc")
172 }
173 }
174
175 }
176 })
177 }