-
Notifications
You must be signed in to change notification settings - Fork 37
/
min_heap_test.go
329 lines (280 loc) · 6.91 KB
/
min_heap_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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright 2020-2024 guonaihong, antlabs. All rights reserved.
//
// mit license
package timer
import (
"log"
"sync"
"sync/atomic"
"testing"
"time"
)
// 测试AfterFunc有没有运行以及时间间隔可对
func Test_MinHeap_AfterFunc_Run(t *testing.T) {
t.Run("1ms", 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, func() {
mu.Lock()
atomic.AddInt32(&count, 1)
if atomic.LoadInt32(&count) <= 2 && !isClose {
tc <- time.Since(now)
}
mu.Unlock()
})
node2 := tm.AfterFunc(time.Millisecond, func() {
mu.Lock()
atomic.AddInt32(&count, 1)
if atomic.LoadInt32(&count) <= 2 && !isClose {
tc <- time.Since(now)
}
mu.Unlock()
})
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("10ms", func(t *testing.T) {
tm := NewTimer(WithMinHeap())
go tm.Run() // 运行事件循环
count := int32(0)
tc := make(chan time.Duration, 2)
var mu sync.Mutex
isClosed := false
now := time.Now()
node1 := tm.AfterFunc(time.Millisecond*10, func() {
now2 := time.Now()
mu.Lock()
atomic.AddInt32(&count, 1)
if atomic.LoadInt32(&count) <= 2 && !isClosed {
tc <- time.Since(now)
}
mu.Unlock()
log.Printf("node1.Lock:%v\n", time.Since(now2))
})
node2 := tm.AfterFunc(time.Millisecond*10, func() {
now2 := time.Now()
mu.Lock()
atomic.AddInt32(&count, 1)
if atomic.LoadInt32(&count) <= 2 && !isClosed {
tc <- time.Since(now)
}
mu.Unlock()
log.Printf("node2.Lock:%v\n", time.Since(now2))
})
time.Sleep(time.Millisecond * 24)
now3 := time.Now()
mu.Lock()
node1.Stop()
node2.Stop()
isClosed = true
close(tc)
mu.Unlock()
log.Printf("node1.Stop:%v\n", time.Since(now3))
cnt := 1
for tv := range tc {
left := time.Millisecond * 10 * time.Duration(cnt)
right := time.Duration(cnt) * 2 * 10 * time.Millisecond
if tv < left || tv > right {
t.Errorf("index(%d) (%v)tc < %v || tc > %v", cnt, tv, left, right)
}
// cnt++
}
if atomic.LoadInt32(&count) != 2 {
t.Errorf("count:%d != 2", atomic.LoadInt32(&count))
}
})
t.Run("90ms", func(t *testing.T) {
tm := NewTimer(WithMinHeap())
go tm.Run()
count := int32(0)
tm.AfterFunc(time.Millisecond*90, func() { atomic.AddInt32(&count, 1) })
tm.AfterFunc(time.Millisecond*90, func() { atomic.AddInt32(&count, 2) })
time.Sleep(time.Millisecond * 180)
if atomic.LoadInt32(&count) != 3 {
t.Errorf("count != 3")
}
})
}
// 测试Schedule 运行的周期可对
func Test_MinHeap_ScheduleFunc_Run(t *testing.T) {
t.Run("1ms", func(t *testing.T) {
tm := NewTimer(WithMinHeap())
go tm.Run()
count := int32(0)
_ = tm.ScheduleFunc(2*time.Millisecond, func() {
log.Printf("%v\n", time.Now())
atomic.AddInt32(&count, 1)
if atomic.LoadInt32(&count) == 2 {
tm.Stop()
}
})
time.Sleep(time.Millisecond * 5)
if atomic.LoadInt32(&count) != 2 {
t.Errorf("count:%d != 2", atomic.LoadInt32(&count))
}
})
t.Run("10ms", func(t *testing.T) {
tm := NewTimer(WithMinHeap())
go tm.Run()
count := int32(0)
tc := make(chan time.Duration, 2)
var mu sync.Mutex
isClosed := false
now := time.Now()
node := tm.ScheduleFunc(time.Millisecond*10, func() {
mu.Lock()
atomic.AddInt32(&count, 1)
if atomic.LoadInt32(&count) <= 2 && !isClosed {
tc <- time.Since(now)
}
mu.Unlock()
})
time.Sleep(time.Millisecond * 25)
mu.Lock()
close(tc)
isClosed = true
node.Stop()
mu.Unlock()
cnt := 1
for tv := range tc {
left := time.Millisecond * 10 * time.Duration(cnt)
right := time.Duration(cnt) * 2 * 10 * time.Millisecond
if tv < left || tv > right {
t.Errorf("index(%d) (%v)tc < %v || tc > %v", cnt, tv, left, right)
}
cnt++
}
if atomic.LoadInt32(&count) != 2 {
t.Errorf("count:%d != 2", atomic.LoadInt32(&count))
}
})
t.Run("30ms", func(t *testing.T) {
tm := NewTimer(WithMinHeap())
go tm.Run()
count := int32(0)
c := make(chan bool, 1)
node := tm.ScheduleFunc(time.Millisecond*30, func() {
atomic.AddInt32(&count, 1)
if atomic.LoadInt32(&count) == 2 {
c <- true
}
})
go func() {
<-c
node.Stop()
}()
time.Sleep(time.Millisecond * 70)
if atomic.LoadInt32(&count) != 2 {
t.Errorf("count:%d != 2", atomic.LoadInt32(&count))
}
})
}
// 测试Stop是否会等待正在运行的任务结束
func Test_Run_Stop(t *testing.T) {
t.Run("1ms", func(t *testing.T) {
tm := NewTimer(WithMinHeap())
count := uint32(0)
tm.AfterFunc(time.Millisecond, func() { atomic.AddUint32(&count, 1) })
tm.AfterFunc(time.Millisecond, func() { atomic.AddUint32(&count, 1) })
go func() {
time.Sleep(time.Millisecond * 4)
tm.Stop()
}()
tm.Run()
if atomic.LoadUint32(&count) != 2 {
t.Errorf("count != 2")
}
})
}
type curstomTest struct {
count int32
}
func (c *curstomTest) Next(now time.Time) (rv time.Time) {
rv = now.Add(time.Duration(c.count) * time.Millisecond * 10)
atomic.AddInt32(&c.count, 1)
return
}
// 验证自定义函数的运行间隔时间
func Test_CustomFunc(t *testing.T) {
t.Run("custom", func(t *testing.T) {
tm := NewTimer(WithMinHeap())
// mh := tm.(*minHeap) // 最小堆
tc := make(chan time.Duration, 2)
now := time.Now()
count := uint32(1)
stop := make(chan bool, 1)
// 自定义函数
node := tm.CustomFunc(&curstomTest{count: 1}, func() {
if atomic.LoadUint32(&count) == 2 {
return
}
// 计算运行次数
atomic.AddUint32(&count, 1)
tc <- time.Since(now)
// 关闭这个任务
close(stop)
})
go func() {
<-stop
node.Stop()
tm.Stop()
}()
tm.Run()
close(tc)
cnt := 1
for tv := range tc {
left := time.Millisecond * 10 * time.Duration(cnt)
right := time.Duration(cnt) * 2 * 10 * time.Millisecond
if tv < left || tv > right {
t.Errorf("index(%d) (%v)tc < %v || tc > %v", cnt, tv, left, right)
}
cnt++
}
if atomic.LoadUint32(&count) != 2 {
t.Errorf("count != 2")
}
// 正在运行的任务是比较短暂的,所以外部很难
// if mh.runCount != int32(1) {
// t.Errorf("mh.runCount:%d != 1", mh.runCount)
// }
})
}
// 验证运行次数是符合预期的
func Test_RunCount(t *testing.T) {
t.Run("runcount-10ms", func(t *testing.T) {
tm := NewTimer(WithMinHeap())
max := 10
go func() {
tm.Run()
}()
count := uint32(0)
for i := 0; i < max; i++ {
tm.ScheduleFunc(time.Millisecond*10, func() {
atomic.AddUint32(&count, 1)
})
}
time.Sleep(time.Millisecond * 15)
tm.Stop()
if count != uint32(max) {
t.Errorf("count:%d != %d", count, max)
}
})
}