improve package gtmer

This commit is contained in:
jflyfox
2021-01-19 11:27:23 +08:00
parent 353286fd84
commit bf6d6a29c0
5 changed files with 13 additions and 11 deletions

View File

@ -45,14 +45,14 @@ func (w *wheel) start() {
// according to its leftover interval in milliseconds.
func (w *wheel) proceed() {
var (
n = w.ticks.Add(1)
l = w.slots[int(n%w.number)]
length = l.Len()
nowMs = w.timer.nowFunc().UnixNano() / 1e6
nowTicks = w.ticks.Add(1)
list = w.slots[int(nowTicks%w.number)]
length = list.Len()
nowMs = w.timer.nowFunc().UnixNano() / 1e6
)
if length > 0 {
go func(l *glist.List, nowTicks int64) {
entry := (*Entry)(nil)
var entry *Entry
for i := length; i > 0; i-- {
if v := l.PopFront(); v == nil {
break
@ -88,6 +88,6 @@ func (w *wheel) proceed() {
entry.wheel.timer.doAddEntryByParent(!runnable, nowMs, entry.installIntervalMs, entry)
}
}
}(l, n)
}(list, nowTicks)
}
}

View File

@ -7,15 +7,20 @@
package gtimer
import (
"github.com/gogf/gf/container/gtype"
"github.com/gogf/gf/test/gtest"
"testing"
"time"
)
func TestTimer(t *testing.T) {
func TestTimer_Proceed(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timer := doNewWithoutAutoStart(10, 60*time.Millisecond, 6)
index := gtype.NewInt()
slice := make([]int, 0)
timer := doNewWithoutAutoStart(10, 60*time.Millisecond, 6)
timer.nowFunc = func() time.Time {
return time.Now().Add(time.Duration(index.Add(1)) * time.Millisecond * 60)
}
timer.AddOnce(2*time.Second, func() {
slice = append(slice, 1)
})
@ -44,9 +49,6 @@ func TestTimer(t *testing.T) {
slice = append(slice, 9)
})
for i := 0; i < 2000000; i++ {
timer.nowFunc = func() time.Time {
return time.Now().Add(time.Duration(i) * time.Millisecond * 60)
}
timer.wheels[0].proceed()
time.Sleep(time.Microsecond)
}