-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathRetryStrategiesTest.java
More file actions
514 lines (410 loc) · 21.4 KB
/
Copy pathRetryStrategiesTest.java
File metadata and controls
514 lines (410 loc) · 21.4 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package software.amazon.lambda.durable.retry;
import static org.junit.jupiter.api.Assertions.*;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import software.amazon.lambda.durable.config.StepConfig;
class RetryStrategiesTest {
@Test
void testNoRetryPreset() {
var strategy = RetryStrategies.Presets.NO_RETRY;
// Should never retry regardless of attempt number
var decision1 = strategy.makeRetryDecision(new RuntimeException("test"), 1);
var decision2 = strategy.makeRetryDecision(new RuntimeException("test"), 2);
var decision3 = strategy.makeRetryDecision(new RuntimeException("test"), 6);
assertFalse(decision1.shouldRetry());
assertFalse(decision2.shouldRetry());
assertFalse(decision3.shouldRetry());
}
@Test
void testDefaultPresetConfiguration() {
var strategy = RetryStrategies.Presets.DEFAULT;
// Should retry for first 5 attempts (0-4), fail on 6th (5)
for (int attempt = 1; attempt <= 5; attempt++) {
var decision = strategy.makeRetryDecision(new RuntimeException("test"), attempt);
assertTrue(decision.shouldRetry(), "Should retry on attempt " + attempt);
assertTrue(decision.delay().toSeconds() >= 1, "Delay should be at least 1 second");
}
// Should not retry on 6th attempt (attempt number 5)
var finalDecision = strategy.makeRetryDecision(new RuntimeException("test"), 6);
assertFalse(finalDecision.shouldRetry());
}
@Test
void testExponentialBackoffDelayCalculation() {
// Test with no jitter to verify exact calculation
var strategy = RetryStrategies.exponentialBackoff(
5, // maxAttempts
Duration.ofSeconds(2), // initialDelay
Duration.ofSeconds(60), // maxDelay
2.0, // backoffRate
JitterStrategy.NONE // no jitter for predictable testing
);
// Verify delay calculation: initialDelay * backoffRate^attemptNumber
var decision0 = strategy.makeRetryDecision(new RuntimeException("test"), 1);
assertEquals(2, decision0.delay().toSeconds()); // 2 * 2^0 = 2
var decision1 = strategy.makeRetryDecision(new RuntimeException("test"), 2);
assertEquals(4, decision1.delay().toSeconds()); // 2 * 2^1 = 4
var decision2 = strategy.makeRetryDecision(new RuntimeException("test"), 3);
assertEquals(8, decision2.delay().toSeconds()); // 2 * 2^2 = 8
var decision3 = strategy.makeRetryDecision(new RuntimeException("test"), 4);
assertEquals(16, decision3.delay().toSeconds()); // 2 * 2^3 = 16
}
@Test
void testMaxDelayCapping() {
var strategy = RetryStrategies.exponentialBackoff(
10, // maxAttempts
Duration.ofSeconds(5), // initialDelay
Duration.ofSeconds(20), // maxDelay (cap at 20 seconds)
2.0, // backoffRate
JitterStrategy.NONE // no jitter
);
// Should be capped at maxDelay
var decision = strategy.makeRetryDecision(new RuntimeException("test"), 6);
assertEquals(20, decision.delay().toSeconds()); // Would be 5 * 2^5 = 160, but capped at 20
}
@Test
void testJitterStrategies() {
var noneStrategy = RetryStrategies.exponentialBackoff(
5, Duration.ofSeconds(10), Duration.ofSeconds(60), 2.0, JitterStrategy.NONE);
var fullStrategy = RetryStrategies.exponentialBackoff(
5, Duration.ofSeconds(10), Duration.ofSeconds(60), 2.0, JitterStrategy.FULL);
var halfStrategy = RetryStrategies.exponentialBackoff(
5, Duration.ofSeconds(10), Duration.ofSeconds(60), 2.0, JitterStrategy.HALF);
// Test multiple times due to randomness
for (int i = 0; i < 10; i++) {
var noneDecision = noneStrategy.makeRetryDecision(new RuntimeException("test"), 2);
var fullDecision = fullStrategy.makeRetryDecision(new RuntimeException("test"), 2);
var halfDecision = halfStrategy.makeRetryDecision(new RuntimeException("test"), 2);
// NONE should always be exactly 20 (10 * 2^1)
assertEquals(20, noneDecision.delay().toSeconds());
// FULL should be between 1 and 20 (0 to baseDelay, minimum 1)
var fullDelay = fullDecision.delay().toSeconds();
assertTrue(fullDelay >= 1 && fullDelay <= 20);
// HALF should be between 10 and 20 (baseDelay/2 to baseDelay)
var halfDelay = halfDecision.delay().toSeconds();
assertTrue(halfDelay >= 10 && halfDelay <= 20);
}
}
@Test
void testMinimumDelayOfOneSecond() {
// Test that delays are properly calculated with 1-second minimum
var strategy = RetryStrategies.exponentialBackoff(
5, Duration.ofSeconds(1), Duration.ofSeconds(60), 1.0, JitterStrategy.FULL);
var decision = strategy.makeRetryDecision(new RuntimeException("test"), 1);
assertTrue(decision.delay().toSeconds() >= 1, "Delay should be at least 1 second");
}
@Test
void testFixedDelayStrategy() {
var strategy = RetryStrategies.fixedDelay(3, Duration.ofSeconds(5));
// Should retry with fixed delay for first 2 attempts
var decision1 = strategy.makeRetryDecision(new RuntimeException("test"), 1);
var decision2 = strategy.makeRetryDecision(new RuntimeException("test"), 2);
assertTrue(decision1.shouldRetry());
assertTrue(decision2.shouldRetry());
assertEquals(5, decision1.delay().toSeconds());
assertEquals(5, decision2.delay().toSeconds());
// Should not retry on 3rd attempt
var decision3 = strategy.makeRetryDecision(new RuntimeException("test"), 3);
assertFalse(decision3.shouldRetry());
}
@Test
void linearBackoff_withCustomDelays_shouldIncreaseByIncrement() {
var strategy = RetryStrategies.linearBackoff(5, Duration.ofSeconds(2), Duration.ofSeconds(3));
var decision1 = strategy.makeRetryDecision(new RuntimeException("test"), 1);
var decision2 = strategy.makeRetryDecision(new RuntimeException("test"), 2);
var decision3 = strategy.makeRetryDecision(new RuntimeException("test"), 3);
var decision4 = strategy.makeRetryDecision(new RuntimeException("test"), 4);
assertTrue(decision1.shouldRetry());
assertEquals(Duration.ofSeconds(2), decision1.delay());
assertTrue(decision2.shouldRetry());
assertEquals(Duration.ofSeconds(5), decision2.delay());
assertTrue(decision3.shouldRetry());
assertEquals(Duration.ofSeconds(8), decision3.delay());
assertTrue(decision4.shouldRetry());
assertEquals(Duration.ofSeconds(11), decision4.delay());
}
@Test
void linearBackoff_withOldOverload_shouldRemainUncappedAndDeterministic() {
var strategy = RetryStrategies.linearBackoff(5, Duration.ofSeconds(10), Duration.ofSeconds(10));
assertEquals(
Duration.ofSeconds(10),
strategy.makeRetryDecision(new RuntimeException("test"), 1).delay());
assertEquals(
Duration.ofSeconds(20),
strategy.makeRetryDecision(new RuntimeException("test"), 2).delay());
assertEquals(
Duration.ofSeconds(30),
strategy.makeRetryDecision(new RuntimeException("test"), 3).delay());
assertEquals(
Duration.ofSeconds(40),
strategy.makeRetryDecision(new RuntimeException("test"), 4).delay());
}
@Test
void linearBackoff_withMaxDelay_shouldCapDelay() {
var strategy = RetryStrategies.linearBackoff(
6, Duration.ofSeconds(2), Duration.ofSeconds(7), Duration.ofSeconds(3), JitterStrategy.NONE);
assertEquals(
Duration.ofSeconds(2),
strategy.makeRetryDecision(new RuntimeException("test"), 1).delay());
assertEquals(
Duration.ofSeconds(5),
strategy.makeRetryDecision(new RuntimeException("test"), 2).delay());
assertEquals(
Duration.ofSeconds(7),
strategy.makeRetryDecision(new RuntimeException("test"), 3).delay());
assertEquals(
Duration.ofSeconds(7),
strategy.makeRetryDecision(new RuntimeException("test"), 4).delay());
}
@Test
void linearBackoff_withMaxDelay_shouldCapBeforeOverflow() {
var strategy = RetryStrategies.linearBackoff(
Integer.MAX_VALUE,
Duration.ofSeconds(1),
Duration.ofSeconds(5),
Duration.ofSeconds(Long.MAX_VALUE),
JitterStrategy.NONE);
var decision = assertDoesNotThrow(() -> strategy.makeRetryDecision(new RuntimeException("test"), 2));
assertTrue(decision.shouldRetry());
assertEquals(Duration.ofSeconds(5), decision.delay());
}
@Test
void linearBackoff_withNewOverload_shouldStopAtMaxAttempts() {
var strategy = RetryStrategies.linearBackoff(
3, Duration.ofSeconds(1), Duration.ofSeconds(5), Duration.ofSeconds(1), JitterStrategy.NONE);
var decision1 = strategy.makeRetryDecision(new RuntimeException("test"), 1);
var decision2 = strategy.makeRetryDecision(new RuntimeException("test"), 2);
var decision3 = strategy.makeRetryDecision(new RuntimeException("test"), 3);
assertTrue(decision1.shouldRetry());
assertEquals(Duration.ofSeconds(1), decision1.delay());
assertTrue(decision2.shouldRetry());
assertEquals(Duration.ofSeconds(2), decision2.delay());
assertFalse(decision3.shouldRetry());
}
@Test
void linearBackoff_withMaxDelayLessThanInitialDelay_shouldCapFirstRetry() {
var strategy = RetryStrategies.linearBackoff(
4, Duration.ofSeconds(10), Duration.ofSeconds(5), Duration.ofSeconds(3), JitterStrategy.NONE);
assertEquals(
Duration.ofSeconds(5),
strategy.makeRetryDecision(new RuntimeException("test"), 1).delay());
assertEquals(
Duration.ofSeconds(5),
strategy.makeRetryDecision(new RuntimeException("test"), 2).delay());
}
@Test
void linearBackoff_withJitter_shouldProduceDelayInExpectedRange() {
var fullStrategy = RetryStrategies.linearBackoff(
5, Duration.ofSeconds(10), Duration.ofSeconds(15), Duration.ofSeconds(10), JitterStrategy.FULL);
var halfStrategy = RetryStrategies.linearBackoff(
5, Duration.ofSeconds(10), Duration.ofSeconds(15), Duration.ofSeconds(10), JitterStrategy.HALF);
for (int i = 0; i < 10; i++) {
var fullDelay = fullStrategy
.makeRetryDecision(new RuntimeException("test"), 2)
.delay()
.toSeconds();
assertTrue(fullDelay >= 1 && fullDelay <= 15);
var halfDelay = halfStrategy
.makeRetryDecision(new RuntimeException("test"), 2)
.delay()
.toSeconds();
assertTrue(halfDelay >= 8 && halfDelay <= 15);
}
}
@Test
void linearPreset_shouldUseOneThroughFiveSecondDelays() {
var strategy = RetryStrategies.Presets.LINEAR;
for (int attempt = 1; attempt <= 5; attempt++) {
var decision = strategy.makeRetryDecision(new RuntimeException("test"), attempt);
assertTrue(decision.shouldRetry(), "Should retry on attempt " + attempt);
assertEquals(Duration.ofSeconds(attempt), decision.delay());
}
var finalDecision = strategy.makeRetryDecision(new RuntimeException("test"), 6);
assertFalse(finalDecision.shouldRetry());
}
@Test
void linearBackoff_shouldStopAtMaxAttempts() {
var strategy = RetryStrategies.linearBackoff(3, Duration.ofSeconds(1), Duration.ofSeconds(1));
var decision1 = strategy.makeRetryDecision(new RuntimeException("test"), 1);
var decision2 = strategy.makeRetryDecision(new RuntimeException("test"), 2);
var decision3 = strategy.makeRetryDecision(new RuntimeException("test"), 3);
assertTrue(decision1.shouldRetry());
assertTrue(decision2.shouldRetry());
assertFalse(decision3.shouldRetry());
}
@Test
void linearBackoff_withInvalidMaxAttempts_shouldThrow() {
var exception = assertThrows(
IllegalArgumentException.class,
() -> RetryStrategies.linearBackoff(0, Duration.ofSeconds(1), Duration.ofSeconds(1)));
assertTrue(exception.getMessage().contains("maxAttempts"));
assertTrue(exception.getMessage().contains("positive"));
}
@Test
void linearBackoff_withSubSecondInitialDelay_shouldThrow() {
var exception = assertThrows(
IllegalArgumentException.class,
() -> RetryStrategies.linearBackoff(3, Duration.ofMillis(500), Duration.ofSeconds(1)));
assertTrue(exception.getMessage().contains("initialDelay"));
assertTrue(exception.getMessage().contains("at least 1 second"));
}
@Test
void linearBackoff_withNullInitialDelay_shouldThrow() {
var exception = assertThrows(
IllegalArgumentException.class, () -> RetryStrategies.linearBackoff(3, null, Duration.ofSeconds(1)));
assertTrue(exception.getMessage().contains("initialDelay"));
assertTrue(exception.getMessage().contains("cannot be null"));
}
@Test
void linearBackoff_withSubSecondIncrement_shouldThrow() {
var exception = assertThrows(
IllegalArgumentException.class,
() -> RetryStrategies.linearBackoff(3, Duration.ofSeconds(1), Duration.ofMillis(500)));
assertTrue(exception.getMessage().contains("increment"));
assertTrue(exception.getMessage().contains("at least 1 second"));
}
@Test
void linearBackoff_withNullIncrement_shouldThrow() {
var exception = assertThrows(
IllegalArgumentException.class, () -> RetryStrategies.linearBackoff(3, Duration.ofSeconds(1), null));
assertTrue(exception.getMessage().contains("increment"));
assertTrue(exception.getMessage().contains("cannot be null"));
}
@Test
void linearBackoff_withSubSecondMaxDelay_shouldThrow() {
var exception = assertThrows(
IllegalArgumentException.class,
() -> RetryStrategies.linearBackoff(
3, Duration.ofSeconds(1), Duration.ofMillis(500), Duration.ofSeconds(1), JitterStrategy.NONE));
assertTrue(exception.getMessage().contains("maxDelay"));
assertTrue(exception.getMessage().contains("at least 1 second"));
}
@Test
void linearBackoff_withNullMaxDelay_shouldThrow() {
var exception = assertThrows(
IllegalArgumentException.class,
() -> RetryStrategies.linearBackoff(
3, Duration.ofSeconds(1), null, Duration.ofSeconds(1), JitterStrategy.NONE));
assertTrue(exception.getMessage().contains("maxDelay"));
assertTrue(exception.getMessage().contains("cannot be null"));
}
@Test
void linearBackoff_withNullJitter_shouldThrow() {
var exception = assertThrows(
IllegalArgumentException.class,
() -> RetryStrategies.linearBackoff(
3, Duration.ofSeconds(1), Duration.ofSeconds(5), Duration.ofSeconds(1), null));
assertTrue(exception.getMessage().contains("jitter"));
assertTrue(exception.getMessage().contains("cannot be null"));
}
@Test
void testInvalidParameters() {
assertThrows(
IllegalArgumentException.class,
() -> RetryStrategies.exponentialBackoff(
0, Duration.ofSeconds(1), Duration.ofSeconds(10), 2.0, JitterStrategy.NONE));
assertThrows(
IllegalArgumentException.class,
() -> RetryStrategies.exponentialBackoff(
5, Duration.ofSeconds(-1), Duration.ofSeconds(10), 2.0, JitterStrategy.NONE));
assertThrows(
IllegalArgumentException.class,
() -> RetryStrategies.exponentialBackoff(
5, Duration.ofSeconds(1), Duration.ofSeconds(-1), 2.0, JitterStrategy.NONE));
assertThrows(
IllegalArgumentException.class,
() -> RetryStrategies.exponentialBackoff(
5, Duration.ofSeconds(1), Duration.ofSeconds(10), 0, JitterStrategy.NONE));
assertThrows(IllegalArgumentException.class, () -> RetryStrategies.fixedDelay(0, Duration.ofSeconds(1)));
assertThrows(IllegalArgumentException.class, () -> RetryStrategies.fixedDelay(5, Duration.ofSeconds(-1)));
}
@Test
void exponentialBackoff_withSubSecondInitialDelay_shouldThrow() {
var exception = assertThrows(
IllegalArgumentException.class,
() -> RetryStrategies.exponentialBackoff(
3, Duration.ofMillis(500), Duration.ofSeconds(60), 2.0, JitterStrategy.NONE));
assertTrue(exception.getMessage().contains("initialDelay"));
assertTrue(exception.getMessage().contains("at least 1 second"));
}
@Test
void exponentialBackoff_withSubSecondMaxDelay_shouldThrow() {
var exception = assertThrows(
IllegalArgumentException.class,
() -> RetryStrategies.exponentialBackoff(
3, Duration.ofSeconds(5), Duration.ofMillis(999), 2.0, JitterStrategy.NONE));
assertTrue(exception.getMessage().contains("maxDelay"));
assertTrue(exception.getMessage().contains("at least 1 second"));
}
@Test
void exponentialBackoff_withNullInitialDelay_shouldThrow() {
var exception = assertThrows(
IllegalArgumentException.class,
() -> RetryStrategies.exponentialBackoff(3, null, Duration.ofSeconds(60), 2.0, JitterStrategy.NONE));
assertTrue(exception.getMessage().contains("initialDelay"));
assertTrue(exception.getMessage().contains("cannot be null"));
}
@Test
void exponentialBackoff_withNullMaxDelay_shouldThrow() {
var exception = assertThrows(
IllegalArgumentException.class,
() -> RetryStrategies.exponentialBackoff(3, Duration.ofSeconds(5), null, 2.0, JitterStrategy.NONE));
assertTrue(exception.getMessage().contains("maxDelay"));
assertTrue(exception.getMessage().contains("cannot be null"));
}
@Test
void fixedDelay_withSubSecondDelay_shouldThrow() {
var exception = assertThrows(
IllegalArgumentException.class, () -> RetryStrategies.fixedDelay(3, Duration.ofMillis(500)));
assertTrue(exception.getMessage().contains("fixedDelay"));
assertTrue(exception.getMessage().contains("at least 1 second"));
}
@Test
void fixedDelay_withNullDelay_shouldThrow() {
var exception = assertThrows(IllegalArgumentException.class, () -> RetryStrategies.fixedDelay(3, null));
assertTrue(exception.getMessage().contains("fixedDelay"));
assertTrue(exception.getMessage().contains("cannot be null"));
}
@Test
void testStepConfigWithRetryStrategy() {
var config1 = StepConfig.builder()
.retryStrategy(RetryStrategies.Presets.DEFAULT)
.build();
var config2 = StepConfig.builder()
.retryStrategy(RetryStrategies.Presets.NO_RETRY)
.build();
var config3 = StepConfig.builder()
.retryStrategy(RetryStrategies.exponentialBackoff(
3, Duration.ofSeconds(1), Duration.ofSeconds(10), 2.0, JitterStrategy.NONE))
.build();
assertNotNull(config1.retryStrategy());
assertNotNull(config2.retryStrategy());
assertNotNull(config3.retryStrategy());
var decision1 = config1.retryStrategy().makeRetryDecision(new RuntimeException("test"), 1);
var decision2 = config2.retryStrategy().makeRetryDecision(new RuntimeException("test"), 1);
var decision3 = config3.retryStrategy().makeRetryDecision(new RuntimeException("test"), 1);
assertTrue(decision1.shouldRetry());
assertFalse(decision2.shouldRetry());
assertTrue(decision3.shouldRetry());
}
@Test
void testRetryStrategyDelayProgression() {
var strategy = RetryStrategies.exponentialBackoff(
5, Duration.ofSeconds(2), Duration.ofSeconds(60), 2.0, JitterStrategy.NONE);
var decision0 = strategy.makeRetryDecision(new RuntimeException("test"), 1);
var decision1 = strategy.makeRetryDecision(new RuntimeException("test"), 2);
var decision2 = strategy.makeRetryDecision(new RuntimeException("test"), 3);
var decision3 = strategy.makeRetryDecision(new RuntimeException("test"), 4);
var decision4 = strategy.makeRetryDecision(new RuntimeException("test"), 5);
assertTrue(decision0.shouldRetry());
assertEquals(2, decision0.delay().toSeconds());
assertTrue(decision1.shouldRetry());
assertEquals(4, decision1.delay().toSeconds());
assertTrue(decision2.shouldRetry());
assertEquals(8, decision2.delay().toSeconds());
assertTrue(decision3.shouldRetry());
assertEquals(16, decision3.delay().toSeconds());
assertFalse(decision4.shouldRetry());
}
}