aboutsummaryrefslogtreecommitdiff
path: root/src/time
diff options
context:
space:
mode:
authorzhouguangyuan <zhouguangyuan.xian@gmail.com>2021-11-03 22:23:29 +0800
committerEmmanuel Odeke <emmanuel@orijtech.com>2021-11-05 21:13:38 +0000
commit091948a55fb198be4202c21a5809ec68d77f70c4 (patch)
treef7106384233d6920ac180fa6756cb4a9f629b8e3 /src/time
parentdbd3cf884986c88f5b3350709c0f51fa02330805 (diff)
downloadgo-091948a55fb198be4202c21a5809ec68d77f70c4.tar.gz
go-091948a55fb198be4202c21a5809ec68d77f70c4.zip
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>
Diffstat (limited to 'src/time')
-rw-r--r--src/time/tick.go6
-rw-r--r--src/time/tick_test.go11
2 files changed, 16 insertions, 1 deletions
diff --git a/src/time/tick.go b/src/time/tick.go
index f9522b0b75..babf865aeb 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 d8cd59228f..f539091869 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)