From 868c02846962558a97a84c085c92baa5ba6e535f Mon Sep 17 00:00:00 2001 From: RanXom Date: Fri, 17 Jul 2026 11:02:36 +0530 Subject: refactor(watcher): refactor tests for faster testing time --- internal/watcher/debounce_test.go | 30 +++++++++++++++++++++++++++--- 1 file 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) -- cgit v1.2.3