@@ -120,6 +120,7 @@ func testLoopDoPanic(t *testing.T) {
120120func testLoopDoSleep (t * testing.T ) {
121121 t .Run ("StandardMethod" , testLoopDoSleepStandardMethod )
122122 t .Run ("IncrementalMethod" , testLoopDoSleepIncrementalMethod )
123+ t .Run ("IncrementalMaxMethod" , testLoopDoSleepIncrementalMaxMethod )
123124}
124125
125126func testLoopDoSleepStandardMethod (t * testing.T ) {
@@ -179,7 +180,7 @@ func testLoopDoSleepIncrementalMethodUnderSecond(t *testing.T) {
179180 fmt .Sprintf ("wanted to take more than %s, took %s" , expected .Sub (started ), finished .Sub (started )),
180181 )
181182 assert .True (t , finished .Before (expected .Add (6 * delay )),
182- fmt .Sprintf ("take (%s) more than expected: %s" , finished .Sub (started ), expected .Add (6 * delay )),
183+ fmt .Sprintf ("took (%s) more than expected: %s" , finished .Sub (started ), expected .Add (6 * delay )),
183184 )
184185}
185186
@@ -210,7 +211,7 @@ func testLoopDoSleepIncrementalMethodOverSecond(t *testing.T) {
210211 fmt .Sprintf ("wanted to take more than %s, took %s" , expected .Sub (started ), finished .Sub (started )),
211212 )
212213 assert .True (t , finished .Before (expected .Add (2 * time .Second )),
213- fmt .Sprintf ("take (%s) more than expected: %s" , finished .Sub (started ), 4 * time .Second ),
214+ fmt .Sprintf ("took (%s) more than expected: %s" , finished .Sub (started ), 4 * time .Second ),
214215 )
215216}
216217
@@ -233,7 +234,98 @@ func testLoopDoSleepIncrementalMethodZero(t *testing.T) {
233234
234235 expected := started .Add (time .Second )
235236 assert .True (t , finished .Before (expected ),
236- fmt .Sprintf ("take (%s) more than expected: %s" , finished .Sub (started ), time .Second ),
237+ fmt .Sprintf ("took (%s) more than expected: %s" , finished .Sub (started ), time .Second ),
238+ )
239+ }
240+
241+ func testLoopDoSleepIncrementalMaxMethod (t * testing.T ) {
242+ t .Run ("UnderSecond" , testLoopDoSleepIncrementalMaxMethodUnderSecond )
243+ t .Run ("OverSecond" , testLoopDoSleepIncrementalMaxMethodOverTwoSeconds )
244+ t .Run ("Zero" , testLoopDoSleepIncrementalMaxMethodZero )
245+ }
246+
247+ func testLoopDoSleepIncrementalMaxMethodUnderSecond (t * testing.T ) {
248+ t .Parallel ()
249+ // In this setup, the delays would be (almost) 100, 200, 300, 300. So in
250+ // almost 900 ms there would be 4 calls. There is a 4*delay amount of
251+ // wiggle added.
252+ delay := 100 * time .Millisecond
253+ l := & retry.Retry {
254+ Attempts : 4 ,
255+ Delay : delay ,
256+ Method : retry .IncrementalDelayMax (delay * 3 ),
257+ }
258+
259+ count := 0
260+ started := time .Now ()
261+ err := l .Do (func () error {
262+ count ++
263+ return assert .AnError
264+ })
265+ finished := time .Now ()
266+ expected := started .Add (900 * time .Millisecond )
267+
268+ assert .Equal (t , l .Attempts , count )
269+ assert .ErrorIs (t , err , assert .AnError )
270+ assert .True (t , finished .After (expected ),
271+ fmt .Sprintf ("wanted to take more than %s, took %s" , expected .Sub (started ), finished .Sub (started )),
272+ )
273+ assert .True (t , finished .Before (expected .Add (6 * delay )),
274+ fmt .Sprintf ("took (%s) more than expected: %s" , finished .Sub (started ), expected .Add (6 * delay )),
275+ )
276+ }
277+
278+ func testLoopDoSleepIncrementalMaxMethodOverTwoSeconds (t * testing.T ) {
279+ t .Parallel ()
280+ if testing .Short () {
281+ t .Skip ("slow test" )
282+ }
283+ l := & retry.Retry {
284+ Attempts : 2 ,
285+ Delay : 10 * time .Second ,
286+ Method : retry .IncrementalDelayMax (2 * time .Second ),
287+ }
288+
289+ count := 0
290+ started := time .Now ()
291+ err := l .Do (func () error {
292+ count ++
293+ return assert .AnError
294+ })
295+ finished := time .Now ()
296+ expected := started .Add (4 * time .Second )
297+
298+ assert .ErrorIs (t , err , assert .AnError )
299+ assert .Equal (t , l .Attempts , count )
300+
301+ assert .True (t , finished .After (expected ),
302+ fmt .Sprintf ("wanted to take more than %s, took %s" , expected .Sub (started ), finished .Sub (started )),
303+ )
304+ assert .True (t , finished .Before (expected .Add (2 * 2 * time .Second )),
305+ fmt .Sprintf ("took (%s) more than expected: %s" , finished .Sub (started ), 4 * time .Second ),
306+ )
307+ }
308+
309+ func testLoopDoSleepIncrementalMaxMethodZero (t * testing.T ) {
310+ t .Parallel ()
311+ l := & retry.Retry {
312+ Attempts : 50 ,
313+ Method : retry .IncrementalDelayMax (time .Second / 2 ),
314+ }
315+
316+ count := 0
317+ started := time .Now ()
318+ err := l .Do (func () error {
319+ count ++
320+ return assert .AnError
321+ })
322+ finished := time .Now ()
323+ assert .ErrorIs (t , err , assert .AnError )
324+ assert .Equal (t , l .Attempts , count )
325+
326+ expected := started .Add (time .Second )
327+ assert .True (t , finished .Before (expected ),
328+ fmt .Sprintf ("took (%s) more than expected: %s" , finished .Sub (started ), time .Second ),
237329 )
238330}
239331
0 commit comments