forked from RussellLuo/timingwheel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimingwheel_test.go
42 lines (35 loc) · 884 Bytes
/
timingwheel_test.go
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
package timingwheel_test
import (
"testing"
"time"
"github.com/RussellLuo/timingwheel"
)
func TestTimingWheel_AfterFunc(t *testing.T) {
tw := timingwheel.NewTimingWheel(time.Millisecond, 20)
tw.Start()
defer tw.Stop()
durations := []time.Duration{
1 * time.Millisecond,
5 * time.Millisecond,
10 * time.Millisecond,
50 * time.Millisecond,
100 * time.Millisecond,
500 * time.Millisecond,
1 * time.Second,
}
for _, d := range durations {
t.Run("", func(t *testing.T) {
exitC := make(chan time.Time)
start := time.Now()
tw.AfterFunc(d, func() {
exitC <- time.Now()
})
got := (<-exitC).Truncate(time.Millisecond)
min := start.Add(d).Truncate(time.Millisecond)
err := 5 * time.Millisecond
if got.Before(min) || got.After(min.Add(err)) {
t.Errorf("NewTimer(%s) want [%s, %s], got %s", d, min, min.Add(err), got)
}
})
}
}