mirror of
https://github.com/golang/go
synced 2024-11-24 03:20:02 -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:
parent
dbd3cf8849
commit
091948a55f
@ -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")
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user