Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: when delay is less than tick, Ticker runs only once. #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (tw *TimeWheel) AddCron(delay time.Duration, callback func()) *Task {
}

func (tw *TimeWheel) addAny(delay time.Duration, callback func(), circle, async bool) *Task {
if delay <= 0 {
if delay < tw.tick {
delay = tw.tick
}

Expand Down
26 changes: 26 additions & 0 deletions timer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,32 @@ func TestTickerSecond(t *testing.T) {
assert.Greater(t, incr, 100)
}

func TestTickerSecondLess(t *testing.T) {
tw, err := NewTimeWheel(10*time.Millisecond, 10000)
assert.Nil(t, err)

tw.Start()
defer tw.Stop()

var (
timeout = time.After(1100 * time.Millisecond)
ticker = tw.NewTicker(9 * time.Millisecond)
incr int
)

for run := true; run; {
select {
case <-timeout:
run = false

case <-ticker.C:
incr++
}
}

assert.Greater(t, incr, 100)
}

func TestBatchTicker(t *testing.T) {
tw, err := NewTimeWheel(100*time.Millisecond, 60)
assert.Nil(t, err)
Expand Down