Skip to content

Commit 524d5f7

Browse files
authored
fix (#13)
1 parent d604cc0 commit 524d5f7

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

min_heap_node.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ type minHeaps []minHeapNode
2929

3030
func (m minHeaps) Len() int { return len(m) }
3131
func (m minHeaps) Less(i, j int) bool { return m[i].absExpire.Before(m[j].absExpire) }
32-
func (m minHeaps) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
32+
func (m minHeaps) Swap(i, j int) {
33+
m[i], m[j] = m[j], m[i]
34+
m[i].index = i
35+
m[j].index = j
36+
}
3337

3438
func (m *minHeaps) Push(x any) {
3539
// Push and Pop use pointer receivers because they modify the slice's length,

min_heap_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ func (c *curstomTest) Next(now time.Time) (rv time.Time) {
196196
return
197197
}
198198

199+
// 验证自定义函数的运行间隔时间
199200
func Test_CustomFunc(t *testing.T) {
200201
t.Run("custom", func(t *testing.T) {
201202

@@ -233,3 +234,26 @@ func Test_CustomFunc(t *testing.T) {
233234
assert.Equal(t, mh.runCount, uint32(3))
234235
})
235236
}
237+
238+
// 验证运行次数是符合预期的
239+
func Test_RunCount(t *testing.T) {
240+
t.Run("runcount-10ms", func(t *testing.T) {
241+
tm := NewTimer(WithMinHeap())
242+
max := 10
243+
go func() {
244+
tm.Run()
245+
}()
246+
247+
count := uint32(0)
248+
for i := 0; i < max; i++ {
249+
250+
tm.ScheduleFunc(time.Millisecond*10, func() {
251+
atomic.AddUint32(&count, 1)
252+
})
253+
}
254+
255+
time.Sleep(time.Millisecond * 15)
256+
tm.Stop()
257+
assert.Equal(t, count, uint32(max))
258+
})
259+
}

0 commit comments

Comments
 (0)