diff options
Diffstat (limited to 'backup/caching_test.go')
-rw-r--r-- | backup/caching_test.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/backup/caching_test.go b/backup/caching_test.go new file mode 100644 index 0000000..6dad62b --- /dev/null +++ b/backup/caching_test.go @@ -0,0 +1,62 @@ +package backup + +import ( + "code.laria.me/petrific/cache" + "code.laria.me/petrific/fs" + "code.laria.me/petrific/objects" + "code.laria.me/petrific/storage/memory" + "testing" + "time" +) + +func TestCacheMTime(t *testing.T) { + c := cache.NewFileCache("") // location doesn't matter here, just use empty string + st := memory.NewMemoryStorage() + filesys := fs.NewMemoryFSRoot("/foo") + + file, err := filesys.CreateChildFile("bar", false) + if err != nil { + t.Fatal(err) + } + + want := file.ModTime() + if _, err := WriteDir(st, "/foo", filesys, c); err != nil { + t.Fatal(err) + } + + have, _, ok := c.PathUpdated("/foo/bar") + if !ok { + t.Fatal("cache doesn't know anything about /foo/bar") + } + + if !have.Equal(want) { + t.Errorf("Unexpected cache time for /foo/bar (want=%s, have=%s)", want, have) + } +} + +func TestCacheRetrieve(t *testing.T) { + c := cache.NewFileCache("") // location doesn't matter here, just use empty string + st := memory.NewMemoryStorage() + filesys := fs.NewMemoryFSRoot("/foo") + + if err := st.Set(objid_emptyfile, objects.OTFile, obj_emptyfile); err != nil { + t.Fatalf("could not set empty file object: %s", err) + } + + file, err := filesys.CreateChildFile("bar", false) + if err != nil { + t.Fatal(err) + } + mfile := file.(*fs.MemfsFile) + + mtime := file.ModTime().Add(1 * time.Hour) + c.SetPathUpdated("/foo/bar", mtime, objid_emptyfile) + + if _, err := WriteDir(st, "/foo", filesys, c); err != nil { + t.Fatal(err) + } + + if mfile.HasBeenRead { + t.Error("/foo/bar has been read by WriteDir") + } +} |