Skip to content

Commit

Permalink
fix flaky test (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong authored Mar 6, 2024
1 parent 38691f9 commit e0fb51f
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions provider/file/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@ func TestFile_Watch(t *testing.T) {
{
description: "write",
action: func(path string) error {
return os.WriteFile(path, []byte(`{"p": {"k": "c"}}`), 0o600)
err := os.WriteFile(path, []byte(`{"p": {"k": "c"}}`), 0o600)
time.Sleep(time.Second) // wait for the file to be written

return err
},
expected: map[string]any{"p": map[string]any{"k": "c"}},
},
{
description: "remove",
action: os.Remove,
action: func(path string) error {
err := os.Remove(path)
for _, e := os.Stat(path); os.IsExist(e); _, err = os.Stat(path) { //nolint:revive
// wait for the file to be removed
}

return err
},
},
}

Expand All @@ -43,7 +53,9 @@ func TestFile_Watch(t *testing.T) {

tmpFile := filepath.Join(t.TempDir(), "watch.json")
assert.NoError(t, os.WriteFile(tmpFile, []byte(`{"p": {"k": "v"}}`), 0o600))
time.Sleep(100 * time.Millisecond) // wait for the file to be written
for _, err := os.Stat(tmpFile); os.IsNotExist(err); _, err = os.Stat(tmpFile) { //nolint:revive
// wait for the file to be written
}

values := make(chan map[string]any)

Expand All @@ -62,7 +74,6 @@ func TestFile_Watch(t *testing.T) {
time.Sleep(time.Second) // wait for the watcher to start

assert.NoError(t, testcase.action(tmpFile))
time.Sleep(time.Second) // wait for the watcher to pick up the change from action
assert.Equal(t, testcase.expected, <-values)
})
}
Expand Down

0 comments on commit e0fb51f

Please sign in to comment.