Skip to content

Commit

Permalink
Allow null fallback values to be passed through
Browse files Browse the repository at this point in the history
Fixes #267
  • Loading branch information
jhalterman committed Jul 28, 2021
1 parent c93bf35 commit 9b70e2b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/main/java/net/jodah/failsafe/ExecutionResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,13 @@ ExecutionResult withNonResult() {
* {@code this} if {@link #success} and {@link #result} are unchanged.
*/
public ExecutionResult withResult(Object result) {
return success && ((this.result == null && result == null) || (this.result != null && this.result.equals(result))) ?
boolean unchangedNull = this.result == null && result == null && failure == null;
boolean unchangedNotNull = this.result != null && this.result.equals(result);
return success && (unchangedNull || unchangedNotNull) ?
this :
new ExecutionResult(result, null, nonResult, waitNanos, true, true, successAll);
}

/**
* Returns a copy of the ExecutionResult with {@code complete} set to false, else this if nothing has changed.
*/
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/net/jodah/failsafe/issues/Issue267Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.jodah.failsafe.issues;

import net.jodah.failsafe.Failsafe;
import net.jodah.failsafe.Fallback;
import net.jodah.failsafe.Timeout;
import net.jodah.failsafe.event.ExecutionAttemptedEvent;
import org.testng.annotations.Test;

import java.net.ConnectException;
import java.time.Duration;

import static org.testng.Assert.assertNull;

@Test
public class Issue267Test {
public void test() {
Timeout<Object> timeout = Timeout.of(Duration.ofMillis(1000L));
Fallback<Object> notFoundFallback = Fallback.of(this::handleNotFound).handleIf(this::causedBy404);
Fallback<Object> failureHandling = Fallback.ofException(this::handleException);

Integer result = Failsafe.with(failureHandling, notFoundFallback, timeout).get(this::connect);
assertNull(result);
}

private Integer connect() throws ConnectException {
throw new ConnectException();
}

private boolean causedBy404(Object o, Throwable throwable) {
return throwable instanceof ConnectException;
}

private Object handleNotFound(ExecutionAttemptedEvent<?> event) {
return null;
}

private Exception handleException(ExecutionAttemptedEvent<?> event) {
return new IllegalArgumentException();
}
}

0 comments on commit 9b70e2b

Please sign in to comment.