Skip to content

Commit 9b70e2b

Browse files
committed
Allow null fallback values to be passed through
Fixes #267
1 parent c93bf35 commit 9b70e2b

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,13 @@ ExecutionResult withNonResult() {
118118
* {@code this} if {@link #success} and {@link #result} are unchanged.
119119
*/
120120
public ExecutionResult withResult(Object result) {
121-
return success && ((this.result == null && result == null) || (this.result != null && this.result.equals(result))) ?
121+
boolean unchangedNull = this.result == null && result == null && failure == null;
122+
boolean unchangedNotNull = this.result != null && this.result.equals(result);
123+
return success && (unchangedNull || unchangedNotNull) ?
122124
this :
123125
new ExecutionResult(result, null, nonResult, waitNanos, true, true, successAll);
124126
}
125-
127+
126128
/**
127129
* Returns a copy of the ExecutionResult with {@code complete} set to false, else this if nothing has changed.
128130
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package net.jodah.failsafe.issues;
2+
3+
import net.jodah.failsafe.Failsafe;
4+
import net.jodah.failsafe.Fallback;
5+
import net.jodah.failsafe.Timeout;
6+
import net.jodah.failsafe.event.ExecutionAttemptedEvent;
7+
import org.testng.annotations.Test;
8+
9+
import java.net.ConnectException;
10+
import java.time.Duration;
11+
12+
import static org.testng.Assert.assertNull;
13+
14+
@Test
15+
public class Issue267Test {
16+
public void test() {
17+
Timeout<Object> timeout = Timeout.of(Duration.ofMillis(1000L));
18+
Fallback<Object> notFoundFallback = Fallback.of(this::handleNotFound).handleIf(this::causedBy404);
19+
Fallback<Object> failureHandling = Fallback.ofException(this::handleException);
20+
21+
Integer result = Failsafe.with(failureHandling, notFoundFallback, timeout).get(this::connect);
22+
assertNull(result);
23+
}
24+
25+
private Integer connect() throws ConnectException {
26+
throw new ConnectException();
27+
}
28+
29+
private boolean causedBy404(Object o, Throwable throwable) {
30+
return throwable instanceof ConnectException;
31+
}
32+
33+
private Object handleNotFound(ExecutionAttemptedEvent<?> event) {
34+
return null;
35+
}
36+
37+
private Exception handleException(ExecutionAttemptedEvent<?> event) {
38+
return new IllegalArgumentException();
39+
}
40+
}

0 commit comments

Comments
 (0)