|
| 1 | +package debounce_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/floatdrop/debounce/v2" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +// helper to collect output with a timeout |
| 13 | +func collect[T any](ch <-chan T, timeout time.Duration) []T { |
| 14 | + var results []T |
| 15 | + timer := time.NewTimer(timeout) |
| 16 | + for { |
| 17 | + select { |
| 18 | + case v, ok := <-ch: |
| 19 | + if !ok { |
| 20 | + return results |
| 21 | + } |
| 22 | + results = append(results, v) |
| 23 | + case <-timer.C: |
| 24 | + return results |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func TestDebounce_LastValueOnly(t *testing.T) { |
| 30 | + in := make(chan int) |
| 31 | + out := debounce.Chan(in, debounce.WithDelay(200*time.Millisecond)) |
| 32 | + |
| 33 | + go func() { |
| 34 | + in <- 1 |
| 35 | + time.Sleep(50 * time.Millisecond) |
| 36 | + in <- 2 |
| 37 | + time.Sleep(50 * time.Millisecond) |
| 38 | + in <- 3 |
| 39 | + time.Sleep(50 * time.Millisecond) |
| 40 | + in <- 4 |
| 41 | + time.Sleep(300 * time.Millisecond) // wait longer than debounce delay |
| 42 | + close(in) |
| 43 | + }() |
| 44 | + |
| 45 | + result := collect(out, 1*time.Second) |
| 46 | + assert.Equal(t, []int{4}, result) |
| 47 | +} |
| 48 | + |
| 49 | +func TestDebounce_MultipleValuesSpacedOut(t *testing.T) { |
| 50 | + in := make(chan int) |
| 51 | + out := debounce.Chan(in, debounce.WithDelay(100*time.Millisecond)) |
| 52 | + |
| 53 | + go func() { |
| 54 | + in <- 1 |
| 55 | + time.Sleep(150 * time.Millisecond) |
| 56 | + in <- 2 |
| 57 | + time.Sleep(150 * time.Millisecond) |
| 58 | + in <- 3 |
| 59 | + time.Sleep(150 * time.Millisecond) |
| 60 | + close(in) |
| 61 | + }() |
| 62 | + |
| 63 | + result := collect(out, 1*time.Second) |
| 64 | + assert.Equal(t, []int{1, 2, 3}, result) |
| 65 | +} |
| 66 | + |
| 67 | +func TestDebounce_WithLimit(t *testing.T) { |
| 68 | + in := make(chan int) |
| 69 | + out := debounce.Chan(in, debounce.WithDelay(200*time.Millisecond), debounce.WithLimit(3)) |
| 70 | + |
| 71 | + go func() { |
| 72 | + in <- 1 |
| 73 | + time.Sleep(50 * time.Millisecond) |
| 74 | + in <- 2 |
| 75 | + time.Sleep(50 * time.Millisecond) |
| 76 | + in <- 3 |
| 77 | + time.Sleep(50 * time.Millisecond) |
| 78 | + in <- 4 |
| 79 | + time.Sleep(300 * time.Millisecond) // wait longer than debounce delay |
| 80 | + close(in) |
| 81 | + }() |
| 82 | + |
| 83 | + result := collect(out, 1*time.Second) |
| 84 | + assert.Equal(t, []int{3, 4}, result) |
| 85 | +} |
| 86 | + |
| 87 | +func TestDebounce_ChannelCloses(t *testing.T) { |
| 88 | + in := make(chan int) |
| 89 | + out := debounce.Chan(in, debounce.WithDelay(100*time.Millisecond)) |
| 90 | + |
| 91 | + go func() { |
| 92 | + in <- 42 |
| 93 | + close(in) |
| 94 | + }() |
| 95 | + |
| 96 | + result := collect(out, 1*time.Second) |
| 97 | + assert.Equal(t, []int{42}, result) |
| 98 | +} |
| 99 | + |
| 100 | +func TestDebounce_EmptyChannelCloses(t *testing.T) { |
| 101 | + in := make(chan int) |
| 102 | + out := debounce.Chan(in, debounce.WithDelay(100*time.Millisecond)) |
| 103 | + |
| 104 | + go func() { |
| 105 | + close(in) |
| 106 | + }() |
| 107 | + |
| 108 | + result := collect(out, 1*time.Second) |
| 109 | + assert.Equal(t, []int(nil), result) |
| 110 | +} |
| 111 | + |
| 112 | +func TestDebounce_ZeroDelay(t *testing.T) { |
| 113 | + in := make(chan int) |
| 114 | + out := debounce.Chan(in) |
| 115 | + assert.Equal(t, (<-chan int)(in), out) |
| 116 | +} |
| 117 | + |
| 118 | +func BenchmarkDebounce_Insert(b *testing.B) { |
| 119 | + in := make(chan int) |
| 120 | + _ = debounce.Chan(in, debounce.WithDelay(100*time.Millisecond)) |
| 121 | + |
| 122 | + b.ResetTimer() |
| 123 | + for i := 0; i < b.N; i++ { |
| 124 | + in <- i |
| 125 | + } |
| 126 | + b.StopTimer() |
| 127 | + close(in) |
| 128 | +} |
0 commit comments