|
20 | 20 | import org.junit.jupiter.api.Assertions;
|
21 | 21 | import org.junit.jupiter.api.Test;
|
22 | 22 | import org.junit.jupiter.api.function.Executable;
|
23 |
| -import software.amazon.cloudformation.proxy.delay.Blended; |
24 |
| -import software.amazon.cloudformation.proxy.delay.Constant; |
25 |
| -import software.amazon.cloudformation.proxy.delay.Exponential; |
26 |
| -import software.amazon.cloudformation.proxy.delay.MultipleOf; |
27 |
| -import software.amazon.cloudformation.proxy.delay.ShiftByMultipleOf; |
| 23 | +import software.amazon.cloudformation.proxy.delay.*; |
28 | 24 |
|
29 | 25 | public class DelayTest {
|
30 | 26 |
|
@@ -203,4 +199,50 @@ public void exponentialDelays() {
|
203 | 199 | }
|
204 | 200 | assertThat(3).isEqualTo(attempt);
|
205 | 201 | }
|
| 202 | + |
| 203 | + @Test |
| 204 | + public void cappedExponentialDelays() { |
| 205 | + Duration MAX_DELAY = Duration.ofSeconds(15); |
| 206 | + final Delay cappedExponential = CappedExponential.of() |
| 207 | + .timeout(Duration.ofMinutes(20)) |
| 208 | + .maxDelay(MAX_DELAY) |
| 209 | + .powerBy(1.3) |
| 210 | + .minDelay(Duration.ofSeconds(1)) |
| 211 | + .build(); |
| 212 | + int[] results = {1, 1, 1, 1, 2, 2, 3, 4, 6, 8, 10, 13, 15, 15, 15, 15}; |
| 213 | + for (int tries = 0; tries <= 15; tries++) { |
| 214 | + Duration delay = cappedExponential.nextDelay(tries); |
| 215 | + assertThat(results[tries]).isEqualTo((int) delay.getSeconds()); |
| 216 | + if (tries >= 12) { |
| 217 | + assertThat(MAX_DELAY.getSeconds()).isEqualTo(delay.getSeconds()); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + //If minDelay is not set, the retry is without delay. |
| 222 | + final Delay cappedExponentialNoDelay = CappedExponential.of().timeout(Duration.ofSeconds(12)).build(); |
| 223 | + for (int tries = 0; tries <= 15; tries++) { |
| 224 | + Duration delay = cappedExponentialNoDelay.nextDelay(tries); |
| 225 | + assertThat(0).isEqualTo((int) delay.getSeconds()); |
| 226 | + if (tries >= 12) { |
| 227 | + assertThat(0).isEqualTo(delay.getSeconds()); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + //If powerBy is not passed, it's set to default 2. |
| 232 | + final Delay cappedExponentialNoPower = CappedExponential.of() |
| 233 | + .timeout(Duration.ofMinutes(20)) |
| 234 | + .maxDelay(MAX_DELAY) |
| 235 | + .minDelay(Duration.ofSeconds(1)) |
| 236 | + .build(); |
| 237 | + |
| 238 | + int[] resultsNoPower = {1, 1, 2, 4, 8, 15, 15, 15, 15, 15}; |
| 239 | + for (int tries = 0; tries <= 6; tries++) { |
| 240 | + Duration delay = cappedExponentialNoPower.nextDelay(tries); |
| 241 | + assertThat(resultsNoPower[tries]).isEqualTo((int) delay.getSeconds()); |
| 242 | + if (tries >= 5) { |
| 243 | + assertThat(MAX_DELAY.getSeconds()).isEqualTo(delay.getSeconds()); |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + } |
206 | 248 | }
|
0 commit comments