Repository navigation
Expand file tree
/
Copy pathtimer_test.go
More file actions
142 lines (121 loc) · 2.6 KB
/
Copy pathtimer_test.go
File metadata and controls
142 lines (121 loc) · 2.6 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package timer_test
import (
"fmt"
"testing"
"time"
"math/rand"
"log"
"runtime/pprof"
"os"
"github.com/xiaonanln/goTimer"
)
func init() {
timer.StartTicks(time.Millisecond)
}
func TestCallback(t *testing.T) {
INTERVAL := 100 * time.Millisecond
for i := 0; i < 10; i++ {
x := false
timer.AddCallback(INTERVAL, func() {
fmt.Println("callback!")
x = true
})
time.Sleep(INTERVAL * 2)
if !x {
t.Fatalf("x should be true, but it's false")
}
}
}
func TestTimer(t *testing.T) {
INTERVAL := 100 * time.Millisecond
x := 0
px := x
now := time.Now()
nextTime := now.Add(INTERVAL)
fmt.Printf("now is %s, next time should be %s\n", time.Now(), nextTime)
timer.AddTimer(INTERVAL, func() {
x += 1
fmt.Printf("timer %s x %v px %v\n", time.Now(), x, px)
})
//time.Sleep(time.Second)
for i := 0; i < 10; i++ {
time.Sleep(nextTime.Add(INTERVAL / 2).Sub(time.Now()))
fmt.Printf("Check x %v px %v @ %s\n", x, px, time.Now())
if x != px+1 {
t.Fatalf("x should be %d, but it's %d", px+1, x)
}
px = x
nextTime = nextTime.Add(INTERVAL)
fmt.Printf("now is %s, next time should be %s\n", time.Now(), nextTime)
}
}
func TestCallbackSeq(t *testing.T) {
a := 0
d := time.Second
for i := 0; i < 100; i++ {
i := i
timer.AddCallback(d, func() {
if a != i {
t.Error(i, a)
}
a += 1
})
}
time.Sleep(d + time.Second*1)
}
func TestCancelCallback(t *testing.T) {
INTERVAL := 20 * time.Millisecond
x := 0
timer := timer.AddCallback(INTERVAL, func() {
x = 1
})
if !timer.IsActive() {
t.Fatalf("timer should be active")
}
timer.Cancel()
if timer.IsActive() {
t.Fatalf("timer should be inactive")
}
time.Sleep(INTERVAL * 2)
if x != 0 {
t.Fatalf("x should be 0, but is %v", x)
}
}
func TestCancelTimer(t *testing.T) {
INTERVAL := 20 * time.Millisecond
x := 0
timer := timer.AddTimer(INTERVAL, func() {
x += 1
})
if !timer.IsActive() {
t.Fatalf("timer should be active")
}
timer.Cancel()
if timer.IsActive() {
t.Fatalf("timer should be inactive")
}
time.Sleep(INTERVAL * 2)
if x != 0 {
t.Fatalf("x should be 0, but is %v", x)
}
}
func TestTimerPerformance(t *testing.T) {
f, err := os.Create("TestTimerPerformance.cpuprof")
if err != nil {
panic(err)
}
pprof.StartCPUProfile(f)
duration := 10 * time.Second
for i := 0; i < 400000; i++ {
if rand.Float32() < 0.5 {
d := time.Duration(rand.Int63n(int64(duration)))
timer.AddCallback(d, func() {})
} else {
d := time.Duration(rand.Int63n(int64(time.Second)))
timer.AddTimer(d, func() {})
}
}
log.Println("Waiting for", duration, "...")
time.Sleep(duration)
pprof.StopCPUProfile()
}