1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
19 verify = false // even if GODEBUG is set
22 func TestBasic(t *testing.T) {
23 dir, err := ioutil.TempDir("", "cachetest-")
27 defer os.RemoveAll(dir)
28 _, err = Open(filepath.Join(dir, "notexist"))
30 t.Fatal(`Open("tmp/notexist") succeeded, want failure`)
33 cdir := filepath.Join(dir, "c1")
34 if err := os.Mkdir(cdir, 0777); err != nil {
40 t.Fatalf("Open(c1) (create): %v", err)
42 if err := c1.putIndexEntry(dummyID(1), dummyID(12), 13, true); err != nil {
43 t.Fatalf("addIndexEntry: %v", err)
45 if err := c1.putIndexEntry(dummyID(1), dummyID(2), 3, true); err != nil { // overwrite entry
46 t.Fatalf("addIndexEntry: %v", err)
48 if entry, err := c1.Get(dummyID(1)); err != nil || entry.OutputID != dummyID(2) || entry.Size != 3 {
49 t.Fatalf("c1.Get(1) = %x, %v, %v, want %x, %v, nil", entry.OutputID, entry.Size, err, dummyID(2), 3)
54 t.Fatalf("Open(c2) (reuse): %v", err)
56 if entry, err := c2.Get(dummyID(1)); err != nil || entry.OutputID != dummyID(2) || entry.Size != 3 {
57 t.Fatalf("c2.Get(1) = %x, %v, %v, want %x, %v, nil", entry.OutputID, entry.Size, err, dummyID(2), 3)
59 if err := c2.putIndexEntry(dummyID(2), dummyID(3), 4, true); err != nil {
60 t.Fatalf("addIndexEntry: %v", err)
62 if entry, err := c1.Get(dummyID(2)); err != nil || entry.OutputID != dummyID(3) || entry.Size != 4 {
63 t.Fatalf("c1.Get(2) = %x, %v, %v, want %x, %v, nil", entry.OutputID, entry.Size, err, dummyID(3), 4)
67 func TestGrowth(t *testing.T) {
68 dir, err := ioutil.TempDir("", "cachetest-")
72 defer os.RemoveAll(dir)
76 t.Fatalf("Open: %v", err)
84 for i := 0; i < n; i++ {
85 if err := c.putIndexEntry(dummyID(i), dummyID(i*99), int64(i)*101, true); err != nil {
86 t.Fatalf("addIndexEntry: %v", err)
88 id := ActionID(dummyID(i))
89 entry, err := c.Get(id)
91 t.Fatalf("Get(%x): %v", id, err)
93 if entry.OutputID != dummyID(i*99) || entry.Size != int64(i)*101 {
94 t.Errorf("Get(%x) = %x, %d, want %x, %d", id, entry.OutputID, entry.Size, dummyID(i*99), int64(i)*101)
97 for i := 0; i < n; i++ {
98 id := ActionID(dummyID(i))
99 entry, err := c.Get(id)
101 t.Fatalf("Get2(%x): %v", id, err)
103 if entry.OutputID != dummyID(i*99) || entry.Size != int64(i)*101 {
104 t.Errorf("Get2(%x) = %x, %d, want %x, %d", id, entry.OutputID, entry.Size, dummyID(i*99), int64(i)*101)
109 func TestVerifyPanic(t *testing.T) {
110 os.Setenv("GODEBUG", "gocacheverify=1")
113 os.Unsetenv("GODEBUG")
118 t.Fatal("initEnv did not set verify")
121 dir, err := ioutil.TempDir("", "cachetest-")
125 defer os.RemoveAll(dir)
129 t.Fatalf("Open: %v", err)
132 id := ActionID(dummyID(1))
133 if err := c.PutBytes(id, []byte("abc")); err != nil {
138 if err := recover(); err != nil {
143 c.PutBytes(id, []byte("def"))
144 t.Fatal("mismatched Put did not panic in verify mode")
147 func dummyID(x int) [HashSize]byte {
148 var out [HashSize]byte
149 binary.LittleEndian.PutUint64(out[:], uint64(x))
153 func TestCacheTrim(t *testing.T) {
154 dir, err := ioutil.TempDir("", "cachetest-")
158 defer os.RemoveAll(dir)
162 t.Fatalf("Open: %v", err)
164 const start = 1000000000
166 c.now = func() time.Time { return time.Unix(now, 0) }
168 checkTime := func(name string, mtime int64) {
170 file := filepath.Join(c.dir, name[:2], name)
171 info, err := os.Stat(file)
175 if info.ModTime().Unix() != mtime {
176 t.Fatalf("%s mtime = %d, want %d", name, info.ModTime().Unix(), mtime)
180 id := ActionID(dummyID(1))
181 c.PutBytes(id, []byte("abc"))
182 entry, _ := c.Get(id)
183 c.PutBytes(ActionID(dummyID(2)), []byte("def"))
185 checkTime(fmt.Sprintf("%x-a", id), mtime)
186 checkTime(fmt.Sprintf("%x-d", entry.OutputID), mtime)
188 // Get should not change recent mtimes.
191 checkTime(fmt.Sprintf("%x-a", id), mtime)
192 checkTime(fmt.Sprintf("%x-d", entry.OutputID), mtime)
194 // Get should change distant mtimes.
197 if _, err := c.Get(id); err != nil {
200 c.OutputFile(entry.OutputID)
201 checkTime(fmt.Sprintf("%x-a", id), mtime2)
202 checkTime(fmt.Sprintf("%x-d", entry.OutputID), mtime2)
204 // Trim should leave everything alone: it's all too new.
206 if _, err := c.Get(id); err != nil {
209 c.OutputFile(entry.OutputID)
210 data, err := ioutil.ReadFile(filepath.Join(dir, "trim.txt"))
214 checkTime(fmt.Sprintf("%x-a", dummyID(2)), start)
216 // Trim less than a day later should not do any work at all.
219 if _, err := c.Get(id); err != nil {
222 c.OutputFile(entry.OutputID)
223 data2, err := ioutil.ReadFile(filepath.Join(dir, "trim.txt"))
227 if !bytes.Equal(data, data2) {
228 t.Fatalf("second trim did work: %q -> %q", data, data2)
231 // Fast forward and do another trim just before the 5 day cutoff.
232 // Note that because of usedQuantum the cutoff is actually 5 days + 1 hour.
233 // We used c.Get(id) just now, so 5 days later it should still be kept.
234 // On the other hand almost a full day has gone by since we wrote dummyID(2)
235 // and we haven't looked at it since, so 5 days later it should be gone.
237 checkTime(fmt.Sprintf("%x-a", dummyID(2)), start)
239 if _, err := c.Get(id); err != nil {
242 c.OutputFile(entry.OutputID)
244 if _, err := c.Get(dummyID(2)); err == nil { // haven't done a Get for this since original write above
245 t.Fatalf("Trim did not remove dummyID(2)")
248 // The c.Get(id) refreshed id's mtime again.
249 // Check that another 5 days later it is still not gone,
250 // but check by using checkTime, which doesn't bring mtime forward.
253 checkTime(fmt.Sprintf("%x-a", id), mtime3)
254 checkTime(fmt.Sprintf("%x-d", entry.OutputID), mtime3)
256 // Half a day later Trim should still be a no-op, because there was a Trim recently.
257 // Even though the entry for id is now old enough to be trimmed,
258 // it gets a reprieve until the time comes for a new Trim scan.
261 checkTime(fmt.Sprintf("%x-a", id), mtime3)
262 checkTime(fmt.Sprintf("%x-d", entry.OutputID), mtime3)
264 // Another half a day later, Trim should actually run, and it should remove id.
267 if _, err := c.Get(dummyID(1)); err == nil {
268 t.Fatal("Trim did not remove dummyID(1)")