-
Notifications
You must be signed in to change notification settings - Fork 0
/
backoffs_test.go
170 lines (151 loc) · 3.52 KB
/
backoffs_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package sagas
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func Test_ConstantBackoff(t *testing.T) {
t.Parallel()
type args struct {
duration time.Duration
n int
}
tests := []struct {
name string
args args
want []time.Duration
}{
{
name: "[SUCCESS] Should return a slice of time.Duration with 3 elements",
args: args{
duration: 1 * time.Second,
n: 3,
},
want: []time.Duration{1 * time.Second, 1 * time.Second, 1 * time.Second},
},
{
name: "[SUCCESS] Should return a slice of time.Duration with 0 elements",
args: args{
duration: 1 * time.Second,
n: 0,
},
want: []time.Duration{},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
assert.NotPanics(t, func() {
got := BackoffConstant(test.args.n, test.args.duration)
assert.Equal(t, test.want, got)
})
})
}
}
func Test_ExponentialBackoff(t *testing.T) {
t.Parallel()
type args struct {
duration time.Duration
n int
r float64
}
tests := []struct {
name string
args args
want []time.Duration
}{
{
name: "[SUCCESS] Should return a slice of time.Duration with 3 elements and rate 2",
args: args{
duration: 1 * time.Second,
n: 3,
r: 2,
},
want: []time.Duration{1 * time.Second, 2 * time.Second, 4 * time.Second},
},
{
name: "[SUCCESS] Should return a slice of time.Duration with 5 elements and rate 3",
args: args{
duration: 1 * time.Second,
n: 5,
r: 3,
},
want: []time.Duration{1 * time.Second, 3 * time.Second, 9 * time.Second, 27 * time.Second, 81 * time.Second},
},
{
name: "[SUCCESS] Should return a slice of time.Duration with 0 elements",
args: args{
duration: 1 * time.Second,
n: 0,
r: 2,
},
want: []time.Duration{},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
assert.NotPanics(t, func() {
got := BackoffExponential(test.args.n, test.args.duration, test.args.r)
assert.Equal(t, test.want, got)
})
})
}
}
func Test_LimitedExponentialBackoff(t *testing.T) {
t.Parallel()
type args struct {
duration time.Duration
limit time.Duration
n int
r float64
}
tests := []struct {
name string
args args
want []time.Duration
}{
{
name: "[SUCCESS] Should return a slice of time.Duration with 3 elements and rate 2",
args: args{
duration: 1 * time.Second,
limit: 5 * time.Second,
n: 5,
r: 2,
},
want: []time.Duration{1 * time.Second, 2 * time.Second, 4 * time.Second, 5 * time.Second, 5 * time.Second},
},
{
name: "[SUCCESS] Should return a slice of time.Duration with 5 elements and rate 3",
args: args{
duration: 1 * time.Second,
limit: 5 * time.Second,
n: 5,
r: 3,
},
want: []time.Duration{1 * time.Second, 3 * time.Second, 5 * time.Second, 5 * time.Second, 5 * time.Second},
},
{
name: "[SUCCESS] Should return a slice of time.Duration with 0 elements",
args: args{
duration: 1 * time.Second,
limit: 5 * time.Second,
n: 0,
r: 2,
},
want: []time.Duration{},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
assert.NotPanics(t, func() {
got := BackoffLimitedExponential(test.args.n, test.args.duration, test.args.limit, test.args.r)
assert.Equal(t, test.want, got)
})
})
}
}