forked from gojek/heimdall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backoff_test.go
110 lines (86 loc) · 4.34 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package heimdall
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestExponentialBackoffNextTime(t *testing.T) {
exponentialBackoff := NewExponentialBackoff(100*time.Millisecond, 1000*time.Millisecond, 2.0, 0*time.Millisecond)
assert.Equal(t, 100*time.Millisecond, exponentialBackoff.Next(0))
assert.Equal(t, 200*time.Millisecond, exponentialBackoff.Next(1))
assert.Equal(t, 400*time.Millisecond, exponentialBackoff.Next(2))
assert.Equal(t, 800*time.Millisecond, exponentialBackoff.Next(3))
}
func TestExponentialBackoffWithInvalidJitter(t *testing.T) {
exponentialBackoff := NewExponentialBackoff(100*time.Millisecond, 1000*time.Millisecond, 2.0, -1*time.Millisecond)
assert.Equal(t, 100*time.Millisecond, exponentialBackoff.Next(0))
assert.Equal(t, 200*time.Millisecond, exponentialBackoff.Next(1))
assert.Equal(t, 400*time.Millisecond, exponentialBackoff.Next(2))
assert.Equal(t, 800*time.Millisecond, exponentialBackoff.Next(3))
}
func TestExponentialBackoffMaxTimeoutCrossed(t *testing.T) {
exponentialBackoff := NewExponentialBackoff(100*time.Millisecond, 1000*time.Millisecond, 2.0, 0*time.Millisecond)
assert.Equal(t, 1000*time.Millisecond, exponentialBackoff.Next(4))
}
func TestExponentialBackoffMaxTimeoutReached(t *testing.T) {
exponentialBackoff := NewExponentialBackoff(100*time.Millisecond, 1600*time.Millisecond, 2.0, 0*time.Millisecond)
assert.Equal(t, 1600*time.Millisecond, exponentialBackoff.Next(4))
}
func TestExponentialBackoffWhenRetryIsLessThanZero(t *testing.T) {
exponentialBackoff := NewExponentialBackoff(100*time.Millisecond, 1000*time.Millisecond, 2.0, 0*time.Millisecond)
assert.Equal(t, 100*time.Millisecond, exponentialBackoff.Next(-1))
}
func TestExponentialBackoffJitter0(t *testing.T) {
exponentialBackoff := NewExponentialBackoff(100*time.Millisecond, 1000*time.Millisecond, 2.0, 0*time.Millisecond)
for i := 0; i < 10000; i++ {
assert.Equal(t, 200*time.Millisecond, exponentialBackoff.Next(1))
}
}
func TestExponentialBackoffJitter1(t *testing.T) {
exponentialBackoff := NewExponentialBackoff(100*time.Millisecond, 1000*time.Millisecond, 2.0, 1*time.Millisecond)
for i := 0; i < 10000; i++ {
assert.True(t, 200*time.Millisecond <= exponentialBackoff.Next(1) && exponentialBackoff.Next(1) <= 201*time.Millisecond)
}
}
func TestExponentialBackoffJitter50(t *testing.T) {
exponentialBackoff := NewExponentialBackoff(100*time.Millisecond, 1000*time.Millisecond, 2.0, 50*time.Millisecond)
for i := 0; i < 10000; i++ {
assert.True(t, 200*time.Millisecond <= exponentialBackoff.Next(1) && exponentialBackoff.Next(1) <= 250*time.Millisecond)
}
}
func TestConstantBackoffNextTime(t *testing.T) {
constantBackoff := NewConstantBackoff(100*time.Millisecond, 0*time.Millisecond)
assert.Equal(t, 100*time.Millisecond, constantBackoff.Next(0))
assert.Equal(t, 100*time.Millisecond, constantBackoff.Next(1))
assert.Equal(t, 100*time.Millisecond, constantBackoff.Next(2))
assert.Equal(t, 100*time.Millisecond, constantBackoff.Next(3))
}
func TestConstantBackoffWithInvalidJitter(t *testing.T) {
constantBackoff := NewConstantBackoff(100*time.Millisecond, -1*time.Millisecond)
assert.Equal(t, 100*time.Millisecond, constantBackoff.Next(0))
assert.Equal(t, 100*time.Millisecond, constantBackoff.Next(1))
assert.Equal(t, 100*time.Millisecond, constantBackoff.Next(2))
assert.Equal(t, 100*time.Millisecond, constantBackoff.Next(3))
}
func TestConstantBackoffWhenRetryIsLessThanZero(t *testing.T) {
constantBackoff := NewConstantBackoff(100*time.Millisecond, 0*time.Millisecond)
assert.Equal(t, 100*time.Millisecond, constantBackoff.Next(-1))
}
func TestConstantBackoffJitter0(t *testing.T) {
constantBackoff := NewConstantBackoff(100*time.Millisecond, 0*time.Millisecond)
for i := 0; i < 10000; i++ {
assert.Equal(t, 100*time.Millisecond, constantBackoff.Next(i))
}
}
func TestConstantBackoffJitter1(t *testing.T) {
constantBackoff := NewConstantBackoff(100*time.Millisecond, 1*time.Millisecond)
for i := 0; i < 10000; i++ {
assert.True(t, 100*time.Millisecond <= constantBackoff.Next(i) && constantBackoff.Next(1) <= 101*time.Millisecond)
}
}
func TestConstantBackoffJitter50(t *testing.T) {
constantBackoff := NewConstantBackoff(100*time.Millisecond, 50*time.Millisecond)
for i := 0; i < 10000; i++ {
assert.True(t, 100*time.Millisecond <= constantBackoff.Next(i) && constantBackoff.Next(1) <= 150*time.Millisecond)
}
}