This repository was archived by the owner on May 18, 2023. It is now read-only.
File tree 3 files changed +26
-13
lines changed
3 files changed +26
-13
lines changed Original file line number Diff line number Diff line change @@ -317,6 +317,13 @@ func (t *Ticker) Stop() {
317
317
}
318
318
}
319
319
320
+ // Reset resets the ticker to a new duration.
321
+ func (t * Ticker ) Reset (dur time.Duration ) {
322
+ if t .ticker != nil {
323
+ t .ticker .Reset (dur )
324
+ }
325
+ }
326
+
320
327
type internalTicker Ticker
321
328
322
329
func (t * internalTicker ) Next () time.Time { return t .next }
Original file line number Diff line number Diff line change @@ -124,21 +124,33 @@ func TestClock_Ticker(t *testing.T) {
124
124
125
125
// Ensure that the clock's ticker can stop correctly.
126
126
func TestClock_Ticker_Stp (t * testing.T ) {
127
+ ticker := New ().Ticker (20 * time .Millisecond )
128
+ <- ticker .C
129
+ ticker .Stop ()
130
+ select {
131
+ case <- ticker .C :
132
+ t .Fatal ("unexpected send" )
133
+ case <- time .After (30 * time .Millisecond ):
134
+ }
135
+ }
136
+
137
+ // Ensure that the clock's ticker can reset correctly.
138
+ func TestClock_Ticker_Rst (t * testing.T ) {
127
139
var ok bool
128
140
go func () {
129
- time .Sleep (10 * time .Millisecond )
141
+ time .Sleep (30 * time .Millisecond )
130
142
ok = true
131
143
}()
132
144
gosched ()
133
145
134
146
ticker := New ().Ticker (20 * time .Millisecond )
135
147
<- ticker .C
136
- ticker .Stop ()
137
- select {
138
- case <- ticker .C :
139
- t .Fatal ("unexpected send" )
140
- case <- time .After (30 * time .Millisecond ):
148
+ ticker .Reset (5 * time .Millisecond )
149
+ <- ticker .C
150
+ if ok {
151
+ t .Fatal ("too late" )
141
152
}
153
+ ticker .Stop ()
142
154
}
143
155
144
156
// Ensure that the clock's timer waits correctly.
@@ -167,12 +179,6 @@ func TestClock_Timer(t *testing.T) {
167
179
168
180
// Ensure that the clock's timer can be stopped.
169
181
func TestClock_Timer_Stop (t * testing.T ) {
170
- var ok bool
171
- go func () {
172
- time .Sleep (10 * time .Millisecond )
173
- ok = true
174
- }()
175
-
176
182
timer := New ().Timer (20 * time .Millisecond )
177
183
if ! timer .Stop () {
178
184
t .Fatal ("timer not running" )
Original file line number Diff line number Diff line change 1
1
module github.com/benbjohnson/clock
2
2
3
- go 1.13
3
+ go 1.15
You can’t perform that action at this time.
0 commit comments