fileutil: update watcher to use fsnotify and polling (#3663)

* fileutil: update watcher to use fsnotify and polling

* raise timeout

* maybe fix
This commit is contained in:
Caleb Doxsey 2022-10-19 09:13:08 -06:00
parent f44c85880b
commit e379e24c6b
9 changed files with 335 additions and 47 deletions

View file

@ -23,6 +23,7 @@ func TestWatcher(t *testing.T) {
}
w := NewWatcher()
defer w.Clear()
w.Add(filepath.Join(tmpdir, "test1.txt"))
ch := w.Bind()
@ -39,3 +40,50 @@ func TestWatcher(t *testing.T) {
t.Error("expected change signal when file is modified")
}
}
func TestWatcherSymlink(t *testing.T) {
t.Parallel()
tmpdir := filepath.Join(os.TempDir(), uuid.New().String())
err := os.MkdirAll(tmpdir, 0o755)
if !assert.NoError(t, err) {
return
}
t.Cleanup(func() { os.RemoveAll(tmpdir) })
err = os.WriteFile(filepath.Join(tmpdir, "test1.txt"), []byte{1, 2, 3, 4}, 0o666)
if !assert.NoError(t, err) {
return
}
err = os.WriteFile(filepath.Join(tmpdir, "test2.txt"), []byte{5, 6, 7, 8}, 0o666)
if !assert.NoError(t, err) {
return
}
assert.NoError(t, os.Symlink(filepath.Join(tmpdir, "test1.txt"), filepath.Join(tmpdir, "symlink1.txt")))
w := NewWatcher()
defer w.Clear()
w.Add(filepath.Join(tmpdir, "symlink1.txt"))
ch := w.Bind()
t.Cleanup(func() { w.Unbind(ch) })
assert.NoError(t, os.WriteFile(filepath.Join(tmpdir, "test1.txt"), []byte{9, 10, 11}, 0o666))
select {
case <-ch:
case <-time.After(time.Second):
t.Error("expected change signal when underlying file is modified")
}
assert.NoError(t, os.Symlink(filepath.Join(tmpdir, "test2.txt"), filepath.Join(tmpdir, "symlink2.txt")))
assert.NoError(t, os.Rename(filepath.Join(tmpdir, "symlink2.txt"), filepath.Join(tmpdir, "symlink1.txt")))
select {
case <-ch:
case <-time.After(10 * time.Second):
t.Error("expected change signal when symlink is changed")
}
}