mirror of
https://github.com/pomerium/pomerium.git
synced 2025-06-27 23:18:13 +02:00
core/config: refactor file watcher (#4702)
* core/config: refactor file watcher * add comments * updates * only use the polling watcher * fix test * fix test * try to fix test again * remove batching * dont rely on file modification timestamp * remove benchmark * try fix again
This commit is contained in:
parent
77bb203276
commit
2771a5ae87
6 changed files with 170 additions and 139 deletions
|
@ -1,79 +1,94 @@
|
|||
package fileutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestWatcher(t *testing.T) {
|
||||
tmpdir := t.TempDir()
|
||||
|
||||
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, "test1.txt"), []byte{1}, 0o666)
|
||||
require.NoError(t, err)
|
||||
|
||||
w := NewWatcher()
|
||||
defer w.Clear()
|
||||
w.Add(filepath.Join(tmpdir, "test1.txt"))
|
||||
|
||||
ch := w.Bind()
|
||||
defer w.Unbind(ch)
|
||||
|
||||
err = os.WriteFile(filepath.Join(tmpdir, "test1.txt"), []byte{5, 6, 7, 8}, 0o666)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
select {
|
||||
case <-ch:
|
||||
case <-time.After(time.Second):
|
||||
t.Error("expected change signal when file is modified")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWatcherSymlink(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmpdir := t.TempDir()
|
||||
|
||||
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"))
|
||||
w.Watch(context.Background(), []string{filepath.Join(tmpdir, "test1.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))
|
||||
err = os.WriteFile(filepath.Join(tmpdir, "test1.txt"), []byte{1, 2}, 0o666)
|
||||
require.NoError(t, err)
|
||||
|
||||
select {
|
||||
case <-ch:
|
||||
case <-time.After(time.Second):
|
||||
t.Error("expected change signal when underlying file is modified")
|
||||
}
|
||||
expectChange(t, ch)
|
||||
}
|
||||
|
||||
func TestWatcherSymlink(t *testing.T) {
|
||||
tmpdir := t.TempDir()
|
||||
|
||||
err := os.WriteFile(filepath.Join(tmpdir, "test1.txt"), []byte{1}, 0o666)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = os.WriteFile(filepath.Join(tmpdir, "test2.txt"), []byte{1, 2}, 0o666)
|
||||
require.NoError(t, err)
|
||||
|
||||
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")})
|
||||
|
||||
ch := w.Bind()
|
||||
t.Cleanup(func() { w.Unbind(ch) })
|
||||
|
||||
assert.NoError(t, os.WriteFile(filepath.Join(tmpdir, "test1.txt"), []byte{1, 2, 3}, 0o666))
|
||||
|
||||
expectChange(t, ch)
|
||||
|
||||
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")))
|
||||
|
||||
expectChange(t, ch)
|
||||
}
|
||||
|
||||
func TestWatcher_FileRemoval(t *testing.T) {
|
||||
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")})
|
||||
|
||||
ch := w.Bind()
|
||||
t.Cleanup(func() { w.Unbind(ch) })
|
||||
|
||||
err = os.Remove(filepath.Join(tmpdir, "test1.txt"))
|
||||
require.NoError(t, err)
|
||||
|
||||
expectChange(t, ch)
|
||||
|
||||
err = os.WriteFile(filepath.Join(tmpdir, "test1.txt"), []byte{1, 2}, 0o666)
|
||||
require.NoError(t, err)
|
||||
|
||||
expectChange(t, ch)
|
||||
}
|
||||
|
||||
func expectChange(t *testing.T, ch chan context.Context) {
|
||||
t.Helper()
|
||||
|
||||
cnt := 0
|
||||
select {
|
||||
case <-ch:
|
||||
cnt++
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Error("expected change signal when symlink is changed")
|
||||
}
|
||||
if cnt == 0 {
|
||||
t.Error("expected change signal")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue