generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstrategy_test.go
148 lines (116 loc) · 3.81 KB
/
strategy_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package backoff_test
import (
"math"
"time"
"github.com/dogmatiq/linger"
. "github.com/dogmatiq/linger/backoff"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("func Exponential()", func() {
It("returns a strategy that backs-off exponentially", func() {
strategy := Exponential(3 * time.Second)
Expect(strategy(nil, 4)).To(Equal(48 * time.Second))
Expect(strategy(nil, 5)).To(Equal(96 * time.Second))
})
It("panics if the unit is zero", func() {
Expect(func() {
Exponential(0)
}).To(Panic())
})
It("panics if the unit is negative", func() {
Expect(func() {
Exponential(-1)
}).To(Panic())
})
It("does not overflow the time.Duration type", func() {
strategy := Exponential(1)
// No overflow at 2^62. We have 63-bits of positive magnitude available
// because time.Duration is a signed 64-bit integer.
Expect(strategy(nil, 62)).To(Equal(time.Duration(math.Pow(2, 62))))
// Starts overflowing at 2^63.
Expect(strategy(nil, 63)).To(Equal(linger.MaxDuration))
// Continues to return the capped value as the exponent increases.
Expect(strategy(nil, 100)).To(Equal(linger.MaxDuration))
})
})
var _ = Describe("func Constant()", func() {
It("returns a strategy that returns a fixed duration", func() {
strategy := Constant(3 * time.Second)
Expect(strategy(nil, 4)).To(Equal(3 * time.Second))
Expect(strategy(nil, 5)).To(Equal(3 * time.Second))
})
})
var _ = Describe("func Linear()", func() {
It("returns a strategy that returns a linearly increasing duration", func() {
strategy := Linear(3 * time.Second)
Expect(strategy(nil, 4)).To(Equal(15 * time.Second))
Expect(strategy(nil, 5)).To(Equal(18 * time.Second))
})
It("panics if the unit is zero", func() {
Expect(func() {
Linear(0)
}).To(Panic())
})
It("panics if the unit is negative", func() {
Expect(func() {
Linear(-1)
}).To(Panic())
})
It("does not overflow the time.Duration type", func() {
unit := linger.MaxDuration - 1
strategy := Linear(unit)
// No overflow at 1 * unit, as it's slightly below the max.
Expect(strategy(nil, 0)).To(Equal(unit))
// Start overflowing at 2 * unit. Overflow is detected because the
// result of the multiplication wraps negative.
Expect(unit * 2).To(BeNumerically("<", 0)) // verify test inputs actually wrap
Expect(strategy(nil, 1)).To(Equal(linger.MaxDuration))
// Continue overflowing. Overflow is detected because the result of the
// multiplication wraps negative, and continues on to be positive again.
Expect(unit * 5).To(BeNumerically(">", 0)) // verify test inputs actually wrap
Expect(strategy(nil, 4)).To(Equal(linger.MaxDuration))
})
})
var _ = Describe("func WithTransform()", func() {
It("returns a strategy that that transforms the result of the input strategy", func() {
s := WithTransforms(
Linear(10*time.Second),
linger.Limiter(15*time.Second, linger.MaxDuration),
linger.Limiter(0, 25*time.Second),
)
Expect(s(nil, 0)).To(Equal(15 * time.Second))
Expect(s(nil, 1)).To(Equal(20 * time.Second))
Expect(s(nil, 2)).To(Equal(25 * time.Second))
})
})
var _ = Describe("func CoalesceStrategy()", func() {
It("returns a strategy that yields the first positive duration", func() {
stgyOne := func(_ error, n uint) time.Duration {
if n == 1 {
return 6 * time.Second
}
return 0
}
stgyTwo := func(_ error, n uint) time.Duration {
if n == 2 {
return 9 * time.Second
}
return 0
}
s := CoalesceStrategy(
stgyOne,
stgyTwo,
)
sC := CoalesceStrategy(
s,
Constant(3*time.Second),
)
Expect(s(nil, 0)).To(Equal(0 * time.Second))
Expect(s(nil, 1)).To(Equal(6 * time.Second))
Expect(s(nil, 2)).To(Equal(9 * time.Second))
Expect(sC(nil, 0)).To(Equal(3 * time.Second))
Expect(sC(nil, 1)).To(Equal(6 * time.Second))
Expect(sC(nil, 2)).To(Equal(9 * time.Second))
})
})