Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a typed parameter for the failure in FailurePolicy.handleIf #288

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/java/net/jodah/failsafe/FailurePolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ public S handle(List<Class<? extends Throwable>> failures) {
/**
* Specifies that a failure has occurred if the {@code failurePredicate} matches the failure.
*
* @param <T> failure type
* @throws NullPointerException if {@code failurePredicate} is null
*/
public S handleIf(Predicate<? extends Throwable> failurePredicate) {
public <T extends Throwable> S handleIf(Predicate<T> failurePredicate) {
Assert.notNull(failurePredicate, "failurePredicate");
failuresChecked = true;
failureConditions.add(failurePredicateFor(failurePredicate));
Expand All @@ -97,10 +98,11 @@ public S handleIf(Predicate<? extends Throwable> failurePredicate) {
/**
* Specifies that a failure has occurred if the {@code resultPredicate} matches the execution result.
*
* @param <T> failure type
* @throws NullPointerException if {@code resultPredicate} is null
*/
@SuppressWarnings("unchecked")
public S handleIf(BiPredicate<R, ? extends Throwable> resultPredicate) {
public <T extends Throwable> S handleIf(BiPredicate<R, T> resultPredicate) {
Assert.notNull(resultPredicate, "resultPredicate");
failuresChecked = true;
failureConditions.add((BiPredicate<R, Throwable>) resultPredicate);
Expand Down
48 changes: 33 additions & 15 deletions src/test/java/net/jodah/failsafe/RetryPolicyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,22 @@

@Test
public class RetryPolicyTest {
static class FooException extends Exception {
String bar;

FooException(String bar) {
this.bar = bar;
}
}

public void testIsFailureNull() {
RetryPolicy<Object> policy = new RetryPolicy<>();
assertFalse(policy.isFailure(null, null));
}

public void testIsFailureCompletionPredicate() {
RetryPolicy<Object> policy = new RetryPolicy<>()
.handleIf((result, failure) -> result == "test" || failure instanceof IllegalArgumentException);
RetryPolicy<Object> policy = new RetryPolicy<>().handleIf(
(result, failure) -> result == "test" || failure instanceof IllegalArgumentException);
assertTrue(policy.isFailure("test", null));
// No retries needed for successful result
assertFalse(policy.isFailure(0, null));
Expand All @@ -48,6 +56,12 @@ public void testIsFailureFailurePredicate() {
assertFalse(policy.isFailure(null, new IllegalStateException()));
}

public void testIsFailureTypedFailurePredicate() {
RetryPolicy<Boolean> policy = new RetryPolicy<Boolean>().<FooException>handleIf((result, failure) -> failure.bar.equals("bar"));
assertTrue(policy.isFailure(null, new FooException("bar")));
assertFalse(policy.isFailure(null, new IllegalStateException()));
}

public void testIsFailureResultPredicate() {
RetryPolicy<Integer> policy = new RetryPolicy<Integer>().handleResultIf(result -> result > 100);
assertTrue(policy.isFailure(110, null));
Expand Down Expand Up @@ -93,8 +107,8 @@ public void testIsAbortableNull() {
}

public void testIsAbortableCompletionPredicate() {
RetryPolicy<Object> policy = new RetryPolicy<>()
.abortIf((result, failure) -> result == "test" || failure instanceof IllegalArgumentException);
RetryPolicy<Object> policy = new RetryPolicy<>().abortIf(
(result, failure) -> result == "test" || failure instanceof IllegalArgumentException);
assertTrue(policy.isAbortable("test", null));
assertFalse(policy.isAbortable(0, null));
assertTrue(policy.isAbortable(null, new IllegalArgumentException()));
Expand Down Expand Up @@ -142,19 +156,23 @@ public void testIsAbortableResult() {
public void shouldRequireValidBackoff() {
Asserts.assertThrows(() -> new RetryPolicy().withBackoff(0, 0, null), NullPointerException.class);
Asserts.assertThrows(
() -> new RetryPolicy().withMaxDuration(Duration.ofMillis(1)).withBackoff(100, 120, ChronoUnit.MILLIS),
IllegalStateException.class);
Asserts.assertThrows(() -> new RetryPolicy().withBackoff(-3, 10, ChronoUnit.MILLIS), IllegalArgumentException.class);
Asserts.assertThrows(() -> new RetryPolicy().withBackoff(100, 10, ChronoUnit.MILLIS), IllegalArgumentException.class);
Asserts.assertThrows(() -> new RetryPolicy().withBackoff(5, 10, ChronoUnit.MILLIS, .5), IllegalArgumentException.class);
() -> new RetryPolicy().withMaxDuration(Duration.ofMillis(1)).withBackoff(100, 120, ChronoUnit.MILLIS),
IllegalStateException.class);
Asserts.assertThrows(() -> new RetryPolicy().withBackoff(-3, 10, ChronoUnit.MILLIS),
IllegalArgumentException.class);
Asserts.assertThrows(() -> new RetryPolicy().withBackoff(100, 10, ChronoUnit.MILLIS),
IllegalArgumentException.class);
Asserts.assertThrows(() -> new RetryPolicy().withBackoff(5, 10, ChronoUnit.MILLIS, .5),
IllegalArgumentException.class);
}

public void shouldRequireValidDelay() {
Asserts.assertThrows(() -> new RetryPolicy().withDelay((Duration)null), NullPointerException.class);
Asserts.assertThrows(() -> new RetryPolicy().withMaxDuration(Duration.ofMillis(1)).withDelay(Duration.ofMillis(100)),
IllegalStateException.class);
Asserts.assertThrows(() -> new RetryPolicy().withDelay((Duration) null), NullPointerException.class);
Asserts.assertThrows(
() -> new RetryPolicy().withMaxDuration(Duration.ofMillis(1)).withDelay(Duration.ofMillis(100)),
IllegalStateException.class);
Asserts.assertThrows(() -> new RetryPolicy().withBackoff(1, 2, ChronoUnit.MILLIS).withDelay(Duration.ofMillis(100)),
IllegalStateException.class);
IllegalStateException.class);
Asserts.assertThrows(() -> new RetryPolicy().withDelay(Duration.ofMillis(-1)), IllegalArgumentException.class);
}

Expand All @@ -164,8 +182,8 @@ public void shouldRequireValidMaxRetries() {

public void shouldRequireValidMaxDuration() {
Asserts.assertThrows(
() -> new RetryPolicy().withDelay(Duration.ofMillis(100)).withMaxDuration(Duration.ofMillis(100)),
IllegalStateException.class);
() -> new RetryPolicy().withDelay(Duration.ofMillis(100)).withMaxDuration(Duration.ofMillis(100)),
IllegalStateException.class);
}

public void testGetMaxAttempts() {
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/net/jodah/failsafe/examples/VertxExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public class VertxExample {
static Vertx vertx = Vertx.vertx();

/** Create RetryPolicy to handle Vert.x failures */
static RetryPolicy<Object> retryPolicy = new RetryPolicy<>().handleIf(
(ReplyException failure) -> ReplyFailure.RECIPIENT_FAILURE.equals(failure.failureType())
|| ReplyFailure.TIMEOUT.equals(failure.failureType()));
static RetryPolicy<Object> retryPolicy = new RetryPolicy<>().<ReplyException>handleIf(
failure -> ReplyFailure.RECIPIENT_FAILURE.equals(failure.failureType()) || ReplyFailure.TIMEOUT.equals(
failure.failureType()));

/** Adapt Vert.x timer to a Failsafe Scheduler */
static Scheduler scheduler = (callable, delay, unit) -> {
Expand Down