check / check (push) Waiting to run
rules and lookup each had the same loop: watch the directory, start a timer at once, restart it at each change that counts, read again when it fires. It now lives once in internal/dirwatch, with its quiet time and its tests. Each package keeps its own log messages, its readAgain, and its test for which changes count. internal/state keeps its own copy. Model: opus-5-5
168 lines
3.7 KiB
Go
168 lines
3.7 KiB
Go
package dirwatch
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"path/filepath"
|
|
"sync/atomic"
|
|
"testing"
|
|
"testing/synctest"
|
|
"time"
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
)
|
|
|
|
// errWatch is an error the watch reports.
|
|
var errWatch = errors.New("too many changes")
|
|
|
|
func TestDirectoryThatCannotBeWatchedIsAnError(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := New(filepath.Join(t.TempDir(), "missing"))
|
|
if err == nil {
|
|
t.Error("a missing directory is watched, want an error")
|
|
}
|
|
}
|
|
|
|
// The tests below run callWhenQuiet in a synctest bubble, where time is a
|
|
// clock of the test's own: time.Sleep moves it on at once, and
|
|
// synctest.Wait returns once callWhenQuiet waits again, so that every call
|
|
// due by then is made. The test sends the changes itself, as the watch of
|
|
// a directory cannot run in a bubble.
|
|
|
|
func TestSecondChangeStartsTheWaitAgain(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
synctest.Test(t, func(t *testing.T) {
|
|
loop := start(t, everyPath)
|
|
|
|
loop.changes <- fsnotify.Event{Name: "50-app.rules", Op: fsnotify.Write}
|
|
|
|
time.Sleep(quietTime - time.Nanosecond)
|
|
synctest.Wait()
|
|
loop.wantCalls(t, 0)
|
|
|
|
// The second part starts the wait again.
|
|
loop.changes <- fsnotify.Event{Name: "50-app.rules", Op: fsnotify.Write}
|
|
|
|
time.Sleep(quietTime - time.Nanosecond)
|
|
synctest.Wait()
|
|
loop.wantCalls(t, 0)
|
|
|
|
time.Sleep(time.Nanosecond)
|
|
synctest.Wait()
|
|
loop.wantCalls(t, 1)
|
|
})
|
|
}
|
|
|
|
func TestCalledOnceQuietAfterTheStart(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
synctest.Test(t, func(t *testing.T) {
|
|
// An edit saved before the watch began sends no change, and is
|
|
// taken in by this call.
|
|
loop := start(t, everyPath)
|
|
|
|
time.Sleep(quietTime)
|
|
synctest.Wait()
|
|
loop.wantCalls(t, 1)
|
|
|
|
// Long after, it has been called only once.
|
|
time.Sleep(time.Hour)
|
|
synctest.Wait()
|
|
loop.wantCalls(t, 1)
|
|
})
|
|
}
|
|
|
|
func TestChangeThatDoesNotCountIsNoChange(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
synctest.Test(t, func(t *testing.T) {
|
|
loop := start(t, func(path string) bool {
|
|
return path == "ipinfo_lite.mmdb"
|
|
})
|
|
|
|
// The wait that starts with the watch ends with a call.
|
|
time.Sleep(quietTime)
|
|
synctest.Wait()
|
|
|
|
loop.changes <- fsnotify.Event{Name: "other.mmdb", Op: fsnotify.Create}
|
|
|
|
time.Sleep(quietTime)
|
|
synctest.Wait()
|
|
loop.wantCalls(t, 1)
|
|
|
|
loop.changes <- fsnotify.Event{Name: "ipinfo_lite.mmdb", Op: fsnotify.Write}
|
|
|
|
time.Sleep(quietTime)
|
|
synctest.Wait()
|
|
loop.wantCalls(t, 2)
|
|
})
|
|
}
|
|
|
|
func TestErrorsOfTheWatchHandedOn(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
synctest.Test(t, func(t *testing.T) {
|
|
loop := start(t, everyPath)
|
|
|
|
loop.errs <- errWatch
|
|
|
|
synctest.Wait()
|
|
|
|
if !errors.Is(loop.failure, errWatch) {
|
|
t.Errorf("failed with %v, want %v", loop.failure, errWatch)
|
|
}
|
|
})
|
|
}
|
|
|
|
// loop is a callWhenQuiet that a test runs.
|
|
type loop struct {
|
|
// changes and errs send it changes and errors.
|
|
changes chan fsnotify.Event
|
|
errs chan error
|
|
// calls counts its calls of changed, and failure is the last error it
|
|
// handed to failed.
|
|
calls atomic.Int32
|
|
failure error
|
|
}
|
|
|
|
// start runs callWhenQuiet, with counts, until the test ends.
|
|
func start(t *testing.T, counts func(path string) bool) *loop {
|
|
t.Helper()
|
|
|
|
l := &loop{changes: make(chan fsnotify.Event), errs: make(chan error)}
|
|
ctx, stop := context.WithCancel(t.Context())
|
|
stopped := make(chan struct{})
|
|
|
|
go func() {
|
|
callWhenQuiet(ctx, l.changes, l.errs, counts,
|
|
func() { l.calls.Add(1) },
|
|
func(err error) { l.failure = err },
|
|
)
|
|
close(stopped)
|
|
}()
|
|
|
|
t.Cleanup(func() {
|
|
stop()
|
|
<-stopped
|
|
})
|
|
|
|
return l
|
|
}
|
|
|
|
// everyPath counts a change to any path.
|
|
func everyPath(string) bool {
|
|
return true
|
|
}
|
|
|
|
// wantCalls checks how many times changed has been called.
|
|
func (l *loop) wantCalls(t *testing.T, want int32) {
|
|
t.Helper()
|
|
|
|
got := l.calls.Load()
|
|
if got != want {
|
|
t.Errorf("changed called %d times, want %d", got, want)
|
|
}
|
|
}
|