don't set a timer when the deadline is the zero value

This commit is contained in:
Marten Seemann 2018-12-16 21:04:39 +06:30
parent 072b84870c
commit 516b427d46
2 changed files with 9 additions and 1 deletions

View file

@ -34,7 +34,9 @@ func (t *Timer) Reset(deadline time.Time) {
if !t.t.Stop() && !t.read {
<-t.t.C
}
t.t.Reset(time.Until(deadline))
if !deadline.IsZero() {
t.t.Reset(time.Until(deadline))
}
t.read = false
t.deadline = deadline

View file

@ -54,6 +54,12 @@ var _ = Describe("Timer", func() {
Eventually(t.Chan()).Should(Receive())
})
It("doesn't set a timer if the deadline is the zero value", func() {
t := NewTimer()
t.Reset(time.Time{})
Consistently(t.Chan()).ShouldNot(Receive())
})
It("fires the timer twice, if reset to the same deadline", func() {
deadline := time.Now().Add(-time.Millisecond)
t := NewTimer()