From c3cc35363b39e9a06ac2ae1496be8240d846e77a Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sun, 21 Jan 2018 14:09:45 +1100 Subject: [PATCH] fire the timer twice, if reset to the same deadline --- internal/utils/timer.go | 2 +- internal/utils/timer_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/internal/utils/timer.go b/internal/utils/timer.go index 695ad3e75..7f8ffc7a0 100644 --- a/internal/utils/timer.go +++ b/internal/utils/timer.go @@ -21,7 +21,7 @@ func (t *Timer) Chan() <-chan time.Time { // Reset the timer, no matter whether the value was read or not func (t *Timer) Reset(deadline time.Time) { - if deadline.Equal(t.deadline) { + if deadline.Equal(t.deadline) && !t.read { // No need to reset the timer return } diff --git a/internal/utils/timer_test.go b/internal/utils/timer_test.go index 8aa0731cb..c1581919d 100644 --- a/internal/utils/timer_test.go +++ b/internal/utils/timer_test.go @@ -42,4 +42,28 @@ var _ = Describe("Timer", func() { t.Reset(time.Now().Add(d)) Eventually(t.Chan()).Should(Receive()) }) + + It("immediately fires the timer, if the deadlines has already passed", func() { + t := NewTimer() + t.Reset(time.Now().Add(-time.Second)) + Eventually(t.Chan()).Should(Receive()) + }) + + It("fires the timer twice, if reset to the same deadline", func() { + deadline := time.Now().Add(-time.Millisecond) + t := NewTimer() + t.Reset(deadline) + Eventually(t.Chan()).Should(Receive()) + t.SetRead() + t.Reset(deadline) + Eventually(t.Chan()).Should(Receive()) + }) + + It("only fires the timer once, if it is reset to the same deadline, but not read in between", func() { + deadline := time.Now().Add(-time.Millisecond) + t := NewTimer() + t.Reset(deadline) + Eventually(t.Chan()).Should(Receive()) + Consistently(t.Chan()).ShouldNot(Receive()) + }) })