diff options
Diffstat (limited to 'internal/watcher/debounce_test.go')
| -rw-r--r-- | internal/watcher/debounce_test.go | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/internal/watcher/debounce_test.go b/internal/watcher/debounce_test.go index a93ba05..007dbd4 100644 --- a/internal/watcher/debounce_test.go +++ b/internal/watcher/debounce_test.go @@ -7,12 +7,18 @@ import ( ) 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: + } }, }) @@ -22,7 +28,11 @@ func TestDebounceCollapsesNotifications(t *testing.T) { w.notify() } - time.Sleep(75 * time.Millisecond) + 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) @@ -30,12 +40,18 @@ func TestDebounceCollapsesNotifications(t *testing.T) { } 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: + } }, }) @@ -43,11 +59,19 @@ func TestDebounceSeparateBursts(t *testing.T) { w.notify() - time.Sleep(40 * time.Millisecond) + select { + case <-done: + case <-time.After(time.Second): + t.Fatal("timed out waiting for first callback") + } w.notify() - time.Sleep(40 * time.Millisecond) + 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) |