-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjitterOff_test.go
More file actions
198 lines (160 loc) · 5.25 KB
/
jitterOff_test.go
File metadata and controls
198 lines (160 loc) · 5.25 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package jitteroff
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewJitter(t *testing.T) {
/*
Testing jitteroff with default settings
*/
// SETUP
j := NewDefaultJitterOff()
// VALIDATE
assert.Equal(t, j.minTime, time.Duration(100*time.Millisecond), "Default min timeout correctly set.")
assert.Equal(t, j.capTime, time.Duration(400*time.Millisecond), "Default cap timeout correctly set.")
assert.Equal(t, j.backOffTime, time.Duration(0*time.Millisecond), "Default backoff timeout correctly set.")
assert.Equal(t, j.maxAttempt, 2, "Default max attempt correctly set.")
}
func TestCustomerJitter(t *testing.T) {
/*
Testing jitteroff with custom settings
*/
totalAttempt := 2
time1 := 100 * time.Millisecond
time2 := 400 * time.Millisecond
// SETUP
j := NewCustomJitterOff(totalAttempt, time1, time2)
// VALIDATE
assert.Equal(t, j.minTime, time1, "Custom min timeout correctly set.")
assert.Equal(t, j.capTime, time2, "Custom max timeout correctly set.")
assert.Equal(t, j.backOffTime, time.Duration(0*time.Second), "Custom backoff timeout correctly set.")
assert.Equal(t, j.maxAttempt, totalAttempt, "Custom max attempt correctly set.")
}
func TestExecute_DefaultSetting(t *testing.T) {
/*
Test-1
with wait call so that call is successfull
*/
// SETUP
j := NewDefaultJitterOff()
// TEST
_, err := j.Do(doWaitCall(95 * time.Millisecond))
// VALIDATE that request is successfull
assert.Nil(t, err, "Function able to perform the task timely")
assert.Equal(t, j.attempt, 0, "No retry required for successful call")
/*
Test-2
with success call
*/
// TEST
_, err = j.Do(doSuccessCall())
// VALIDATE that request is successfull
assert.Nil(t, err, "Function able to perform the task timely")
assert.Equal(t, j.attempt, 0, "No retry required for successful call")
/*
Test-3
Request which fails for all max attempts.
*/
// TEST
_, err = j.Do(doFailCall())
// VALIDATE
assert.NotNil(t, err, "Function able to perform the task timely")
assert.Equal(t, j.attempt, j.maxAttempt, "Max retries reached")
assert.Greater(t, int(j.backOffTime), int(j.minTime), "No retry required for successful call")
assert.LessOrEqual(t, int(j.backOffTime), int(200*time.Millisecond), "No retry required for successful call")
/*
Test-4
One more success call after fail call
timer and num of attempts should be resetted.
*/
// TEST
_, err = j.Do(doSuccessCall())
// VALIDATE
assert.Nil(t, err, "Function able to perform the task timely")
assert.Equal(t, j.attempt, 0, "No retry required for successful call")
assert.Equal(t, j.backOffTime, time.Duration(0), "BackOff time is resetted")
}
func TestExecute_CustomSetting(t *testing.T) {
/*
Testing jitteroff execute method with custom settings
*/
totalAttempt := 5
time1 := 200 * time.Millisecond
time2 := 800 * time.Millisecond
// SETUP
j := NewCustomJitterOff(totalAttempt, time1, time2)
// TEST
_, err := j.Do(doWaitCall(95 * time.Millisecond))
// VALIDATE that request is successfull
assert.Nil(t, err, "Function able to perform the task timely")
assert.Equal(t, j.attempt, 0, "No retry required for successful call")
/*
Test-2
with success call
*/
// TEST
_, err = j.Do(doSuccessCall())
// VALIDATE that request is successfull
assert.Nil(t, err, "Function able to perform the task timely")
assert.Equal(t, j.attempt, 0, "No retry required for successful call")
/*
Test-3
Request which fails for all max attempts.
*/
// TEST
_, err = j.Do(doFailCall())
// VALIDATE
assert.NotNil(t, err, "Function able to perform the task timely")
assert.Equal(t, j.attempt, totalAttempt, "Max retries reached")
assert.GreaterOrEqual(t, int(j.backOffTime), int(time1), "No retry required for successful call")
assert.LessOrEqual(t, int(j.backOffTime), int(time2), "No retry required for successful call")
/*
Test-4
One more success call after fail call
timer and num of attempts should be resetted.
*/
// TEST
_, err = j.Do(doSuccessCall())
// VALIDATE
assert.Nil(t, err, "Function able to perform the task timely")
assert.Equal(t, j.attempt, 0, "No retry required for successful call")
assert.Equal(t, j.backOffTime, time.Duration(0), "Backoff time should be resetted to 0")
}
func TestReset(t *testing.T){
/*
Test to verify if reset successfully resets the
number of attempts and back-off time.
*/
j := NewDefaultJitterOff()
assert.Equal(t, j.attempt, 0, "Correct attempts configured ")
j.attempt=2
j.backOffTime=time.Duration(time.Second)
assert.Equal(t, j.attempt, 2, "Attempts correctly changed")
assert.Equal(t, j.backOffTime, time.Duration(time.Second), "BackOff Time updated correctly")
// TEST by resetting the timers and attempts
j.Reset()
assert.Equal(t, j.attempt, 0, "Attempts correctly changed")
assert.Equal(t, j.backOffTime, time.Duration(0), "BackOff Time updated correctly")
}
// HELPER
// do wait call
func doWaitCall(duration time.Duration) func() (interface{}, error) {
return func() (interface{}, error) {
// do some wait time task
time.Sleep(duration)
return nil, nil
}
}
// do success call
func doSuccessCall() func() (interface{}, error) {
return func() (interface{}, error) {
return nil, nil
}
}
// do fail call
func doFailCall() func() (interface{}, error) {
return func() (interface{}, error) {
return nil, errFailed
}
}