-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimingwheel_test.go
61 lines (51 loc) · 1.22 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package timer
import (
"log"
"testing"
"time"
)
func TestTimingWheel(t *testing.T) {
tw, err := NewTimingWheel(1 * time.Millisecond)
if err != nil {
t.Fatal(err)
}
go tw.Run()
time.Sleep(100 * time.Millisecond)
start := time.Now()
for i := 15; i < 20; i++ {
delay := time.Duration(i) * time.Second
tw.AddFunc(delay, func() {
log.Printf("job func triggerd, time diff %dms", time.Now().Sub(start)/1e6)
})
log.Printf("%d added", i)
}
time.Sleep(20 * time.Second)
for i := 5; i < 10; i++ {
delay := time.Duration(i) * time.Second
tw.AddFunc(delay, func() {
log.Printf("job func triggerd, time diff %dms", time.Now().Sub(start)/1e6)
})
log.Printf("%d added", i)
}
time.Sleep(7 * time.Second)
tw.Close()
}
func TestTimingWheel2(t *testing.T) {
tw, err := NewTimingWheel(1 * time.Millisecond)
if err != nil {
log.Fatal(err)
}
go tw.Run()
for i := 0; i < 10; i++ {
delay := time.Duration(i+1) * time.Second
tw.AddFunc(delay,
func(createTime time.Time, i int, delay time.Duration) func() {
return func() {
log.Printf("job %d with delay %v triggerd, time diff: %v", i, delay, time.Now().Sub(createTime))
}
}(time.Now(), i, delay),
)
}
time.Sleep(15 * time.Second)
tw.Close()
}