diff options
| author | RanXom <saiyedshizain@gmail.com> | 2026-07-17 11:00:10 +0530 |
|---|---|---|
| committer | RanXom <saiyedshizain@gmail.com> | 2026-07-17 11:00:10 +0530 |
| commit | cc42b82782eca935beb6b552e293e4458bb715bc (patch) | |
| tree | 2d7578e42f75c827f86c6ec7fa567c808f7987a4 /internal/watcher/debounce_test.go | |
| parent | 39445365fdc472087d429c7c81ffb02909168a99 (diff) | |
| download | galleryd-cc42b82782eca935beb6b552e293e4458bb715bc.tar.xz galleryd-cc42b82782eca935beb6b552e293e4458bb715bc.zip | |
test(watcher): verify debounce behavior
Diffstat (limited to 'internal/watcher/debounce_test.go')
| -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) + } +} |