From bb206a5624016691e8b6c4fb62a5ae773ee9eed1 Mon Sep 17 00:00:00 2001 From: Jonathan Halterman Date: Tue, 10 Aug 2021 22:00:45 -0700 Subject: [PATCH] Add a typed parameter for the failure in FailurePolicy.handleIf --- .../net/jodah/failsafe/FailurePolicy.java | 6 ++- .../net/jodah/failsafe/RetryPolicyTest.java | 48 +++++++++++++------ .../jodah/failsafe/examples/VertxExample.java | 6 +-- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/main/java/net/jodah/failsafe/FailurePolicy.java b/src/main/java/net/jodah/failsafe/FailurePolicy.java index 04069503..e7be5725 100644 --- a/src/main/java/net/jodah/failsafe/FailurePolicy.java +++ b/src/main/java/net/jodah/failsafe/FailurePolicy.java @@ -85,9 +85,10 @@ public S handle(List> failures) { /** * Specifies that a failure has occurred if the {@code failurePredicate} matches the failure. * + * @param failure type * @throws NullPointerException if {@code failurePredicate} is null */ - public S handleIf(Predicate failurePredicate) { + public S handleIf(Predicate failurePredicate) { Assert.notNull(failurePredicate, "failurePredicate"); failuresChecked = true; failureConditions.add(failurePredicateFor(failurePredicate)); @@ -97,10 +98,11 @@ public S handleIf(Predicate failurePredicate) { /** * Specifies that a failure has occurred if the {@code resultPredicate} matches the execution result. * + * @param failure type * @throws NullPointerException if {@code resultPredicate} is null */ @SuppressWarnings("unchecked") - public S handleIf(BiPredicate resultPredicate) { + public S handleIf(BiPredicate resultPredicate) { Assert.notNull(resultPredicate, "resultPredicate"); failuresChecked = true; failureConditions.add((BiPredicate) resultPredicate); diff --git a/src/test/java/net/jodah/failsafe/RetryPolicyTest.java b/src/test/java/net/jodah/failsafe/RetryPolicyTest.java index 064d44b5..498059f5 100644 --- a/src/test/java/net/jodah/failsafe/RetryPolicyTest.java +++ b/src/test/java/net/jodah/failsafe/RetryPolicyTest.java @@ -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 policy = new RetryPolicy<>(); assertFalse(policy.isFailure(null, null)); } public void testIsFailureCompletionPredicate() { - RetryPolicy policy = new RetryPolicy<>() - .handleIf((result, failure) -> result == "test" || failure instanceof IllegalArgumentException); + RetryPolicy 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)); @@ -48,6 +56,12 @@ public void testIsFailureFailurePredicate() { assertFalse(policy.isFailure(null, new IllegalStateException())); } + public void testIsFailureTypedFailurePredicate() { + RetryPolicy policy = new RetryPolicy().handleIf((result, failure) -> failure.bar.equals("bar")); + assertTrue(policy.isFailure(null, new FooException("bar"))); + assertFalse(policy.isFailure(null, new IllegalStateException())); + } + public void testIsFailureResultPredicate() { RetryPolicy policy = new RetryPolicy().handleResultIf(result -> result > 100); assertTrue(policy.isFailure(110, null)); @@ -93,8 +107,8 @@ public void testIsAbortableNull() { } public void testIsAbortableCompletionPredicate() { - RetryPolicy policy = new RetryPolicy<>() - .abortIf((result, failure) -> result == "test" || failure instanceof IllegalArgumentException); + RetryPolicy 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())); @@ -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); } @@ -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() { diff --git a/src/test/java/net/jodah/failsafe/examples/VertxExample.java b/src/test/java/net/jodah/failsafe/examples/VertxExample.java index bda53934..47755bb3 100644 --- a/src/test/java/net/jodah/failsafe/examples/VertxExample.java +++ b/src/test/java/net/jodah/failsafe/examples/VertxExample.java @@ -30,9 +30,9 @@ public class VertxExample { static Vertx vertx = Vertx.vertx(); /** Create RetryPolicy to handle Vert.x failures */ - static RetryPolicy retryPolicy = new RetryPolicy<>().handleIf( - (ReplyException failure) -> ReplyFailure.RECIPIENT_FAILURE.equals(failure.failureType()) - || ReplyFailure.TIMEOUT.equals(failure.failureType())); + static RetryPolicy retryPolicy = new RetryPolicy<>().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) -> {