mirror of
https://github.com/hackdiaz-dev/free-turn-proxy.git
synced 2026-10-12 09:41:53 +02:00
38 lines
771 B
Go
38 lines
771 B
Go
package wake
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestWatchStopsOnContext(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
done := make(chan struct{})
|
|
go func() {
|
|
Watch(ctx, time.Millisecond, time.Hour, nil)
|
|
close(done)
|
|
}()
|
|
|
|
cancel()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("Watch did not return after context cancel")
|
|
}
|
|
}
|
|
|
|
func TestWatchIgnoresGapsBelowThreshold(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
|
|
defer cancel()
|
|
|
|
fired := make(chan time.Duration, 1)
|
|
Watch(ctx, time.Millisecond, time.Hour, func(gap time.Duration) { fired <- gap })
|
|
|
|
select {
|
|
case gap := <-fired:
|
|
t.Fatalf("onGap fired without a sleep gap: %s", gap)
|
|
default:
|
|
}
|
|
}
|