-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer_test.go
More file actions
executable file
·85 lines (76 loc) · 1.59 KB
/
timer_test.go
File metadata and controls
executable file
·85 lines (76 loc) · 1.59 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package jtimer
import (
"container/heap"
"fmt"
"math/rand"
"testing"
"time"
)
func verify(t *testing.T, th timerHeap) {
for index, val := range th {
if index != val.index {
t.Errorf("verify error index != time.index index:[%v], timer:%+v", index, *val)
return
}
}
}
func TestHeapIndex(t *testing.T) {
h := make(timerHeap, 0)
heap.Init(&h)
for i := 0; i < 10000; i++ {
v := rand.Int63n(10000)
heap.Push(&h, &timer{endAt: time.Unix(0, v)})
verify(t, h)
}
for i := 0; i < 10000; i++ {
index := rand.Intn(h.Len())
heap.Remove(&h, index)
verify(t, h)
}
}
func TestTimer(t *testing.T) {
timerMgr := New()
newTimer := func() {
startAt := time.Now()
delayTime := time.Duration(rand.Int63n(10000)) * time.Millisecond
timerMgr.Add(
time.Now(),
startAt.Add(delayTime),
func(dt time.Duration) {
duration := time.Now().Sub(startAt)
if duration/time.Millisecond != delayTime/time.Millisecond {
t.Errorf("timer error delayTime:%v, duration:%v", delayTime, duration)
} else {
println("OK")
}
},
1,
)
}
go func() {
for {
newTimer()
time.Sleep(time.Millisecond * 100)
}
}()
go func() {
tick := time.NewTicker(time.Millisecond)
for range tick.C {
timerMgr.Update(time.Now())
}
}()
time.Sleep(time.Hour)
}
func TestDelete(t *testing.T) {
timerMgr := New()
timerMgr.Add(time.Now(), time.Now().Add(1*time.Second), func(dt time.Duration) {
fmt.Println("1 * second")
}, -1, "abc")
go func() {
tick := time.NewTicker(time.Millisecond)
for range tick.C {
timerMgr.Update(time.Now())
}
}()
time.Sleep(time.Hour)
}