From d6e247cc46f8c83e6a4858d4a1feec8bc6d14c2f Mon Sep 17 00:00:00 2001 From: Aaron Jacobs Date: Tue, 11 Aug 2015 06:19:14 +0000 Subject: [PATCH] Fixed several mtime assertions. --- fusetesting/stat.go | 26 +++++++++++++++----------- samples/memfs/memfs_test.go | 15 +++++++++------ 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/fusetesting/stat.go b/fusetesting/stat.go index c084303..c8bdbc1 100644 --- a/fusetesting/stat.go +++ b/fusetesting/stat.go @@ -28,28 +28,32 @@ import ( // also that it matches. func MtimeIs(expected time.Time) oglematchers.Matcher { return oglematchers.NewMatcher( - func(c interface{}) error { return mtimeIs(c, expected) }, + func(c interface{}) error { return mtimeIsWithin(c, expected, 0) }, fmt.Sprintf("mtime is %v", expected)) } -func mtimeIs(c interface{}, expected time.Time) error { +// Like MtimeIs, but allows for a tolerance. +func MtimeIsWithin(expected time.Time, d time.Duration) oglematchers.Matcher { + return oglematchers.NewMatcher( + func(c interface{}) error { return mtimeIsWithin(c, expected, d) }, + fmt.Sprintf("mtime is within %v of %v", d, expected)) +} + +func mtimeIsWithin(c interface{}, expected time.Time, d time.Duration) error { fi, ok := c.(os.FileInfo) if !ok { return fmt.Errorf("which is of type %v", reflect.TypeOf(c)) } // Check ModTime(). - if fi.ModTime() != expected { - d := fi.ModTime().Sub(expected) - return fmt.Errorf("which has mtime %v, off by %v", fi.ModTime(), d) + diff := fi.ModTime().Sub(expected) + absDiff := diff + if absDiff < 0 { + absDiff = -absDiff } - // Check Sys(). - if sysMtime, ok := extractMtime(fi.Sys()); ok { - if sysMtime != expected { - d := sysMtime.Sub(expected) - return fmt.Errorf("which has Sys() mtime %v, off by %v", sysMtime, d) - } + if !(absDiff < d) { + return fmt.Errorf("which has mtime %v, off by %v", fi.ModTime(), diff) } return nil diff --git a/samples/memfs/memfs_test.go b/samples/memfs/memfs_test.go index f28529b..3906d12 100644 --- a/samples/memfs/memfs_test.go +++ b/samples/memfs/memfs_test.go @@ -35,6 +35,9 @@ import ( func TestMemFS(t *testing.T) { RunTests(t) } +// TODO(jacobsa): Comments. +const timeSlop = 5 * time.Millisecond + //////////////////////////////////////////////////////////////////////// // Helpers //////////////////////////////////////////////////////////////////////// @@ -125,7 +128,7 @@ func (t *MemFSTest) Mkdir_OneLevel() { ExpectEq("dir", fi.Name()) ExpectEq(0, fi.Size()) ExpectEq(os.ModeDir|applyUmask(0754), fi.Mode()) - ExpectThat(fi, fusetesting.MtimeIs(createTime)) + ExpectThat(fi, fusetesting.MtimeIsWithin(createTime, timeSlop)) ExpectThat(fi, fusetesting.BirthtimeIs(createTime)) ExpectTrue(fi.IsDir()) @@ -181,7 +184,7 @@ func (t *MemFSTest) Mkdir_TwoLevels() { ExpectEq("dir", fi.Name()) ExpectEq(0, fi.Size()) ExpectEq(os.ModeDir|applyUmask(0754), fi.Mode()) - ExpectThat(fi, fusetesting.MtimeIs(createTime)) + ExpectThat(fi, fusetesting.MtimeIsWithin(createTime, timeSlop)) ExpectThat(fi, fusetesting.BirthtimeIs(createTime)) ExpectTrue(fi.IsDir()) @@ -290,7 +293,7 @@ func (t *MemFSTest) CreateNewFile_InRoot() { ExpectEq("foo", fi.Name()) ExpectEq(len(contents), fi.Size()) ExpectEq(applyUmask(0400), fi.Mode()) - ExpectThat(fi, fusetesting.MtimeIs(createTime)) + ExpectThat(fi, fusetesting.MtimeIsWithin(createTime, timeSlop)) ExpectThat(fi, fusetesting.BirthtimeIs(createTime)) ExpectFalse(fi.IsDir()) @@ -332,7 +335,7 @@ func (t *MemFSTest) CreateNewFile_InSubDir() { ExpectEq("foo", fi.Name()) ExpectEq(len(contents), fi.Size()) ExpectEq(applyUmask(0400), fi.Mode()) - ExpectThat(fi, fusetesting.MtimeIs(createTime)) + ExpectThat(fi, fusetesting.MtimeIsWithin(createTime, timeSlop)) ExpectThat(fi, fusetesting.BirthtimeIs(createTime)) ExpectFalse(fi.IsDir()) @@ -379,7 +382,7 @@ func (t *MemFSTest) ModifyExistingFile_InRoot() { ExpectEq("foo", fi.Name()) ExpectEq(len("Hello, world!"), fi.Size()) ExpectEq(applyUmask(0600), fi.Mode()) - ExpectThat(fi, fusetesting.MtimeIs(modifyTime)) + ExpectThat(fi, fusetesting.MtimeIsWithin(modifyTime, timeSlop)) ExpectThat(fi, fusetesting.BirthtimeIs(createTime)) ExpectFalse(fi.IsDir()) @@ -431,7 +434,7 @@ func (t *MemFSTest) ModifyExistingFile_InSubDir() { ExpectEq("foo", fi.Name()) ExpectEq(len("Hello, world!"), fi.Size()) ExpectEq(applyUmask(0600), fi.Mode()) - ExpectThat(fi, fusetesting.MtimeIs(modifyTime)) + ExpectThat(fi, fusetesting.MtimeIsWithin(modifyTime, timeSlop)) ExpectThat(fi, fusetesting.BirthtimeIs(createTime)) ExpectFalse(fi.IsDir())