-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
47 lines (39 loc) · 1.32 KB
/
utils_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
package retry
import (
"reflect"
"testing"
)
func TestExponentialBackoff(t *testing.T) {
backOffs := []uint64{}
backOff := uint64(1)
backOffs = append(backOffs, backOff) // backOffs = [1]
backOffFn := ExponentialBackoff(10)
// backOffs = [1]
backOff = backOffFn(backOff)
backOffs = append(backOffs, backOff) // backOffs = [1, 2]
backOff = backOffFn(backOff)
backOffs = append(backOffs, backOff) // backOffs = [1, 2, 4]
backOff = backOffFn(backOff)
backOffs = append(backOffs, backOff) // backOffs = [1, 2, 4, 8]
backOff = backOffFn(backOff)
backOffs = append(backOffs, backOff) // backOffs = [1, 2, 4, 8, 10] Because maxBackOff expected is 10
expectedBackOffs := []uint64{1, 2, 4, 8, 10}
if !reflect.DeepEqual(backOffs, expectedBackOffs) {
t.Fatalf("Expected BackOff should be %+v but found %+v", expectedBackOffs, backOffs)
}
}
func TestRandomBackOff(t *testing.T) {
lower := 2
upper := 7
initialBackOff := uint64(1)
backoffFunc := RandomBackoff(lower, upper)
firstBackOff := backoffFunc(initialBackOff)
secondBackOff := backoffFunc(firstBackOff)
thirdBackOff := backoffFunc(firstBackOff)
backOffs := []uint64{firstBackOff, secondBackOff, thirdBackOff}
for _, num := range backOffs {
if int(num) < lower || int(num) > upper {
t.Fatalf("Random number %d should be between %d and %d", num, lower, upper)
}
}
}