fix(graph): stabilize debouncer tests with synctest fake clock
schemas / vulnerabilities (pull_request) Successful in 2m17s
schemas / check-release (pull_request) Successful in 4m9s
schemas / check (pull_request) Successful in 4m52s
pre-commit / pre-commit (pull_request) Successful in 9m43s
schemas / build (pull_request) Successful in 6m58s
schemas / deploy-prod (pull_request) Has been skipped
schemas / vulnerabilities (pull_request) Successful in 2m17s
schemas / check-release (pull_request) Successful in 4m9s
schemas / check (pull_request) Successful in 4m52s
pre-commit / pre-commit (pull_request) Successful in 9m43s
schemas / build (pull_request) Successful in 6m58s
schemas / deploy-prod (pull_request) Has been skipped
Replace real time.Sleep waits with testing/synctest fake clock to eliminate CI flakiness caused by timer races on loaded machines. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+18
-6
@@ -3,6 +3,7 @@ package graph
|
||||
import (
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -10,6 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func TestDebouncer_Coalesces(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
d := NewDebouncer(50 * time.Millisecond)
|
||||
var calls atomic.Int32
|
||||
|
||||
@@ -20,13 +22,16 @@ func TestDebouncer_Coalesces(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// Wait for the debounce delay plus some margin.
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
// Advance fake clock past the debounce delay and let goroutines settle.
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
synctest.Wait()
|
||||
|
||||
assert.Equal(t, int32(1), calls.Load(), "rapid calls should coalesce into a single execution")
|
||||
})
|
||||
}
|
||||
|
||||
func TestDebouncer_DifferentKeys(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
d := NewDebouncer(50 * time.Millisecond)
|
||||
var calls atomic.Int32
|
||||
|
||||
@@ -34,24 +39,31 @@ func TestDebouncer_DifferentKeys(t *testing.T) {
|
||||
d.Debounce("key-b", func() { calls.Add(1) })
|
||||
d.Debounce("key-c", func() { calls.Add(1) })
|
||||
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
synctest.Wait()
|
||||
|
||||
assert.Equal(t, int32(3), calls.Load(), "different keys should fire independently")
|
||||
})
|
||||
}
|
||||
|
||||
func TestDebouncer_TimerReset(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
d := NewDebouncer(100 * time.Millisecond)
|
||||
var value atomic.Int32
|
||||
|
||||
// First call sets value to 1.
|
||||
d.Debounce("key", func() { value.Store(1) })
|
||||
|
||||
// Wait 60ms (less than the 100ms delay), then replace with value 2.
|
||||
// Advance 60ms (less than the 100ms delay) — first timer hasn't fired.
|
||||
time.Sleep(60 * time.Millisecond)
|
||||
|
||||
// Replace with value 2 — resets the timer to fire at 60+100 = 160ms.
|
||||
d.Debounce("key", func() { value.Store(2) })
|
||||
|
||||
// At 60ms the first timer hasn't fired yet. Wait for the second timer.
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
// Advance another 100ms (total 160ms) to fire the reset timer.
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
synctest.Wait()
|
||||
|
||||
require.Equal(t, int32(2), value.Load(), "later call should replace the earlier one")
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user