-
Notifications
You must be signed in to change notification settings - Fork 37
/
timer_test.go
219 lines (181 loc) · 3.97 KB
/
timer_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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2020-2024 guonaihong, antlabs. All rights reserved.
//
// mit license
package timer
import (
"log"
"sync"
"sync/atomic"
"testing"
"time"
)
func Test_ScheduleFunc(t *testing.T) {
tm := NewTimer()
log.SetFlags(log.Ldate | log.Lmicroseconds)
count := uint32(0)
log.Printf("start\n")
tm.ScheduleFunc(time.Millisecond*100, func() {
log.Printf("schedule\n")
atomic.AddUint32(&count, 1)
})
go func() {
time.Sleep(570 * time.Millisecond)
log.Printf("stop\n")
tm.Stop()
}()
tm.Run()
if count != 5 {
t.Errorf("count:%d != 5\n", count)
}
}
func Test_AfterFunc(t *testing.T) {
tm := NewTimer()
go tm.Run()
log.Printf("start\n")
count := uint32(0)
tm.AfterFunc(time.Millisecond*20, func() {
log.Printf("after Millisecond * 20")
atomic.AddUint32(&count, 1)
})
tm.AfterFunc(time.Second, func() {
log.Printf("after second")
atomic.AddUint32(&count, 1)
})
/*
tm.AfterFunc(time.Minute, func() {
log.Printf("after Minute")
})
*/
/*
tm.AfterFunc(time.Hour, nil)
tm.AfterFunc(time.Hour*24, nil)
tm.AfterFunc(time.Hour*24*365, nil)
tm.AfterFunc(time.Hour*24*365*12, nil)
*/
time.Sleep(time.Second + time.Millisecond*100)
tm.Stop()
if count != 2 {
t.Errorf("count:%d != 2\n", count)
}
}
func Test_Node_Stop_1(t *testing.T) {
tm := NewTimer()
count := uint32(0)
node := tm.AfterFunc(time.Millisecond*10, func() {
atomic.AddUint32(&count, 1)
})
go func() {
time.Sleep(time.Millisecond * 30)
node.Stop()
tm.Stop()
}()
tm.Run()
if count != 1 {
t.Errorf("count:%d == 1\n", count)
}
}
func Test_Node_Stop(t *testing.T) {
tm := NewTimer()
count := uint32(0)
node := tm.AfterFunc(time.Millisecond*100, func() {
atomic.AddUint32(&count, 1)
})
node.Stop()
go func() {
time.Sleep(time.Millisecond * 200)
tm.Stop()
}()
tm.Run()
if count == 1 {
t.Errorf("count:%d == 1\n", count)
}
}
// 测试重置定时器
func Test_Reset(t *testing.T) {
t.Run("min heap reset", func(t *testing.T) {
tm := NewTimer(WithMinHeap())
go tm.Run()
count := int32(0)
tc := make(chan time.Duration, 2)
var mu sync.Mutex
isClose := false
now := time.Now()
node1 := tm.AfterFunc(time.Millisecond*100, func() {
mu.Lock()
atomic.AddInt32(&count, 1)
if atomic.LoadInt32(&count) <= 2 && !isClose {
tc <- time.Since(now)
}
mu.Unlock()
})
node2 := tm.AfterFunc(time.Millisecond*100, func() {
mu.Lock()
atomic.AddInt32(&count, 1)
if atomic.LoadInt32(&count) <= 2 && !isClose {
tc <- time.Since(now)
}
mu.Unlock()
})
node1.Reset(time.Millisecond)
node2.Reset(time.Millisecond)
time.Sleep(time.Millisecond * 3)
mu.Lock()
isClose = true
close(tc)
node1.Stop()
node2.Stop()
mu.Unlock()
for tv := range tc {
if tv < time.Millisecond || tv > 2*time.Millisecond {
t.Errorf("tc < time.Millisecond tc > 2*time.Millisecond")
}
}
if atomic.LoadInt32(&count) != 2 {
t.Errorf("count:%d != 2", atomic.LoadInt32(&count))
}
})
t.Run("time wheel reset", func(t *testing.T) {
tm := NewTimer()
go func() {
tm.Run()
}()
count := int32(0)
tc := make(chan time.Duration, 2)
var mu sync.Mutex
isClose := false
now := time.Now()
node1 := tm.AfterFunc(time.Millisecond*10, func() {
mu.Lock()
atomic.AddInt32(&count, 1)
if atomic.LoadInt32(&count) <= 2 && !isClose {
tc <- time.Since(now)
}
mu.Unlock()
})
node2 := tm.AfterFunc(time.Millisecond*10, func() {
mu.Lock()
atomic.AddInt32(&count, 1)
if atomic.LoadInt32(&count) <= 2 && !isClose {
tc <- time.Since(now)
}
mu.Unlock()
})
node1.Reset(time.Millisecond * 20)
node2.Reset(time.Millisecond * 20)
time.Sleep(time.Millisecond * 40)
mu.Lock()
isClose = true
close(tc)
node1.Stop()
node2.Stop()
mu.Unlock()
for tv := range tc {
if tv < time.Millisecond*20 || tv > 2*time.Millisecond*20 {
t.Errorf("tc < time.Millisecond tc > 2*time.Millisecond")
}
}
if atomic.LoadInt32(&count) != 2 {
t.Errorf("count:%d != 2", atomic.LoadInt32(&count))
}
})
}