-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry_test.go
193 lines (167 loc) · 4.47 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
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
package goretry_test
import (
"context"
"errors"
"fmt"
"strings"
"testing"
"time"
goretry "github.com/IlyasYOY/go-retry"
)
var errTest = errors.New("test error")
func TestUnlimitedRetriesEverySecond(t *testing.T) {
t.Parallel()
t.Run("correct wait time", func(t *testing.T) {
t.Parallel()
retryer := goretry.New[any]()
failingFunc := failingFuncProducer(2)
finishesIn := time.Second * 3
timeRetrying := assertRetriesFinishedIn(t, retryer, failingFunc, finishesIn)
if timeRetrying.Seconds() < 2 {
t.Fatal("too soon to end retries", timeRetrying)
}
})
t.Run("returns value after errors are gone", func(t *testing.T) {
t.Parallel()
retryer := goretry.New[any](
goretry.WithInitialDelay(time.Second),
)
err := retryer.Retry(failingFuncProducer(1))
if err != nil {
t.Logf("error must be nil after retries but was %v", err)
t.Fail()
}
})
t.Run("retry for value works", func(t *testing.T) {
t.Parallel()
retryer := goretry.New[int](
goretry.WithInitialDelay(time.Second),
)
expected := 10
result, err := retryer.RetryReturn(failingReturnFuncProducer(1, expected))
if err != nil {
t.Logf("error must be nil after retries but was %v", err)
t.Fail()
}
if result != expected {
t.Logf("result must be %d", expected)
t.Fail()
}
})
}
func TestWithCustomConstantDelayInfiniteTimes(t *testing.T) {
t.Parallel()
retryer := goretry.New[any](
goretry.WithInitialDelay(time.Millisecond * 10),
)
failingFunc := failingFuncProducer(100)
finishesIn := time.Second + 100*time.Millisecond
timeRetrying := assertRetriesFinishedIn(t, retryer, failingFunc, finishesIn)
if timeRetrying.Seconds() < 1 {
t.Fatal("too soon to end retries", timeRetrying)
}
}
func TestWithCustomConstantDelayAndRetryLimit(t *testing.T) {
t.Parallel()
retryer := goretry.New[any](
goretry.WithInitialDelay(time.Second),
goretry.WithMaxRetries(2),
)
err := retryer.Retry(failingFuncProducer(3))
if err == nil {
t.Logf("error must not be nil after retries but was %v", err)
t.Fail()
}
assertErrorNumber(t, err, 3)
}
func TestCustomBuilder(t *testing.T) {
t.Parallel()
retryer := goretry.New[any](
goretry.WithInitialDelay(time.Second),
goretry.WithMaxRetries(2),
)
err := retryer.Retry(failingFuncProducer(3))
if err == nil {
t.Logf("error must not be nil after retries but was %v", err)
t.Fail()
}
assertErrorNumber(t, err, 3)
}
func TestRetriesWithIncreasingBackoff(t *testing.T) {
t.Parallel()
retryer := goretry.New[any](
goretry.WithInitialDelay(time.Second),
goretry.WithIncreasingDelay(time.Second),
)
failingFunc := failingFuncProducer(2)
finishesIn := time.Second * 4
timeRetrying := assertRetriesFinishedIn(t, retryer, failingFunc, finishesIn)
if timeRetrying.Seconds() < 3 {
t.Fatal("too soon to end retries", timeRetrying)
}
}
func TestRetriesWithJitter(t *testing.T) {
t.Parallel()
retryer := goretry.New[any](
goretry.WithInitialDelay(time.Second),
goretry.WithJittingDelay(time.Second/2),
)
failingFunc := failingFuncProducer(1)
finishesIn := time.Second + 500*time.Millisecond
timeRetrying := assertRetriesFinishedIn(t, retryer, failingFunc, finishesIn)
if timeRetrying.Seconds() < 0.5 {
t.Fatal("too soon to end retries", timeRetrying)
}
}
func assertErrorNumber(t *testing.T, err error, errorNumber goretry.RetryCount) {
requiredPrefix := "call #" + fmt.Sprint(errorNumber)
errorMessage := err.Error()
if !strings.HasPrefix(errorMessage, requiredPrefix) {
t.Logf("error '%s' must have prefix '%s'", errorMessage, requiredPrefix)
}
}
func assertRetriesFinishedIn[T any](
t *testing.T,
retryer goretry.Retryer[T],
failingFunc goretry.RetryFunc,
finishesIn time.Duration,
) time.Duration {
start := time.Now()
deadline := start.Add(finishesIn)
ctx, cancel := context.WithDeadline(context.Background(), deadline)
go func() {
_ = retryer.Retry(failingFunc)
cancel()
}()
<-ctx.Done()
if ctx.Err() != context.Canceled {
t.Fatal("function took to long to retry")
}
return time.Since(start)
}
func failingReturnFuncProducer[T any](
times goretry.RetryCount,
value T,
) goretry.RetryReturningFunc[T] {
allTimes := times
var empty T
return func() (T, error) {
if times != 0 {
times--
return empty, fmt.Errorf("call #%d: %w", allTimes-times, errTest)
}
return value, nil
}
}
func failingFuncProducer(
times goretry.RetryCount,
) goretry.RetryFunc {
allTimes := times
return func() error {
if times != 0 {
times--
return fmt.Errorf("call #%d: %w", allTimes-times, errTest)
}
return nil
}
}