@Test public void worksInGeneral() { ExponentialTaskRetryPolicy policy = new ExponentialTaskRetryPolicy() .setMaxCount(9999).setDelay(Duration.ofSeconds(5)).setMultiplier(2); Task task = new Task().setProcessingTriesCount(200); final TestClock testClock = new TestClock(); TwContextClockHolder.setClock(testClock); task.setProcessingTriesCount(1); Instant retryTime = policy.getRetryTime(task, null).toInstant(); assertThat(retryTime).isEqualTo(testClock.instant().plus(Duration.ofSeconds(5))); task.setProcessingTriesCount(2); retryTime = policy.getRetryTime(task, null).toInstant(); assertThat(retryTime).isEqualTo(testClock.instant().plus(Duration.ofSeconds(10))); }
@Override public ZonedDateTime getRetryTime(ITask taskForProcessing, Throwable t) { long triesCount = taskForProcessing.getProcessingTriesCount() + triesDelta; if (triesCount >= maxCount) { return null; } long addedTimeMs; try { if (triesCount > Integer.MAX_VALUE) { addedTimeMs = maxDelay.toMillis(); } else { addedTimeMs = LongMath.checkedMultiply(delay.toMillis(), LongMath.checkedPow(multiplier, (int) triesCount - 1)); if (addedTimeMs > maxDelay.toMillis()) { addedTimeMs = maxDelay.toMillis(); } } } catch (ArithmeticException ignored) { addedTimeMs = maxDelay.toMillis(); } return ZonedDateTime.now(TwContextClockHolder.getClock()).plus(addedTimeMs, ChronoUnit.MILLIS); }
ExponentialTaskRetryPolicy implements ITaskRetryPolicy { @Override public ZonedDateTime getRetryTime(ITask taskForProcessing, Throwable t) { long triesCount = taskForProcessing.getProcessingTriesCount() + triesDelta; if (triesCount >= maxCount) { return null; } long addedTimeMs; try { if (triesCount > Integer.MAX_VALUE) { addedTimeMs = maxDelay.toMillis(); } else { addedTimeMs = LongMath.checkedMultiply(delay.toMillis(), LongMath.checkedPow(multiplier, (int) triesCount - 1)); if (addedTimeMs > maxDelay.toMillis()) { addedTimeMs = maxDelay.toMillis(); } } } catch (ArithmeticException ignored) { addedTimeMs = maxDelay.toMillis(); } return ZonedDateTime.now(TwContextClockHolder.getClock()).plus(addedTimeMs, ChronoUnit.MILLIS); } }
ExponentialTaskRetryPolicy implements ITaskRetryPolicy { @Override public ZonedDateTime getRetryTime(ITask taskForProcessing, Throwable t) { long triesCount = taskForProcessing.getProcessingTriesCount() + triesDelta; if (triesCount >= maxCount) { return null; } long addedTimeMs; try { if (triesCount > Integer.MAX_VALUE) { addedTimeMs = maxDelay.toMillis(); } else { addedTimeMs = LongMath.checkedMultiply(delay.toMillis(), LongMath.checkedPow(multiplier, (int) triesCount - 1)); if (addedTimeMs > maxDelay.toMillis()) { addedTimeMs = maxDelay.toMillis(); } } } catch (ArithmeticException ignored) { addedTimeMs = maxDelay.toMillis(); } return ZonedDateTime.now(TwContextClockHolder.getClock()).plus(addedTimeMs, ChronoUnit.MILLIS); }  }
ExponentialTaskRetryPolicy implements ITaskRetryPolicy { @Override public ZonedDateTime getRetryTime(ITask taskForProcessing, Throwable t) { long triesCount = taskForProcessing.getProcessingTriesCount() + triesDelta; if (triesCount >= maxCount) { return null; } long addedTimeMs; try { if (triesCount > Integer.MAX_VALUE) { addedTimeMs = maxDelay.toMillis(); } else { addedTimeMs = LongMath.checkedMultiply(delay.toMillis(), LongMath.checkedPow(multiplier, (int) triesCount - 1)); if (addedTimeMs > maxDelay.toMillis()) { addedTimeMs = maxDelay.toMillis(); } } } catch (ArithmeticException ignored) { addedTimeMs = maxDelay.toMillis(); } return ZonedDateTime.now(TwContextClockHolder.getClock()).plus(addedTimeMs, ChronoUnit.MILLIS); }  @Override ZonedDateTime getRetryTime(ITask taskForProcessing, Throwable t); }
ExponentialTaskRetryPolicy implements ITaskRetryPolicy { @Override public ZonedDateTime getRetryTime(ITask taskForProcessing, Throwable t) { long triesCount = taskForProcessing.getProcessingTriesCount() + triesDelta; if (triesCount >= maxCount) { return null; } long addedTimeMs; try { if (triesCount > Integer.MAX_VALUE) { addedTimeMs = maxDelay.toMillis(); } else { addedTimeMs = LongMath.checkedMultiply(delay.toMillis(), LongMath.checkedPow(multiplier, (int) triesCount - 1)); if (addedTimeMs > maxDelay.toMillis()) { addedTimeMs = maxDelay.toMillis(); } } } catch (ArithmeticException ignored) { addedTimeMs = maxDelay.toMillis(); } return ZonedDateTime.now(TwContextClockHolder.getClock()).plus(addedTimeMs, ChronoUnit.MILLIS); }  @Override ZonedDateTime getRetryTime(ITask taskForProcessing, Throwable t);  }