Skip to content

Commit

Permalink
fix test case file write/stat race
Browse files Browse the repository at this point in the history
On github actions test file_test fails occasionally when the stat
returns the file has changed before the data has been written. That is
the stat shows an update, it reads the file and can't find the change.

I had attempted to fix this before with a Sync, which seemed to help at
the time, but obviously didn't fix it. This change removes the file Sync
call with a O_SYNC flag on the file open call. Hopefully this will fix
that test (it is getting annoying).
  • Loading branch information
eikenb committed Aug 12, 2021
1 parent 1b5a0a8 commit c1ae925
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions dependency/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,22 @@ func TestFileQuery_Fetch(t *testing.T) {
}
})

syncWriteFile := func(name string, data []byte, perm os.FileMode) error {
f, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC|os.O_SYNC, perm)
if err == nil {
_, err = f.Write(data)
if err1 := f.Close(); err1 != nil && err == nil {
err = err1
}
}
return err
}
t.Run("fires_changes", func(t *testing.T) {
f, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(f.Name(), []byte("hello"), 0644); err != nil {
if err := syncWriteFile(f.Name(), []byte("hello"), 0644); err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())
Expand Down Expand Up @@ -171,10 +181,7 @@ func TestFileQuery_Fetch(t *testing.T) {
case <-dataCh:
}

if err := ioutil.WriteFile(f.Name(), []byte("goodbye"), 0644); err != nil {
t.Fatal(err)
}
if err := f.Sync(); err != nil {
if err := syncWriteFile(f.Name(), []byte("goodbye"), 0644); err != nil {
t.Fatal(err)
}

Expand Down

0 comments on commit c1ae925

Please sign in to comment.