Skip to content

Commit

Permalink
fix flaky file attributes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ashmeenkaur committed Apr 29, 2024
1 parent 70d49e2 commit 45700b3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
26 changes: 10 additions & 16 deletions tools/integration_tests/operations/file_and_dir_attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (

"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/operations"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/setup"
"github.com/jacobsa/fuse/fusetesting"
"github.com/jacobsa/ogletest"
)

const DirAttrTest = "dirAttrTest"
Expand All @@ -40,10 +42,8 @@ func checkIfObjectAttrIsCorrect(objName string, preCreateTime time.Time, postCre
if objName != statObjName {
t.Errorf("File name not matched in os.Stat, found: %s, expected: %s", statObjName, objName)
}
statModTime := oStat.ModTime().Round(time.Second)
if (preCreateTime.After(statModTime)) || (postCreateTime.Before(statModTime)) {
t.Errorf("File modification time not in the expected time-range")
}
ogletest.ExpectThat(oStat, fusetesting.MtimeIsWithin(preCreateTime, operations.TimeSlop))
ogletest.ExpectThat(oStat, fusetesting.MtimeIsWithin(postCreateTime, operations.TimeSlop))

if oStat.Size() != byteSize {
t.Errorf("File size is not %v bytes, found size: %d bytes", BytesWrittenInFile, oStat.Size())
Expand All @@ -54,11 +54,9 @@ func TestFileAttributes(t *testing.T) {
// Clean the mountedDirectory before running test.
setup.CleanMntDir()

// kernel time can be slightly out of sync of time.Now(), so rounding off
// times to seconds. Ref: https://github.com/golang/go/issues/33510
preCreateTime := time.Now().Round(time.Second)
preCreateTime := time.Now()
fileName := setup.CreateTempFile()
postCreateTime := time.Now().Round(time.Second)
postCreateTime := time.Now()

// The file size in createTempFile() is BytesWrittenInFile bytes
// https://github.com/GoogleCloudPlatform/gcsfuse/blob/master/tools/integration_tests/util/setup/setup.go#L124
Expand All @@ -69,12 +67,10 @@ func TestEmptyDirAttributes(t *testing.T) {
// Clean the mountedDirectory before running test.
setup.CleanMntDir()

// kernel time can be slightly out of sync of time.Now(), so rounding off
// times to seconds. Ref: https://github.com/golang/go/issues/33510
preCreateTime := time.Now().Round(time.Second)
preCreateTime := time.Now()
dirName := path.Join(setup.MntDir(), DirAttrTest)
operations.CreateDirectoryWithNFiles(0, dirName, "", t)
postCreateTime := time.Now().Round(time.Second)
postCreateTime := time.Now()

checkIfObjectAttrIsCorrect(dirName, preCreateTime, postCreateTime, 0, t)
}
Expand All @@ -83,12 +79,10 @@ func TestNonEmptyDirAttributes(t *testing.T) {
// Clean the mountedDirectory before running test.
setup.CleanMntDir()

// kernel time can be slightly out of sync of time.Now(), so rounding off
// times to seconds. Ref: https://github.com/golang/go/issues/33510
preCreateTime := time.Now().Round(time.Second)
preCreateTime := time.Now()
dirName := path.Join(setup.MntDir(), DirAttrTest)
operations.CreateDirectoryWithNFiles(NumberOfFilesInDirAttrTest, dirName, PrefixFileInDirAttrTest, t)
postCreateTime := time.Now().Round(time.Second)
postCreateTime := time.Now()

checkIfObjectAttrIsCorrect(dirName, preCreateTime, postCreateTime, 0, t)
}
6 changes: 6 additions & 0 deletions tools/integration_tests/util/operations/file_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ import (
"strings"
"syscall"
"testing"
"time"
)

const (
OneKiB = 1024
OneMiB = OneKiB * OneKiB
// ChunkSizeForContentComparison is currently set to 1 MiB.
ChunkSizeForContentComparison int = OneMiB

// TimeSlop The radius we use for "expect mtime is within"-style assertions as kernel
// time can be slightly out of sync of time.Now().
// Ref: https://github.com/golang/go/issues/33510
TimeSlop = 25 * time.Millisecond
)

func copyFile(srcFileName, dstFileName string, allowOverwrite bool) (err error) {
Expand Down

0 comments on commit 45700b3

Please sign in to comment.