-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock_test.go
71 lines (60 loc) · 1.59 KB
/
clock_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
package clock_test
import (
"time"
. "github.com/bangzek/clock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Clock", func() {
var c Clock
BeforeEach(func() {
c = New()
})
Describe("Now", func() {
It("return increasing time.Time", func() {
t1 := c.Now()
time.Sleep(ms)
t2 := c.Now()
Expect(t2.After(t1)).To(BeTrue())
})
})
Describe("NewTimer", func() {
It("fired by the timer", func() {
t := c.NewTimer(200 * ms)
Consistently(t.C, 80*ms, 10*ms).ShouldNot(Receive())
d := 20 * ms
Expect(t.Reset(d)).To(BeTrue(), "1st reset")
t1 := time.Now()
ct := <-t.C
t2 := time.Now()
Expect(ct).To(BeTemporally("~", t2, th), "ct")
Expect(t2).To(BeTemporally("~", t1.Add(d), th), "t2 - t1")
Expect(t.Reset(50 * ms)).To(BeFalse())
Expect(t.Stop()).To(BeTrue(), "stop")
Consistently(t.C).ShouldNot(Receive())
})
})
Describe("NewTicker", func() {
It("tick in regular order", func() {
d1 := 100 * ms
d2 := 50 * ms
t := c.NewTicker(d1)
ct1 := <-t.C
t1 := time.Now()
ct2 := <-t.C
t2 := time.Now()
t.Reset(d2)
ct3 := <-t.C
t3 := time.Now()
t.Stop()
Consistently(t.C).ShouldNot(Receive())
Expect(ct1).To(BeTemporally("~", t1, th), "ct1")
Expect(ct2).To(BeTemporally("~", t2, th), "ct2")
Expect(ct3).To(BeTemporally("~", t3, th), "ct3")
Expect(ct1.Before(ct2)).To(BeTrue(), "ct1 < ct2")
Expect(ct2).To(BeTemporally("~", ct1.Add(d1), th), "ct2 - ct1")
Expect(ct2.Before(ct3)).To(BeTrue(), "ct2 < ct3")
Expect(ct3).To(BeTemporally("~", ct2.Add(d2), th), "ct3 - ct2")
})
})
})