Skip to content

Commit c93bf35

Browse files
committed
Change the default internal waitNanos for retries to 0
Changes the default internal waitNanos for retries to 0. This is necessary since returning -1 nanos will convert, via TimeUnit, to 0 millis in some JREs, but not all.
1 parent 61e82fe commit c93bf35

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/main/java/net/jodah/failsafe/RetryPolicyExecutor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class RetryPolicyExecutor extends PolicyExecutor<RetryPolicy> {
3737
private volatile int failedAttempts;
3838
private volatile boolean retriesExceeded;
3939
/** The fixed, backoff, random or computed delay time in nanoseconds. */
40-
private volatile long delayNanos = -1;
40+
private volatile long delayNanos;
4141

4242
// Listeners
4343
private final EventListener abortListener;
@@ -221,7 +221,7 @@ private long getFixedOrRandomDelayNanos(long waitNanos) {
221221
Duration delayMin = policy.getDelayMin();
222222
Duration delayMax = policy.getDelayMax();
223223

224-
if (waitNanos == -1 && delay != null && !delay.equals(Duration.ZERO))
224+
if (waitNanos == 0 && delay != null && !delay.equals(Duration.ZERO))
225225
waitNanos = delay.toNanos();
226226
else if (delayMin != null && delayMax != null)
227227
waitNanos = randomDelayInRange(delayMin.toNanos(), delayMax.toNanos(), Math.random());

src/test/java/net/jodah/failsafe/AsyncFailsafeTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,20 @@ public void shouldSkipExecutionWhenCircuitOpen() {
539539
assertThrows(future::get, ExecutionException.class, CircuitBreakerOpenException.class);
540540
}
541541

542+
public void testRetryPolicyScheduledDelayIsZero() throws Throwable {
543+
RetryPolicy<Object> rp = new RetryPolicy<>().onRetryScheduled(e -> {
544+
assertEquals(e.getDelay().toMillis(), 0);
545+
System.out.println(e.getDelay().toMillis());
546+
waiter.resume();
547+
});
548+
549+
Failsafe.with(rp).runAsync(() -> {
550+
throw new IllegalStateException();
551+
});
552+
553+
waiter.await(1000);
554+
}
555+
542556
private void assertInterruptedExceptionOnCancel(FailsafeExecutor<Boolean> failsafe) throws Throwable {
543557
CompletableFuture<Void> future = failsafe.runAsync(() -> {
544558
try {

0 commit comments

Comments
 (0)