1
0
mirror of https://github.com/golang/go synced 2024-11-24 05:40:15 -07:00

time: make Ticker.Reset(0) panic

Fixes #49315

Change-Id: I0887bad1059b25ae0749bfa1ed6ddccbecca7951
Reviewed-on: https://go-review.googlesource.com/c/go/+/361074
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
This commit is contained in:
zhouguangyuan 2021-11-03 22:23:29 +08:00 committed by Emmanuel Odeke
parent dbd3cf8849
commit 091948a55f
2 changed files with 16 additions and 1 deletions

View File

@ -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")
}

View File

@ -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)