Skip to content

Commit 939d46a

Browse files
authored
Updated nextDelay method to calculate accrued time for timeout condition (#430)
* Updated nextDelay method to calculate accrued time for timeout condition * Updated with pre-commit
1 parent eeff3cb commit 939d46a

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

src/main/java/software/amazon/cloudformation/proxy/delay/CappedExponential.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ public class CappedExponential extends MinDelayAbstractBase {
2323

2424
final Duration maxDelay;
2525

26-
private Duration accrued = Duration.ZERO;
27-
2826
CappedExponential(Duration timeout,
2927
Duration minDelay,
3028
Double powerBy,
@@ -68,19 +66,27 @@ public CappedExponential build() {
6866
}
6967
}
7068

69+
/**
70+
* Calculating accrued time as summation of all the delay based on attempt.
71+
* Assumption:- attempt will not be big number.
72+
*/
7173
@Override
7274
public Duration nextDelay(int attempt) {
7375
Duration next = Duration.ofSeconds(Math.round(Math.pow(powerBy, attempt)));
7476
Duration nextDelay = Duration.ofSeconds(Math.min(maxDelay.getSeconds(), next.getSeconds()));
77+
Duration accrued = Duration.ZERO;
78+
for (int i = 1; i <= attempt; i++) {
79+
Duration nextDuration = i > 1 ? nextDelay(i - 1) : Duration.ZERO;
80+
accrued = accrued.plus(nextDuration);
81+
}
7582
accrued = accrued.plus(nextDelay);
7683
return enforceBounds(accrued, nextDelay);
77-
7884
}
7985

8086
@Override
8187
public String toString() {
82-
return "CappedExponential{" + "powerBy=" + powerBy + ", maxDelay=" + maxDelay + ", accrued=" + accrued + ", minDelay="
83-
+ minDelay + ", timeout=" + timeout + '}';
88+
return "CappedExponential{" + "powerBy=" + powerBy + ", maxDelay=" + maxDelay + ", minDelay=" + minDelay + ", timeout="
89+
+ timeout + '}';
8490
}
8591

8692
}

src/test/java/software/amazon/cloudformation/proxy/DelayTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public void cappedExponentialDelays() {
206206
final Delay cappedExponential = CappedExponential.of().timeout(Duration.ofMinutes(20)).maxDelay(MAX_DELAY).powerBy(1.3)
207207
.minDelay(Duration.ofSeconds(1)).build();
208208
int[] results = { 1, 1, 2, 2, 3, 4, 5, 6, 8, 11, 14, 15, 15, 15, 15, 15, 15 };
209-
for (int tries = 0; tries <= 15; tries++) {
209+
for (int tries = 1; tries <= 15; tries++) {
210210
Duration delay = cappedExponential.nextDelay(tries);
211211
assertThat(results[tries]).isEqualTo((int) delay.getSeconds());
212212
if (tries >= 11) {
@@ -218,7 +218,7 @@ public void cappedExponentialDelays() {
218218
final Delay cappedExponentialNoPower = CappedExponential.of().timeout(Duration.ofMinutes(20)).maxDelay(MAX_DELAY)
219219
.minDelay(Duration.ofSeconds(2)).build();
220220
int[] resultsNoPower = { 2, 2, 4, 8, 15, 15, 15, 15, 15 };
221-
for (int tries = 0; tries <= 6; tries++) {
221+
for (int tries = 1; tries <= 6; tries++) {
222222
Duration delay = cappedExponentialNoPower.nextDelay(tries);
223223
assertThat(resultsNoPower[tries]).isEqualTo((int) delay.getSeconds());
224224
if (tries >= 5) {
@@ -230,20 +230,20 @@ public void cappedExponentialDelays() {
230230
final Delay cappedExponentialTimeout = CappedExponential.of().timeout(Duration.ofSeconds(5))
231231
.maxDelay(Duration.ofSeconds(1)).powerBy(1.0).minDelay(Duration.ofSeconds(1)).build();
232232

233-
int[] resultsTimeout = { 1, 1, 1, 1, 1, 0 };
234-
for (int tries = 0; tries <= 5; tries++) {
233+
int[] resultsTimeout = { 1, 1, 1, 1, 1, 1, 0 };
234+
for (int tries = 1; tries <= 6; tries++) {
235235
Duration delay = cappedExponentialTimeout.nextDelay(tries);
236236
assertThat(resultsTimeout[tries]).isEqualTo((int) delay.getSeconds());
237-
if (tries >= 5) {
237+
if (tries >= 6) {
238238
assertThat(0).isEqualTo(delay.getSeconds());
239239
}
240240
}
241241

242242
// If minDelay is not passed, it's set to default 1.
243243
final Delay cappedExponentialNoMinDelay = CappedExponential.of().timeout(Duration.ofSeconds(5))
244244
.maxDelay(Duration.ofSeconds(1)).powerBy(1.0).build();
245-
int[] resultsNoMinDelay = { 1, 1, 1, 1, 1, 0 };
246-
for (int tries = 0; tries <= 5; tries++) {
245+
int[] resultsNoMinDelay = { 1, 1, 1, 1, 1, 1, 0 };
246+
for (int tries = 1; tries <= 5; tries++) {
247247
Duration delay = cappedExponentialNoMinDelay.nextDelay(tries);
248248
assertThat(resultsNoMinDelay[tries]).isEqualTo((int) delay.getSeconds());
249249
}
@@ -252,18 +252,18 @@ public void cappedExponentialDelays() {
252252
final Delay cappedExponentialNoMaxDelay = CappedExponential.of().timeout(Duration.ofMinutes(20))
253253
.minDelay(Duration.ofSeconds(2)).build();
254254
int[] resultsNoMaxDelay = { 2, 2, 4, 8, 16, 20, 20, 20, 20 };
255-
for (int tries = 0; tries <= 6; tries++) {
255+
for (int tries = 1; tries <= 6; tries++) {
256256
Duration delay = cappedExponentialNoMaxDelay.nextDelay(tries);
257257
assertThat(resultsNoMaxDelay[tries]).isEqualTo((int) delay.getSeconds());
258258
}
259259

260260
final Delay cappedExponentialSameMinMaxDelay = CappedExponential.of().timeout(Duration.ofSeconds(5))
261261
.maxDelay(Duration.ofSeconds(1)).powerBy(1.3).minDelay(Duration.ofSeconds(1)).build();
262-
int[] resultsSameMinMaxDelay = { 1, 1, 1, 1, 1, 0 };
263-
for (int tries = 0; tries <= 5; tries++) {
262+
int[] resultsSameMinMaxDelay = { 1, 1, 1, 1, 1, 1, 0 };
263+
for (int tries = 1; tries <= 6; tries++) {
264264
Duration delay = cappedExponentialSameMinMaxDelay.nextDelay(tries);
265265
assertThat(resultsSameMinMaxDelay[tries]).isEqualTo((int) delay.getSeconds());
266-
if (tries >= 5) {
266+
if (tries >= 6) {
267267
assertThat(0).isEqualTo(delay.getSeconds());
268268
}
269269
}

0 commit comments

Comments
 (0)