diff options
| author | Saiyed Shizain <saiyedshizain@gmail.com> | 2026-07-17 11:10:53 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-07-17 11:10:53 +0530 |
| commit | 6afb80ab754da137939dd523ee419818026f9fb5 (patch) | |
| tree | f9f94ec69ac87abe8060cefca8708572bc13bdd6 /internal/watcher/debounce_test.go | |
| parent | bcfb199ac8e5399e1a92cfbbc28af3674f80959d (diff) | |
| parent | 5ae787be3d1d9de3355f8b5662fea857204e693c (diff) | |
| download | galleryd-6afb80ab754da137939dd523ee419818026f9fb5.tar.xz galleryd-6afb80ab754da137939dd523ee419818026f9fb5.zip | |
Merge pull request #11 from RanXom/feat/live-gallery
Feat/live-gallery: live gallery with automatic filesystem reloads
Diffstat (limited to 'internal/watcher/debounce_test.go')
| -rw-r--r-- | internal/watcher/debounce_test.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/internal/watcher/debounce_test.go b/internal/watcher/debounce_test.go new file mode 100644 index 0000000..007dbd4 --- /dev/null +++ b/internal/watcher/debounce_test.go @@ -0,0 +1,79 @@ +package watcher + +import ( + "sync/atomic" + "testing" + "time" +) + +func TestDebounceCollapsesNotifications(t *testing.T) { + done := make(chan struct{}, 1) + var calls atomic.Int32 + + w := New(Config{ + Debounce: 25 * time.Millisecond, + OnChange: func() { + calls.Add(1) + + select { + case done <- struct{}{}: + default: + } + }, + }) + + go w.debounceLoop(t.Context()) + + for range 10 { + w.notify() + } + + select { + case <-done: + case <-time.After(time.Second): + t.Fatal("timed out waiting for callback") + } + + if got := calls.Load(); got != 1 { + t.Fatalf("expected 1 callback, got %d", got) + } +} + +func TestDebounceSeparateBursts(t *testing.T) { + done := make(chan struct{}, 2) + var calls atomic.Int32 + + w := New(Config{ + Debounce: 20 * time.Millisecond, + OnChange: func() { + calls.Add(1) + + select { + case done <- struct{}{}: + default: + } + }, + }) + + go w.debounceLoop(t.Context()) + + w.notify() + + select { + case <-done: + case <-time.After(time.Second): + t.Fatal("timed out waiting for first callback") + } + + w.notify() + + select { + case <-done: + case <-time.After(time.Second): + t.Fatal("timed out waiting for second callback") + } + + if got := calls.Load(); got != 2 { + t.Fatalf("expected 2 callbacks, got %d", got) + } +} |