Commit 44ff17e 1 parent 55740f7 commit 44ff17e Copy full SHA for 44ff17e
File tree 2 files changed +37
-2
lines changed
2 files changed +37
-2
lines changed Original file line number Diff line number Diff line change @@ -35,10 +35,10 @@ type Timer struct {
35
35
36
36
// Stop prevents the Timer from firing.
37
37
// It returns true if the call stops the timer, false if the timer has already
38
- // expired or stopped.
38
+ // expired or been stopped.
39
39
// Stop does not close the channel, to prevent a read from the channel succeeding
40
40
// incorrectly.
41
- func (t * Timer ) Stop () ( ok bool ) {
41
+ func (t * Timer ) Stop () bool {
42
42
return stopTimer (& t .r )
43
43
}
44
44
@@ -58,6 +58,17 @@ func NewTimer(d Duration) *Timer {
58
58
return t
59
59
}
60
60
61
+ // Reset changes the timer to expire after duration d.
62
+ // It returns true if the timer had been active, false if the timer had
63
+ // expired or been stopped.
64
+ func (t * Timer ) Reset (d Duration ) bool {
65
+ when := nano () + int64 (d )
66
+ active := stopTimer (& t .r )
67
+ t .r .when = when
68
+ startTimer (& t .r )
69
+ return active
70
+ }
71
+
61
72
func sendTime (now int64 , c interface {}) {
62
73
// Non-blocking send of time on c.
63
74
// Used in NewTimer, it cannot block anyway (buffer).
Original file line number Diff line number Diff line change @@ -245,3 +245,27 @@ func TestSleepZeroDeadlock(t *testing.T) {
245
245
}
246
246
<- c
247
247
}
248
+
249
+ func TestReset (t * testing.T ) {
250
+ t0 := NewTimer (100 * Millisecond )
251
+ Sleep (50 * Millisecond )
252
+ if t0 .Reset (150 * Millisecond ) != true {
253
+ t .Fatalf ("resetting unfired timer returned false" )
254
+ }
255
+ Sleep (100 * Millisecond )
256
+ select {
257
+ case <- t0 .C :
258
+ t .Fatalf ("timer fired early" )
259
+ default :
260
+ }
261
+ Sleep (100 * Millisecond )
262
+ select {
263
+ case <- t0 .C :
264
+ default :
265
+ t .Fatalf ("reset timer did not fire" )
266
+ }
267
+
268
+ if t0 .Reset (50 * Millisecond ) != false {
269
+ t .Fatalf ("resetting expired timer returned true" )
270
+ }
271
+ }
You can’t perform that action at this time.
0 commit comments