Skip to content

Commit

Permalink
fix flaky modification time check in integration tests (#1875)
Browse files Browse the repository at this point in the history
* fix flaky file attributes tests

* changes to use time slop
  • Loading branch information
ashmeenkaur committed May 2, 2024
1 parent f2c3405 commit 485cd8d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
30 changes: 17 additions & 13 deletions tools/integration_tests/operations/file_and_dir_attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,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)

statModTime := oStat.ModTime()
if (preCreateTime.After(statModTime)) || (postCreateTime.Before(statModTime)) {
t.Errorf("File modification time not in the expected time-range")
}
Expand All @@ -53,12 +54,13 @@ func checkIfObjectAttrIsCorrect(objName string, preCreateTime time.Time, postCre
func TestFileAttributes(t *testing.T) {
testDir := setup.SetupTestDirectory(DirForOperationTests)

// 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)
// kernel time can be slightly out of sync of time.Now(), so using
// operations.TimeSlop to adjust pre and post create time.
// Ref: https://github.com/golang/go/issues/33510
preCreateTime := time.Now().Add(-operations.TimeSlop)
fileName := path.Join(testDir, tempFileName)
operations.CreateFileWithContent(fileName, setup.FilePermission_0600, Content, t)
postCreateTime := time.Now().Round(time.Second)
postCreateTime := time.Now().Add(+operations.TimeSlop)

// 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 @@ -68,25 +70,27 @@ func TestFileAttributes(t *testing.T) {
func TestEmptyDirAttributes(t *testing.T) {
testDir := setup.SetupTestDirectory(DirForOperationTests)

// 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)
// kernel time can be slightly out of sync of time.Now(), so using
// operations.TimeSlop to adjust pre and post create time.
// Ref: https://github.com/golang/go/issues/33510
preCreateTime := time.Now().Add(-operations.TimeSlop)
dirName := path.Join(testDir, DirAttrTest)
operations.CreateDirectoryWithNFiles(0, dirName, "", t)
postCreateTime := time.Now().Round(time.Second)
postCreateTime := time.Now().Add(operations.TimeSlop)

checkIfObjectAttrIsCorrect(path.Join(testDir, DirAttrTest), preCreateTime, postCreateTime, 0, t)
}

func TestNonEmptyDirAttributes(t *testing.T) {
testDir := setup.SetupTestDirectory(DirForOperationTests)

// 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)
// kernel time can be slightly out of sync of time.Now(), so using
// operations.TimeSlop to adjust pre and post create time.
// Ref: https://github.com/golang/go/issues/33510
preCreateTime := time.Now().Add(-operations.TimeSlop)
dirName := path.Join(testDir, DirAttrTest)
operations.CreateDirectoryWithNFiles(NumberOfFilesInDirAttrTest, dirName, PrefixFileInDirAttrTest, t)
postCreateTime := time.Now().Round(time.Second)
postCreateTime := time.Now().Add(operations.TimeSlop)

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 485cd8d

Please sign in to comment.