diff options
author | Brad Fitzpatrick <bradfitz@golang.org> | 2014-10-21 13:26:40 +0200 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2014-10-21 13:26:40 +0200 |
commit | 67a569fc4cdae5efbddc5ebe5365fc7457ee38a5 (patch) | |
tree | 0c089958512c4dee1e7364e3fa5a8e3a8b190c76 /src | |
parent | 2043f3b47093bc0258c14f13200a29042cf137e9 (diff) | |
download | go-67a569fc4cdae5efbddc5ebe5365fc7457ee38a5.tar.gz |
time: panic with a more helpful error on use of invalid Timer
Fixes Issue 8721
LGTM=rsc
R=r, rsc
CC=golang-codereviews
https://codereview.appspot.com/155620045
Diffstat (limited to 'src')
-rw-r--r-- | src/time/sleep.go | 6 | ||||
-rw-r--r-- | src/time/sleep_test.go | 21 |
2 files changed, 27 insertions, 0 deletions
diff --git a/src/time/sleep.go b/src/time/sleep.go index 61660d14f..e7a2ee205 100644 --- a/src/time/sleep.go +++ b/src/time/sleep.go @@ -55,6 +55,9 @@ type Timer struct { // Stop does not close the channel, to prevent a read from the channel succeeding // incorrectly. func (t *Timer) Stop() bool { + if t.r.f == nil { + panic("time: Stop called on uninitialized Timer") + } return stopTimer(&t.r) } @@ -78,6 +81,9 @@ func NewTimer(d Duration) *Timer { // It returns true if the timer had been active, false if the timer had // expired or been stopped. func (t *Timer) Reset(d Duration) bool { + if t.r.f == nil { + panic("time: Reset called on uninitialized Timer") + } w := when(d) active := stopTimer(&t.r) t.r.when = w diff --git a/src/time/sleep_test.go b/src/time/sleep_test.go index 2cfb6a59c..c9b2956b7 100644 --- a/src/time/sleep_test.go +++ b/src/time/sleep_test.go @@ -9,6 +9,7 @@ import ( "fmt" "runtime" "sort" + "strings" "sync" "sync/atomic" "testing" @@ -407,3 +408,23 @@ func TestOverflowRuntimeTimer(t *testing.T) { // the end of CheckRuntimeTimerOverflow in internal_test.go. CheckRuntimeTimerOverflow() } + +func checkZeroPanicString(t *testing.T) { + e := recover() + s, _ := e.(string) + if want := "called on uninitialized Timer"; !strings.Contains(s, want) { + t.Errorf("panic = %v; want substring %q", e, want) + } +} + +func TestZeroTimerResetPanics(t *testing.T) { + defer checkZeroPanicString(t) + var tr Timer + tr.Reset(1) +} + +func TestZeroTimerStopPanics(t *testing.T) { + defer checkZeroPanicString(t) + var tr Timer + tr.Stop() +} |