blob: a93ba058b397b699f350054a495ae88034ee70c3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
package watcher
import (
"sync/atomic"
"testing"
"time"
)
func TestDebounceCollapsesNotifications(t *testing.T) {
var calls atomic.Int32
w := New(Config{
Debounce: 25 * time.Millisecond,
OnChange: func() {
calls.Add(1)
},
})
go w.debounceLoop(t.Context())
for range 10 {
w.notify()
}
time.Sleep(75 * time.Millisecond)
if got := calls.Load(); got != 1 {
t.Fatalf("expected 1 callback, got %d", got)
}
}
func TestDebounceSeparateBursts(t *testing.T) {
var calls atomic.Int32
w := New(Config{
Debounce: 20 * time.Millisecond,
OnChange: func() {
calls.Add(1)
},
})
go w.debounceLoop(t.Context())
w.notify()
time.Sleep(40 * time.Millisecond)
w.notify()
time.Sleep(40 * time.Millisecond)
if got := calls.Load(); got != 2 {
t.Fatalf("expected 2 callbacks, got %d", got)
}
}
|