-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinterval_test.go
73 lines (56 loc) · 1.65 KB
/
interval_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
package raft_test
import (
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/bbengfort/raft"
)
var _ = Describe("Interval", func() {
var actor Actor
var events []Event
var interval Interval
BeforeEach(func() {
events = make([]Event, 0)
actor = NewLocker(func(e Event) error {
events = append(events, e)
return nil
})
})
Describe("FixedInterval", func() {
BeforeEach(func() {
interval = NewFixedInterval(actor, 5*time.Millisecond, MessageEvent)
})
It("should return the same interval on GetDelay", func() {
for i := 0; i < 100; i++ {
Ω(interval.GetDelay()).Should(Equal(5 * time.Millisecond))
}
})
It("should be able to be started and stopped multiple times", func() {
Ω(interval.Start()).Should(BeTrue())
Ω(interval.Start()).Should(BeFalse())
Ω(interval.Stop()).Should(BeTrue())
Ω(interval.Stop()).Should(BeFalse())
})
})
Describe("RandomInterval", func() {
BeforeEach(func() {
interval = NewRandomInterval(actor, 4*time.Millisecond, 6*time.Millisecond, MessageEvent)
})
It("should return a random interval, end inclusive on GetDelay", func() {
prev := time.Duration(0)
for i := 0; i < 100; i++ {
delay := interval.GetDelay()
Ω(delay).ShouldNot(Equal(prev))
Ω(delay).Should(BeNumerically(">=", 4*time.Millisecond))
Ω(delay).Should(BeNumerically("<=", 6*time.Millisecond))
prev = delay
}
})
It("should be able to be started and stopped multiple times", func() {
Ω(interval.Start()).Should(BeTrue())
Ω(interval.Start()).Should(BeFalse())
Ω(interval.Stop()).Should(BeTrue())
Ω(interval.Stop()).Should(BeFalse())
})
})
})