forked from quic-go/quic-go
fire the timer twice, if reset to the same deadline
This commit is contained in:
@@ -21,7 +21,7 @@ func (t *Timer) Chan() <-chan time.Time {
|
|||||||
|
|
||||||
// Reset the timer, no matter whether the value was read or not
|
// Reset the timer, no matter whether the value was read or not
|
||||||
func (t *Timer) Reset(deadline time.Time) {
|
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
|
// No need to reset the timer
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,4 +42,28 @@ var _ = Describe("Timer", func() {
|
|||||||
t.Reset(time.Now().Add(d))
|
t.Reset(time.Now().Add(d))
|
||||||
Eventually(t.Chan()).Should(Receive())
|
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())
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user