mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-06 01:32:18 +00:00
28 lines
628 B
Go
28 lines
628 B
Go
|
package asyncutil
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"testing"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func TestDebounce(t *testing.T) {
|
||
|
eventsChan := make(chan interface{}, 100)
|
||
|
ctx, cancel := context.WithCancel(context.Background())
|
||
|
interval := time.Second
|
||
|
timesHandled := 0
|
||
|
go Debounce(ctx, interval, eventsChan, func(event interface{}) {
|
||
|
timesHandled++
|
||
|
})
|
||
|
for i := 0; i < 100; i++ {
|
||
|
eventsChan <- struct{}{}
|
||
|
}
|
||
|
time.Sleep(interval)
|
||
|
cancel()
|
||
|
// We should expect 100 rapid fire changes to only have caused
|
||
|
// 1 handler to trigger after the debouncing period.
|
||
|
if timesHandled != 1 {
|
||
|
t.Errorf("Expected 1 handler call, received %d", timesHandled)
|
||
|
}
|
||
|
}
|