2026-02-23 08:05:47 +01:00
|
|
|
package graph
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"sync/atomic"
|
|
|
|
|
"testing"
|
2026-02-25 13:25:18 +01:00
|
|
|
"testing/synctest"
|
2026-02-23 08:05:47 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestDebouncer_Coalesces(t *testing.T) {
|
2026-02-25 13:25:18 +01:00
|
|
|
synctest.Test(t, func(t *testing.T) {
|
|
|
|
|
d := NewDebouncer(50 * time.Millisecond)
|
|
|
|
|
var calls atomic.Int32
|
2026-02-23 08:05:47 +01:00
|
|
|
|
2026-02-25 13:25:18 +01:00
|
|
|
// Fire 10 rapid calls for the same key — only the last should execute.
|
|
|
|
|
for range 10 {
|
|
|
|
|
d.Debounce("key1", func() {
|
|
|
|
|
calls.Add(1)
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-02-23 08:05:47 +01:00
|
|
|
|
2026-02-25 13:25:18 +01:00
|
|
|
// Advance fake clock past the debounce delay and let goroutines settle.
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
synctest.Wait()
|
2026-02-23 08:05:47 +01:00
|
|
|
|
2026-02-25 13:25:18 +01:00
|
|
|
assert.Equal(t, int32(1), calls.Load(), "rapid calls should coalesce into a single execution")
|
|
|
|
|
})
|
2026-02-23 08:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDebouncer_DifferentKeys(t *testing.T) {
|
2026-02-25 13:25:18 +01:00
|
|
|
synctest.Test(t, func(t *testing.T) {
|
|
|
|
|
d := NewDebouncer(50 * time.Millisecond)
|
|
|
|
|
var calls atomic.Int32
|
2026-02-23 08:05:47 +01:00
|
|
|
|
2026-02-25 13:25:18 +01:00
|
|
|
d.Debounce("key-a", func() { calls.Add(1) })
|
|
|
|
|
d.Debounce("key-b", func() { calls.Add(1) })
|
|
|
|
|
d.Debounce("key-c", func() { calls.Add(1) })
|
2026-02-23 08:05:47 +01:00
|
|
|
|
2026-02-25 13:25:18 +01:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
synctest.Wait()
|
2026-02-23 08:05:47 +01:00
|
|
|
|
2026-02-25 13:25:18 +01:00
|
|
|
assert.Equal(t, int32(3), calls.Load(), "different keys should fire independently")
|
|
|
|
|
})
|
2026-02-23 08:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDebouncer_TimerReset(t *testing.T) {
|
2026-02-25 13:25:18 +01:00
|
|
|
synctest.Test(t, func(t *testing.T) {
|
|
|
|
|
d := NewDebouncer(100 * time.Millisecond)
|
|
|
|
|
var value atomic.Int32
|
2026-02-23 08:05:47 +01:00
|
|
|
|
2026-02-25 13:25:18 +01:00
|
|
|
// First call sets value to 1.
|
|
|
|
|
d.Debounce("key", func() { value.Store(1) })
|
2026-02-23 08:05:47 +01:00
|
|
|
|
2026-02-25 13:25:18 +01:00
|
|
|
// Advance 60ms (less than the 100ms delay) — first timer hasn't fired.
|
|
|
|
|
time.Sleep(60 * time.Millisecond)
|
2026-02-23 08:05:47 +01:00
|
|
|
|
2026-02-25 13:25:18 +01:00
|
|
|
// Replace with value 2 — resets the timer to fire at 60+100 = 160ms.
|
|
|
|
|
d.Debounce("key", func() { value.Store(2) })
|
2026-02-23 08:05:47 +01:00
|
|
|
|
2026-02-25 13:25:18 +01:00
|
|
|
// 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")
|
|
|
|
|
})
|
2026-02-23 08:05:47 +01:00
|
|
|
}
|