diff --git a/timer.go b/timer.go index d60498d..54b4336 100644 --- a/timer.go +++ b/timer.go @@ -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 } diff --git a/timer_test.go b/timer_test.go index a5c9a83..3fdee8c 100644 --- a/timer_test.go +++ b/timer_test.go @@ -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)