// Stop prevents the Timer from firing.
// It returns true if the call stops the timer, false if the timer has already
-// expired or stopped.
+// expired or been stopped.
// Stop does not close the channel, to prevent a read from the channel succeeding
// incorrectly.
-func (t *Timer) Stop() (ok bool) {
+func (t *Timer) Stop() bool {
return stopTimer(&t.r)
}
return t
}
+// Reset changes the timer to expire after duration d.
+// 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 {
+ when := nano() + int64(d)
+ active := stopTimer(&t.r)
+ t.r.when = when
+ startTimer(&t.r)
+ return active
+}
+
func sendTime(now int64, c interface{}) {
// Non-blocking send of time on c.
// Used in NewTimer, it cannot block anyway (buffer).
}
<-c
}
+
+func TestReset(t *testing.T) {
+ t0 := NewTimer(100 * Millisecond)
+ Sleep(50 * Millisecond)
+ if t0.Reset(150*Millisecond) != true {
+ t.Fatalf("resetting unfired timer returned false")
+ }
+ Sleep(100 * Millisecond)
+ select {
+ case <-t0.C:
+ t.Fatalf("timer fired early")
+ default:
+ }
+ Sleep(100 * Millisecond)
+ select {
+ case <-t0.C:
+ default:
+ t.Fatalf("reset timer did not fire")
+ }
+
+ if t0.Reset(50*Millisecond) != false {
+ t.Fatalf("resetting expired timer returned true")
+ }
+}