Skip to content

Commit

Permalink
Merge pull request #70 from jonboulle/panic
Browse files Browse the repository at this point in the history
Make NewTicker panic if d<=0, like time.NewTicker. This more closely follows the standard library function behavior.

It is directly copied from the standard library: https://cs.opensource.google/go/go/+/refs/tags/go1.20.3:src/time/tick.go;l=23-25

Although panicking is generally bad, it is preferable to mimic the standard library so users see the same behavior between clockwork and the golang standard library.

Without this, some tests can pass with a no op where a real time.Ticker implementation would crash.
  • Loading branch information
DPJacques authored Jul 16, 2023
2 parents 8a29bc1 + b054768 commit 44a3d47
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions clockwork.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Package clockwork contains a simple fake clock for Go.
package clockwork

import (
"context"
"errors"
"sort"
"sync"
"time"
Expand Down Expand Up @@ -141,7 +143,14 @@ func (fc *fakeClock) Since(t time.Time) time.Duration {

// NewTicker returns a Ticker that will expire only after calls to
// fakeClock.Advance() have moved the clock past the given duration.
//
// The duration d must be greater than zero; if not, NewTicker will panic.
func (fc *fakeClock) NewTicker(d time.Duration) Ticker {
// Maintain parity with
// https://cs.opensource.google/go/go/+/refs/tags/go1.20.3:src/time/tick.go;l=23-25
if d <= 0 {
panic(errors.New("non-positive interval for NewTicker"))
}
var ft *fakeTicker
ft = &fakeTicker{
firer: newFirer(),
Expand Down Expand Up @@ -294,9 +303,10 @@ func (fc *fakeClock) set(e expirer, d time.Duration) {
// The caller must hold fc.l.
func (fc *fakeClock) setExpirer(e expirer, d time.Duration) {
if d.Nanoseconds() <= 0 {
// special case - trigger immediately, never reset.
// Special case for timers with duration <= 0: trigger immediately, never
// reset.
//
// TODO: Explain what cases this covers.
// Tickers never get here, they panic if d is < 0.
e.expire(fc.time)
return
}
Expand All @@ -307,7 +317,7 @@ func (fc *fakeClock) setExpirer(e expirer, d time.Duration) {
return fc.waiters[i].expiry().Before(fc.waiters[j].expiry())
})

// Notify blockers of our new waiter.
// Notify blockers of our new waiter.
var blocked []*blocker
count := len(fc.waiters)
for _, b := range fc.blockers {
Expand Down

0 comments on commit 44a3d47

Please sign in to comment.