-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbackoff_test.go
87 lines (71 loc) · 2.13 KB
/
backoff_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
package repeat
import (
"math"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestConstantBackoff(t *testing.T) {
fn77 := FixedBackoffAlgorithm(77)
assert.EqualValues(t, fn77(), 77)
assert.EqualValues(t, fn77(), 77)
}
func TestFullJitterBackoffDefaults(t *testing.T) {
do := &DelayOptions{}
FullJitterBackoff(time.Second).Set()(do)
for i := 0; i < 20; i++ {
c := int64(math.Pow(2, float64(i)))
InRange(t, do.Backoff(), 0, time.Duration(c)*time.Second)
}
}
func TestFullJitterBackoff(t *testing.T) {
do := &DelayOptions{}
FullJitterBackoff(1).WithMaxDelay(30).Set()(do)
for i := 0; i < 50; i++ {
c := int64(math.Pow(2, float64(i)))
if c > 30 {
c = 30
}
InRange(t, do.Backoff(), 0, time.Duration(c))
}
}
var floatSecond = float64(time.Second)
func TestExponentialBackoffDefaults(t *testing.T) {
do := &DelayOptions{}
ExponentialBackoff(time.Second).Set()(do)
for i := 0; i < 30; i++ {
c := math.Pow(2, float64(i))
InRange(t, do.Backoff(), time.Duration(c*floatSecond), time.Duration(c*floatSecond))
}
}
func TestExponentialBackoffJitter(t *testing.T) {
do := &DelayOptions{}
ExponentialBackoff(time.Second).WithJitter(.5).Set()(do)
for i := 0; i < 30; i++ {
c := math.Pow(2, float64(i))
fi := .5 * c
InRange(t, do.Backoff(), time.Duration((c-fi)*floatSecond), time.Duration((c+fi)*floatSecond))
}
}
func TestExponentialBackoffJitterAndMultiplier(t *testing.T) {
do := &DelayOptions{}
ExponentialBackoff(time.Second).WithJitter(.1).WithMultiplier(1.74).Set()(do)
for i := 0; i < 30; i++ {
c := math.Pow(1.74, float64(i))
fi := .1 * c
InRange(t, do.Backoff(), time.Duration((c-fi)*floatSecond), time.Duration((c+fi)*floatSecond))
}
}
func TestExponentialBackoff(t *testing.T) {
do := &DelayOptions{}
ExponentialBackoff(354 * time.Millisecond).WithJitter(.9).WithMultiplier(1.12).WithMaxDelay(5 * time.Second).Set()(do)
initDelay := float64(354 * time.Millisecond)
for i := 0; i < 300; i++ {
c := math.Pow(1.12, float64(i)) * initDelay
if c > float64(5*time.Second) {
c = float64(5 * time.Second)
}
fi := .9 * c
InRange(t, do.Backoff(), time.Duration(c-fi), time.Duration(c+fi))
}
}