forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer_test.go
95 lines (81 loc) · 2.21 KB
/
timer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package utils
import (
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Timer", func() {
const d = 10 * time.Millisecond
It("doesn't fire a newly created timer", func() {
t := NewTimer()
Consistently(t.Chan()).ShouldNot(Receive())
})
It("works", func() {
t := NewTimer()
t.Reset(time.Now().Add(d))
Eventually(t.Chan()).Should(Receive())
})
It("returns the deadline", func() {
t := NewTimer()
deadline := time.Now().Add(d)
t.Reset(deadline)
Expect(t.Deadline()).To(Equal(deadline))
Eventually(t.Chan()).Should(Receive())
})
It("works multiple times with reading", func() {
t := NewTimer()
for i := 0; i < 10; i++ {
t.Reset(time.Now().Add(d))
Eventually(t.Chan()).Should(Receive())
t.SetRead()
}
})
It("works multiple times without reading", func() {
t := NewTimer()
for i := 0; i < 10; i++ {
t.Reset(time.Now().Add(d))
time.Sleep(d * 2)
}
Eventually(t.Chan()).Should(Receive())
})
It("works when resetting without expiration", func() {
t := NewTimer()
for i := 0; i < 10; i++ {
t.Reset(time.Now().Add(time.Hour))
}
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("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()
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())
})
It("stops", func() {
t := NewTimer()
t.Reset(time.Now().Add(50 * time.Millisecond))
t.Stop()
Consistently(t.Chan()).ShouldNot(Receive())
})
})