diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/watcher/debounce_test.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/internal/watcher/debounce_test.go b/internal/watcher/debounce_test.go new file mode 100644 index 0000000..a93ba05 --- /dev/null +++ b/internal/watcher/debounce_test.go @@ -0,0 +1,55 @@ +package watcher + +import ( + "sync/atomic" + "testing" + "time" +) + +func TestDebounceCollapsesNotifications(t *testing.T) { + var calls atomic.Int32 + + w := New(Config{ + Debounce: 25 * time.Millisecond, + OnChange: func() { + calls.Add(1) + }, + }) + + go w.debounceLoop(t.Context()) + + for range 10 { + w.notify() + } + + time.Sleep(75 * time.Millisecond) + + if got := calls.Load(); got != 1 { + t.Fatalf("expected 1 callback, got %d", got) + } +} + +func TestDebounceSeparateBursts(t *testing.T) { + var calls atomic.Int32 + + w := New(Config{ + Debounce: 20 * time.Millisecond, + OnChange: func() { + calls.Add(1) + }, + }) + + go w.debounceLoop(t.Context()) + + w.notify() + + time.Sleep(40 * time.Millisecond) + + w.notify() + + time.Sleep(40 * time.Millisecond) + + if got := calls.Load(); got != 2 { + t.Fatalf("expected 2 callbacks, got %d", got) + } +} |