Skip to content
This repository was archived by the owner on May 18, 2023. It is now read-only.

Commit 307483a

Browse files
authored
Merge pull request #25 from mqnfred/master
Introduce ticker Reset
2 parents d30813f + 421f858 commit 307483a

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

clock.go

+7
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,13 @@ func (t *Ticker) Stop() {
317317
}
318318
}
319319

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+
320327
type internalTicker Ticker
321328

322329
func (t *internalTicker) Next() time.Time { return t.next }

clock_test.go

+18-12
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,33 @@ func TestClock_Ticker(t *testing.T) {
124124

125125
// Ensure that the clock's ticker can stop correctly.
126126
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) {
127139
var ok bool
128140
go func() {
129-
time.Sleep(10 * time.Millisecond)
141+
time.Sleep(30 * time.Millisecond)
130142
ok = true
131143
}()
132144
gosched()
133145

134146
ticker := New().Ticker(20 * time.Millisecond)
135147
<-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")
141152
}
153+
ticker.Stop()
142154
}
143155

144156
// Ensure that the clock's timer waits correctly.
@@ -167,12 +179,6 @@ func TestClock_Timer(t *testing.T) {
167179

168180
// Ensure that the clock's timer can be stopped.
169181
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-
176182
timer := New().Timer(20 * time.Millisecond)
177183
if !timer.Stop() {
178184
t.Fatal("timer not running")

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/benbjohnson/clock
22

3-
go 1.13
3+
go 1.15

0 commit comments

Comments
 (0)