forked from avast/retry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry_test.go
164 lines (147 loc) · 3.82 KB
/
retry_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package retry
import (
"errors"
"testing"
"time"
"fmt"
"github.com/stretchr/testify/assert"
)
func TestDoAllFailed(t *testing.T) {
var retrySum uint
err := Do(
func() error { return errors.New("test") },
OnRetry(func(n uint, err error) { retrySum += n }),
Delay(time.Nanosecond),
)
assert.Error(t, err)
expectedErrorFormat := `All attempts fail:
#1: test
#2: test
#3: test
#4: test
#5: test
#6: test
#7: test
#8: test
#9: test
#10: test`
assert.Equal(t, expectedErrorFormat, err.Error(), "retry error format")
assert.Equal(t, uint(45), retrySum, "right count of retry")
}
func TestDoFirstOk(t *testing.T) {
var retrySum uint
err := Do(
func() error { return nil },
OnRetry(func(n uint, err error) { retrySum += n }),
)
assert.NoError(t, err)
assert.Equal(t, uint(0), retrySum, "no retry")
}
func TestRetryIf(t *testing.T) {
var retryCount uint
err := Do(
func() error {
if retryCount >= 2 {
return errors.New("special")
} else {
return errors.New("test")
}
},
OnRetry(func(n uint, err error) { retryCount++ }),
RetryIf(func(err error) bool {
return err.Error() != "special"
}),
Delay(time.Nanosecond),
)
assert.Error(t, err)
expectedErrorFormat := `All attempts fail:
#1: test
#2: test
#3: special`
assert.Equal(t, expectedErrorFormat, err.Error(), "retry error format")
assert.Equal(t, uint(2), retryCount, "right count of retry")
}
func TestDefaultSleep(t *testing.T) {
start := time.Now()
err := Do(
func() error { return errors.New("test") },
Attempts(3),
)
dur := time.Since(start)
assert.Error(t, err)
assert.True(t, dur > 300*time.Millisecond, "3 times default retry is longer then 300ms")
}
func TestFixedSleep(t *testing.T) {
start := time.Now()
err := Do(
func() error { return errors.New("test") },
Attempts(3),
DelayType(FixedDelay),
)
dur := time.Since(start)
assert.Error(t, err)
assert.True(t, dur < 500*time.Millisecond, "3 times default retry is shorter then 500ms")
}
func TestLastErrorOnly(t *testing.T) {
var retrySum uint
err := Do(
func() error { return fmt.Errorf("%d", retrySum) },
OnRetry(func(n uint, err error) { retrySum += 1 }),
Delay(time.Nanosecond),
LastErrorOnly(true),
)
assert.Error(t, err)
assert.Equal(t, "9", err.Error())
}
func TestUnrecoverableError(t *testing.T) {
attempts := 0
expectedErr := errors.New("error")
err := Do(
func() error {
attempts++
return Unrecoverable(expectedErr)
},
Attempts(2),
LastErrorOnly(true),
)
assert.Equal(t, expectedErr, err)
assert.Equal(t, 1, attempts, "unrecoverable error broke the loop")
}
func TestCombineFixedDelays(t *testing.T) {
start := time.Now()
err := Do(
func() error { return errors.New("test") },
Attempts(3),
DelayType(CombineDelay(FixedDelay, FixedDelay)),
)
dur := time.Since(start)
assert.Error(t, err)
assert.True(t, dur > 400*time.Millisecond, "3 times combined, fixed retry is longer then 400ms")
assert.True(t, dur < 500*time.Millisecond, "3 times combined, fixed retry is shorter then 500ms")
}
func TestRandomDelay(t *testing.T) {
start := time.Now()
err := Do(
func() error { return errors.New("test") },
Attempts(3),
DelayType(RandomDelay),
MaxJitter(50*time.Millisecond),
)
dur := time.Since(start)
assert.Error(t, err)
assert.True(t, dur > 2*time.Millisecond, "3 times random retry is longer then 2ms")
assert.True(t, dur < 100*time.Millisecond, "3 times random retry is shorter then 100ms")
}
func TestMaxDelay(t *testing.T) {
start := time.Now()
err := Do(
func() error { return errors.New("test") },
Attempts(5),
Delay(10*time.Millisecond),
MaxDelay(50*time.Millisecond),
)
dur := time.Since(start)
assert.Error(t, err)
assert.True(t, dur > 170*time.Millisecond, "5 times with maximum delay retry is longer than 70ms")
assert.True(t, dur < 200*time.Millisecond, "5 times with maximum delay retry is shorter than 200ms")
}