diff --git a/src/time/tick.go b/src/time/tick.go index f9522b0b754..babf865aeb0 100644 --- a/src/time/tick.go +++ b/src/time/tick.go @@ -48,8 +48,12 @@ func (t *Ticker) Stop() { } // Reset stops a ticker and resets its period to the specified duration. -// The next tick will arrive after the new period elapses. +// The next tick will arrive after the new period elapses. The duration d +// must be greater than zero; if not, Reset will panic. func (t *Ticker) Reset(d Duration) { + if d <= 0 { + panic("non-positive interval for Ticker.Reset") + } if t.r.f == nil { panic("time: Reset called on uninitialized Ticker") } diff --git a/src/time/tick_test.go b/src/time/tick_test.go index d8cd59228f0..f539091869f 100644 --- a/src/time/tick_test.go +++ b/src/time/tick_test.go @@ -134,6 +134,17 @@ func TestNewTickerLtZeroDuration(t *testing.T) { NewTicker(-1) } +// Test that Ticker.Reset panics when given a duration less than zero. +func TestTickerResetLtZeroDuration(t *testing.T) { + defer func() { + if err := recover(); err == nil { + t.Errorf("Ticker.Reset(0) should have panicked") + } + }() + tk := NewTicker(Second) + tk.Reset(0) +} + func BenchmarkTicker(b *testing.B) { benchmark(b, func(n int) { ticker := NewTicker(Nanosecond)