forked from go-sql-driver/mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackoff_test.go
143 lines (114 loc) · 5 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
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
package mysql
import (
"math"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewConstantBackoff_Success(t *testing.T) {
backoff := newConstantBackoff()
assert.NotNil(t, backoff)
assert.Equal(t, 500*time.Millisecond, backoff.backoffInterval)
assert.Equal(t, 200*time.Millisecond, backoff.jitterInterval)
assert.Equal(t, 3*time.Second, backoff.maxInterval)
}
func TestConstantBackoff_NextInterval(t *testing.T) {
backoff := newConstantBackoff()
// order less than 0
nextInterval := backoff.NextInterval(-1)
assert.True(t, nextInterval == 0*time.Millisecond)
// order 0
nextInterval = backoff.NextInterval(0)
assert.True(t, nextInterval == 0*time.Millisecond)
// order more than 0
nextInterval = backoff.NextInterval(1)
assert.True(t, nextInterval >= backoff.backoffInterval)
assert.True(t, nextInterval < backoff.backoffInterval+backoff.jitterInterval)
assert.True(t, nextInterval <= backoff.maxInterval+backoff.jitterInterval)
nextInterval = backoff.NextInterval(2)
assert.True(t, nextInterval >= backoff.backoffInterval)
assert.True(t, nextInterval < backoff.backoffInterval+backoff.jitterInterval)
assert.True(t, nextInterval <= backoff.maxInterval+backoff.jitterInterval)
nextInterval = backoff.NextInterval(3)
assert.True(t, nextInterval >= backoff.backoffInterval)
assert.True(t, nextInterval < backoff.backoffInterval+backoff.jitterInterval)
assert.True(t, nextInterval <= backoff.maxInterval+backoff.jitterInterval)
nextInterval = backoff.NextInterval(4)
assert.True(t, nextInterval >= backoff.backoffInterval)
assert.True(t, nextInterval < backoff.backoffInterval+backoff.jitterInterval)
assert.True(t, nextInterval <= backoff.maxInterval+backoff.jitterInterval)
nextInterval = backoff.NextInterval(5)
assert.True(t, nextInterval >= backoff.backoffInterval)
assert.True(t, nextInterval < backoff.backoffInterval+backoff.jitterInterval)
assert.True(t, nextInterval <= backoff.maxInterval+backoff.jitterInterval)
}
func TestNewExponentialBackoff_Success(t *testing.T) {
backoff := newExponentialBackoff()
assert.NotNil(t, backoff)
assert.Equal(t, 500*time.Millisecond, backoff.backoffInterval)
assert.Equal(t, 200*time.Millisecond, backoff.jitterInterval)
assert.Equal(t, 3*time.Second, backoff.maxInterval)
assert.Equal(t, int64(2), backoff.multiplier)
}
func TestExponentialBackoff_NextInterval(t *testing.T) {
backoff := newExponentialBackoff()
// order less than 0
nextInterval := backoff.NextInterval(-1)
assert.True(t, nextInterval == 0*time.Millisecond)
// order 0
nextInterval = backoff.NextInterval(0)
assert.True(t, nextInterval == 0*time.Millisecond)
// order 1
nextInterval = backoff.NextInterval(1)
assert.True(t, nextInterval >= backoff.backoffInterval)
assert.True(t, nextInterval < backoff.backoffInterval+backoff.jitterInterval)
assert.True(t, nextInterval <= backoff.maxInterval+backoff.jitterInterval)
// order 2
nextInterval = backoff.NextInterval(2)
backoffInterval := time.Duration(math.Pow(float64(backoff.multiplier), 1)) * backoff.backoffInterval
assert.True(t, nextInterval >= backoffInterval)
assert.True(t, nextInterval < backoffInterval+backoff.jitterInterval)
assert.True(t, nextInterval <= backoff.maxInterval+backoff.jitterInterval)
// order 3
nextInterval = backoff.NextInterval(3)
backoffInterval = time.Duration(math.Pow(float64(backoff.multiplier), 2)) * backoff.backoffInterval
assert.True(t, nextInterval >= backoffInterval)
assert.True(t, nextInterval < backoffInterval+backoff.jitterInterval)
assert.True(t, nextInterval <= backoff.maxInterval+backoff.jitterInterval)
// order 4
// it exceeds max interval
nextInterval = backoff.NextInterval(4)
backoffInterval = time.Duration(math.Pow(float64(backoff.multiplier), 3)) * backoff.backoffInterval
assert.True(t, nextInterval <= backoffInterval)
assert.True(t, nextInterval <= backoff.maxInterval+backoff.jitterInterval)
// order 5
// it exceeds max interval
nextInterval = backoff.NextInterval(5)
backoffInterval = time.Duration(math.Pow(float64(backoff.multiplier), 4)) * backoff.backoffInterval
assert.True(t, nextInterval <= backoffInterval)
assert.True(t, nextInterval <= backoff.maxInterval+backoff.jitterInterval)
}
func TestNewNoBackoff_Success(t *testing.T) {
backoff := newNoBackoff()
assert.NotNil(t, backoff)
}
func TestNoBackoff_NextInterval(t *testing.T) {
backoff := newNoBackoff()
// order less than 0
nextInterval := backoff.NextInterval(-1)
assert.True(t, nextInterval == 0*time.Millisecond)
// order 0
nextInterval = backoff.NextInterval(0)
assert.True(t, nextInterval == 0*time.Millisecond)
// order more than 0
nextInterval = backoff.NextInterval(1)
assert.True(t, nextInterval == 0*time.Millisecond)
nextInterval = backoff.NextInterval(2)
assert.True(t, nextInterval == 0*time.Millisecond)
nextInterval = backoff.NextInterval(3)
assert.True(t, nextInterval == 0*time.Millisecond)
nextInterval = backoff.NextInterval(4)
assert.True(t, nextInterval == 0*time.Millisecond)
nextInterval = backoff.NextInterval(5)
assert.True(t, nextInterval == 0*time.Millisecond)
}