From 83e6dd9ac97d5f2faf1528228dfcb4fdf47cb3f7 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Fri, 29 Dec 2017 22:18:02 +0100 Subject: Fix caching --- backup/backup.go | 2 +- backup/caching_test.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 backup/caching_test.go (limited to 'backup') diff --git a/backup/backup.go b/backup/backup.go index 84fb554..5e4ffc1 100644 --- a/backup/backup.go +++ b/backup/backup.go @@ -108,7 +108,7 @@ func (proc writeDirProcess) writeDir( switch c.Type() { case fs.FFile: - mtime, file_id, ok := pcache.PathUpdated(abspath) + mtime, file_id, ok := pcache.PathUpdated(abspath + "/" + c.Name()) if !ok || mtime.Before(c.ModTime()) { // According to cache the file was changed 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") + } +} -- cgit v1.2.3-70-g09d2