-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry_example_test.go
204 lines (182 loc) · 3.58 KB
/
retry_example_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
194
195
196
197
198
199
200
201
202
203
204
package retry_test
import (
"context"
"errors"
"fmt"
"time"
"github.com/arsham/retry/v3"
)
func ExampleRetry_DoContext() {
r := &retry.Retry{
Attempts: 4,
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err := r.DoContext(ctx, func() error {
// we cancel early to prove a point.
cancel()
return nil
}, func() error {
panic("should not have been called")
})
fmt.Println("Error:", err)
// Output:
// Error: context canceled
}
func ExampleRetry_Do() {
r := &retry.Retry{
Attempts: 4,
}
err := r.Do(func() error {
return nil
})
fmt.Println("Error:", err)
// Output:
// Error: <nil>
}
func ExampleRetry_Do_zero() {
r := &retry.Retry{}
err := r.Do(func() error {
fmt.Println("this should not happen")
return nil
})
fmt.Println("Error:", err)
// Output:
// Error: <nil>
}
func ExampleRetry_Do_error() {
r := &retry.Retry{
Attempts: 4,
Delay: time.Nanosecond,
}
err := r.Do(func() error {
return errors.New("some error")
})
fmt.Println("Error:", err)
// Output:
// Error: some error
}
func ExampleRetry_Do_standardMethod() {
r := &retry.Retry{
Attempts: 4,
Delay: time.Nanosecond,
}
i := 0
err := r.Do(func() error {
i++
fmt.Printf("Running iteration %d.\n", i)
if i < 3 {
return errors.New("ignored error")
}
return nil
})
fmt.Println("Error:", err)
// Output:
// Running iteration 1.
// Running iteration 2.
// Running iteration 3.
// Error: <nil>
}
func ExampleStopError() {
r := &retry.Retry{
Attempts: 10,
}
i := 0
err := r.Do(func() error {
i++
fmt.Printf("Running iteration %d.\n", i)
if i > 2 {
return &retry.StopError{}
}
return errors.New("ignored error")
})
fmt.Println("Error:", err)
// Output:
// Running iteration 1.
// Running iteration 2.
// Running iteration 3.
// Error: <nil>
}
func ExampleStopError_stopErr() {
r := &retry.Retry{
Attempts: 10,
}
i := 0
stopErr := &retry.StopError{
Err: errors.New("this is the returned error"),
}
err := r.Do(func() error {
i++
if i > 2 {
return stopErr
}
return errors.New("ignored error")
})
fmt.Println("Error:", err)
fmt.Println("Stopped with:", stopErr)
// Output:
// Error: this is the returned error
// Stopped with: this is the returned error
}
func ExampleIncrementalDelay() {
// This setup will delay 20ms + 40ms + 80ms + 160ms, and a jitters at 5
// attempts, until on the 6th attempt that it would succeed.
r := &retry.Retry{
Attempts: 6,
Delay: 20 * time.Millisecond,
Method: retry.IncrementalDelay,
}
i := 0
err := r.Do(func() error {
i++
if i < r.Attempts {
return errors.New("ignored error")
}
return nil
})
fmt.Println("Error:", err)
// Output:
// Error: <nil>
}
func ExampleIncrementalDelayMax() {
// This setup will delay 20ms + 40ms + 50ms + 50ms, and a jitters at 5
// attempts, until on the 6th attempt that it would succeed.
r := &retry.Retry{
Attempts: 6,
Delay: 20 * time.Millisecond,
Method: retry.IncrementalDelayMax(50 * time.Millisecond),
}
i := 0
err := r.Do(func() error {
i++
if i < r.Attempts {
return errors.New("ignored error")
}
return nil
})
fmt.Println("Error:", err)
// Output:
// Error: <nil>
}
func ExampleRetry_Do_multipleFuncs() {
r := &retry.Retry{
Attempts: 4,
Delay: time.Nanosecond,
}
err := r.Do(func() error {
fmt.Println("Running func 1.")
return nil
}, func() error {
fmt.Println("Running func 2.")
return nil
}, func() error {
fmt.Println("Running func 3.")
return nil
})
fmt.Println("Error:", err)
// Output:
// Running func 1.
// Running func 2.
// Running func 3.
// Error: <nil>
}