generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcounter_test.go
65 lines (52 loc) · 1.52 KB
/
counter_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
package backoff_test
import (
"context"
"time"
. "github.com/dogmatiq/linger/backoff"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("type Counter", func() {
var (
strategy Strategy
counter *Counter
)
BeforeEach(func() {
strategy = Linear(10 * time.Millisecond)
counter = &Counter{
Strategy: strategy,
}
})
Describe("func Reset()", func() {
It("resets the failure count", func() {
counter.Fail(nil)
counter.Fail(nil)
counter.Reset()
Expect(counter.Fail(nil)).To(Equal(10 * time.Millisecond))
})
})
Describe("func Fail()", func() {
It("uses the default strategy if none is specified", func() {
counter.Strategy = nil
// The default strategy uses FullJitter, so this is hard to
// test well, but essentially we're ensuring it doesn't panic.
Expect(counter.Fail(nil)).To(BeNumerically("<=", 3*time.Second))
Expect(counter.Fail(nil)).To(BeNumerically("<=", 6*time.Second))
Expect(counter.Fail(nil)).To(BeNumerically("<=", 12*time.Second))
})
It("uses the specified strategy", func() {
Expect(counter.Fail(nil)).To(Equal(10 * time.Millisecond))
Expect(counter.Fail(nil)).To(Equal(20 * time.Millisecond))
})
})
Describe("func Sleep()", func() {
It("sleeps for the computed wait duration", func() {
start := time.Now()
err := counter.Sleep(context.Background(), nil)
stop := time.Now()
elapsed := stop.Sub(start)
Expect(err).ShouldNot(HaveOccurred())
Expect(elapsed).To(BeNumerically(">=", 10*time.Millisecond))
})
})
})