Skip to content

Commit 4cd61f1

Browse files
committed
Fix delays between manually triggered async execution retries.
Fixes #242.
1 parent deb1c2f commit 4cd61f1

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public abstract class AbstractExecution extends ExecutionContext {
4242
/* Whether the execution has been interrupted */
4343
volatile boolean interrupted;
4444
/* The wait time in nanoseconds. */
45-
private volatile long waitNanos;
45+
volatile long waitNanos;
4646
/* Whether the execution has been completed */
4747
volatile boolean completed;
4848

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ void executeAsync(boolean asyncExecution) {
196196
*/
197197
boolean completeOrHandle(Object result, Throwable failure) {
198198
synchronized (future) {
199-
ExecutionResult er = new ExecutionResult(result, failure);
199+
ExecutionResult er = new ExecutionResult(result, failure).withWaitNanos(waitNanos);
200200
if (!completeCalled)
201201
record(er);
202202
completeCalled = true;

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ public boolean isSuccess() {
100100
}
101101

102102
/**
103-
* Returns a copy of the ExecutionResult with a non-result, and completed and success set to true. Returns
104-
* {@code this} if {@link #success} and {@link #result} are unchanged.
103+
* Returns a copy of the ExecutionResult with a non-result, and completed and success set to true. Returns {@code
104+
* this} if {@link #success} and {@link #result} are unchanged.
105105
*/
106106
ExecutionResult withNonResult() {
107107
return success && this.result == null && nonResult ?
@@ -136,6 +136,15 @@ ExecutionResult with(boolean completed, boolean success) {
136136
successAll == null ? success : success && successAll);
137137
}
138138

139+
/**
140+
* Returns a copy of the ExecutionResult with the {@code waitNanos} value.
141+
*/
142+
public ExecutionResult withWaitNanos(long waitNanos) {
143+
return this.waitNanos == waitNanos ?
144+
this :
145+
new ExecutionResult(result, failure, nonResult, waitNanos, complete, success, successAll);
146+
}
147+
139148
/**
140149
* Returns a copy of the ExecutionResult with the {@code waitNanos}, {@code completed} and {@code success} values.
141150
*/
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package net.jodah.failsafe.issues;
2+
3+
import net.jodah.failsafe.Failsafe;
4+
import net.jodah.failsafe.RetryPolicy;
5+
import org.testng.annotations.Test;
6+
7+
import java.time.Duration;
8+
9+
import static org.testng.Assert.assertTrue;
10+
11+
@Test
12+
public class Issue242Test {
13+
public void test() throws Throwable {
14+
RetryPolicy<String> retryPolicy = new RetryPolicy<String>().handleResult(null)
15+
.withDelay(Duration.ofMillis(100))
16+
.withMaxAttempts(3);
17+
18+
long startTime = System.currentTimeMillis();
19+
Failsafe.with(retryPolicy).runAsyncExecution(exec -> {
20+
if (!exec.complete(null, null))
21+
exec.retry();
22+
}).get();
23+
assertTrue(System.currentTimeMillis() - startTime > 200, "Expected delay between retries");
24+
}
25+
}

0 commit comments

Comments
 (0)