fileutil: reimplement file watcher (#5498)

* remove context, add close

* update tests

* cleanup

* fileutil: reimplement file watcher

* remove test, simplify tree set code, fix data race
This commit is contained in:
Caleb Doxsey 2025-02-26 09:21:06 -07:00 committed by GitHub
parent 1b2618170d
commit 1f30dead31
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 332 additions and 103 deletions

View file

@ -12,13 +12,16 @@ import (
)
func TestWatcher(t *testing.T) {
t.Parallel()
tmpdir := t.TempDir()
err := os.WriteFile(filepath.Join(tmpdir, "test1.txt"), []byte{1}, 0o666)
require.NoError(t, err)
w := NewWatcher()
w.Watch(context.Background(), []string{filepath.Join(tmpdir, "test1.txt")})
defer w.Close()
w.Watch([]string{filepath.Join(tmpdir, "test1.txt")})
ch := w.Bind()
t.Cleanup(func() { w.Unbind(ch) })
@ -30,6 +33,8 @@ func TestWatcher(t *testing.T) {
}
func TestWatcherSymlink(t *testing.T) {
t.Parallel()
tmpdir := t.TempDir()
err := os.WriteFile(filepath.Join(tmpdir, "test1.txt"), []byte{1}, 0o666)
@ -41,7 +46,8 @@ func TestWatcherSymlink(t *testing.T) {
assert.NoError(t, os.Symlink(filepath.Join(tmpdir, "test1.txt"), filepath.Join(tmpdir, "symlink1.txt")))
w := NewWatcher()
w.Watch(context.Background(), []string{filepath.Join(tmpdir, "symlink1.txt")})
defer w.Close()
w.Watch([]string{filepath.Join(tmpdir, "symlink1.txt")})
ch := w.Bind()
t.Cleanup(func() { w.Unbind(ch) })
@ -57,13 +63,16 @@ func TestWatcherSymlink(t *testing.T) {
}
func TestWatcher_FileRemoval(t *testing.T) {
t.Parallel()
tmpdir := t.TempDir()
err := os.WriteFile(filepath.Join(tmpdir, "test1.txt"), []byte{1}, 0o666)
require.NoError(t, err)
w := NewWatcher()
w.Watch(context.Background(), []string{filepath.Join(tmpdir, "test1.txt")})
defer w.Close()
w.Watch([]string{filepath.Join(tmpdir, "test1.txt")})
ch := w.Bind()
t.Cleanup(func() { w.Unbind(ch) })
@ -79,6 +88,56 @@ func TestWatcher_FileRemoval(t *testing.T) {
expectChange(t, ch)
}
func TestWatcher_FileModification(t *testing.T) {
t.Parallel()
tmpdir := t.TempDir()
nm := filepath.Join(tmpdir, "test1.txt")
now := time.Now()
require.NoError(t, os.WriteFile(nm, []byte{1, 2, 3, 4}, 0o666))
require.NoError(t, os.Chtimes(nm, now, now))
w := NewWatcher()
defer w.Close()
w.Watch([]string{nm})
ch := w.Bind()
t.Cleanup(func() { w.Unbind(ch) })
require.NoError(t, os.WriteFile(nm, []byte{5, 6, 7, 8}, 0o666))
require.NoError(t, os.Chtimes(nm, now, now))
expectChange(t, ch)
}
func TestWatcher_UnWatch(t *testing.T) {
t.Parallel()
tmpdir := t.TempDir()
nm := filepath.Join(tmpdir, "test1.txt")
now := time.Now()
require.NoError(t, os.WriteFile(nm, []byte{1, 2, 3}, 0o666))
require.NoError(t, os.Chtimes(nm, now, now))
w := NewWatcher()
defer w.Close()
ch := w.Bind()
t.Cleanup(func() { w.Unbind(ch) })
w.Watch([]string{nm})
require.NoError(t, os.WriteFile(nm, []byte{4, 5, 6}, 0o666))
require.NoError(t, os.Chtimes(nm, now, now))
expectChange(t, ch)
w.Watch(nil)
require.NoError(t, os.WriteFile(nm, []byte{7, 8, 9}, 0o666))
require.NoError(t, os.Chtimes(nm, now, now))
expectNoChange(t, ch)
}
func expectChange(t *testing.T, ch chan context.Context) {
t.Helper()
@ -86,9 +145,19 @@ func expectChange(t *testing.T, ch chan context.Context) {
select {
case <-ch:
cnt++
case <-time.After(10 * time.Second):
}
if cnt == 0 {
t.Error("expected change signal")
case <-time.After(2 * pollingInterval):
}
assert.Greater(t, cnt, 0, "should signal a change")
}
func expectNoChange(t *testing.T, ch chan context.Context) {
t.Helper()
cnt := 0
select {
case <-ch:
cnt++
case <-time.After(2 * pollingInterval):
}
assert.Equal(t, 0, cnt, "should not signal a change")
}