From 2186b3d1cbcd44950a6b48f23520ee629335abe6 Mon Sep 17 00:00:00 2001 From: Jonathan Halterman Date: Sun, 13 Jan 2019 20:39:32 -0800 Subject: [PATCH] Remove FailsafeExecutor overloads for withFallback --- .../net/jodah/failsafe/AsyncExecution.java | 1 + .../net/jodah/failsafe/ExecutionContext.java | 2 +- .../net/jodah/failsafe/FailsafeExecutor.java | 82 +------------------ .../jodah/failsafe/AbstractFailsafeTest.java | 8 +- .../net/jodah/failsafe/AsyncFailsafeTest.java | 2 +- .../functional/ComputedDelayTest.java | 5 +- .../jodah/failsafe/issues/Issue55Test.java | 16 ++-- .../jodah/failsafe/issues/Issue75Test.java | 3 +- .../jodah/failsafe/issues/Issue84Test.java | 11 +-- 9 files changed, 26 insertions(+), 104 deletions(-) diff --git a/src/main/java/net/jodah/failsafe/AsyncExecution.java b/src/main/java/net/jodah/failsafe/AsyncExecution.java index 9dd4a61a..b2a11c94 100644 --- a/src/main/java/net/jodah/failsafe/AsyncExecution.java +++ b/src/main/java/net/jodah/failsafe/AsyncExecution.java @@ -38,6 +38,7 @@ public final class AsyncExecution extends AbstractExecution { private volatile boolean completeCalled; private volatile boolean retryCalled; + @SuppressWarnings("unchecked") AsyncExecution(Scheduler scheduler, FailsafeFuture future, FailsafeExecutor executor) { super((FailsafeExecutor) executor); this.scheduler = scheduler; diff --git a/src/main/java/net/jodah/failsafe/ExecutionContext.java b/src/main/java/net/jodah/failsafe/ExecutionContext.java index 26c75bc9..7afc36e8 100644 --- a/src/main/java/net/jodah/failsafe/ExecutionContext.java +++ b/src/main/java/net/jodah/failsafe/ExecutionContext.java @@ -23,7 +23,7 @@ * @author Jonathan Halterman */ public class ExecutionContext { - final Duration startTime; + private final Duration startTime; /** Number of execution attempts */ volatile int executions; diff --git a/src/main/java/net/jodah/failsafe/FailsafeExecutor.java b/src/main/java/net/jodah/failsafe/FailsafeExecutor.java index a433f6a0..e97607e1 100644 --- a/src/main/java/net/jodah/failsafe/FailsafeExecutor.java +++ b/src/main/java/net/jodah/failsafe/FailsafeExecutor.java @@ -321,84 +321,6 @@ public FailsafeExecutor with(RetryPolicy retryPolicy) { return this; } - /** - * Configures the {@code fallback} action to be executed if execution fails. - * - * @throws NullPointerException if {@code fallback} is null - * @throws IllegalStateException if {@code withFallback} method has already been called or if ordered policies - * have been configured - */ - public FailsafeExecutor withFallback(CheckedSupplier fallback) { - return withFallback(Fallback.of(fallback)); - } - - /** - * Configures the {@code fallback} action to be executed if execution fails. - * - * @throws NullPointerException if {@code fallback} is null - * @throws IllegalStateException if {@code withFallback} method has already been called or if ordered policies - * have been configured - */ - public FailsafeExecutor withFallback(CheckedBiConsumer fallback) { - return withFallback(Fallback.of(fallback)); - } - - /** - * Configures the {@code fallback} action to be executed if execution fails. - * - * @throws NullPointerException if {@code fallback} is null - * @throws IllegalStateException if {@code withFallback} method has already been called or if ordered policies - * have been configured - */ - public FailsafeExecutor withFallback(CheckedBiFunction fallback) { - withFallback(Fallback.of(fallback)); - return this; - } - - /** - * Configures the {@code fallback} action to be executed if execution fails. - * - * @throws NullPointerException if {@code fallback} is null - * @throws IllegalStateException if {@code withFallback} method has already been called or if ordered policies - * have been configured - */ - public FailsafeExecutor withFallback(CheckedConsumer fallback) { - return withFallback(Fallback.of(fallback)); - } - - /** - * Configures the {@code fallback} action to be executed if execution fails. - * - * @throws NullPointerException if {@code fallback} is null - * @throws IllegalStateException if {@code withFallback} method has already been called or if ordered policies - * have been configured - */ - public FailsafeExecutor withFallback(CheckedFunction fallback) { - return withFallback(Fallback.of(fallback)); - } - - /** - * Configures the {@code fallback} action to be executed if execution fails. - * - * @throws NullPointerException if {@code fallback} is null - * @throws IllegalStateException if {@code withFallback} method has already been called or if ordered policies - * have been configured - */ - public FailsafeExecutor withFallback(CheckedRunnable fallback) { - return withFallback(Fallback.of(fallback)); - } - - /** - * Configures the {@code fallback} result to be returned if execution fails. - * - * @throws NullPointerException if {@code fallback} is null - * @throws IllegalStateException if {@code withFallback} method has already been called or if ordered policies - * have been configured - */ - public FailsafeExecutor withFallback(R fallback) { - return withFallback(Fallback.of(fallback)); - } - /** * Configures the {@code fallback} result to be returned if execution fails. * @@ -406,8 +328,8 @@ public FailsafeExecutor withFallback(R fallback) { * @throws IllegalStateException if {@code withFallback} method has already been called or if ordered policies * have been configured */ - public FailsafeExecutor withFallback(Fallback fallback) { - Assert.state(this.fallback == null, "withFallback has already been called"); + public FailsafeExecutor with(Fallback fallback) { + Assert.state(this.fallback == null, "A fallback has already been configured"); Assert.state(policies == null || policies.isEmpty(), "Policies have already been configured"); this.fallback = Assert.notNull(fallback, "fallback"); return this; diff --git a/src/test/java/net/jodah/failsafe/AbstractFailsafeTest.java b/src/test/java/net/jodah/failsafe/AbstractFailsafeTest.java index ab5511a8..71fafe0d 100644 --- a/src/test/java/net/jodah/failsafe/AbstractFailsafeTest.java +++ b/src/test/java/net/jodah/failsafe/AbstractFailsafeTest.java @@ -94,8 +94,8 @@ void failsafeRun(CircuitBreaker breaker, CheckedRunnable runnable) { */ T failsafeGet(CircuitBreaker breaker, CheckedBiFunction fallback, CheckedSupplier supplier) { ScheduledExecutorService executor = getExecutor(); - return unwrapExceptions(() -> executor == null ? Failsafe.with(breaker).withFallback(fallback).get(supplier) - : Failsafe.with(breaker).with(executor).withFallback(fallback).getAsync(supplier).get()); + return unwrapExceptions(() -> executor == null ? Failsafe.with(breaker).with(Fallback.of(fallback)).get(supplier) + : Failsafe.with(breaker).with(executor).with(Fallback.of(fallback)).getAsync(supplier).get()); } /** @@ -103,8 +103,8 @@ T failsafeGet(CircuitBreaker breaker, CheckedBiFunction */ T failsafeGet(RetryPolicy retryPolicy, CheckedBiFunction fallback, CheckedSupplier supplier) { ScheduledExecutorService executor = getExecutor(); - return unwrapExceptions(() -> executor == null ? Failsafe.with(retryPolicy).withFallback(fallback).get(supplier) - : Failsafe.with(retryPolicy).with(executor).withFallback(fallback).getAsync(supplier).get()); + return unwrapExceptions(() -> executor == null ? Failsafe.with(retryPolicy).with(Fallback.of(fallback)).get(supplier) + : Failsafe.with(retryPolicy).with(executor).with(Fallback.of(fallback)).getAsync(supplier).get()); } /** diff --git a/src/test/java/net/jodah/failsafe/AsyncFailsafeTest.java b/src/test/java/net/jodah/failsafe/AsyncFailsafeTest.java index 77ae67ca..2f10908c 100644 --- a/src/test/java/net/jodah/failsafe/AsyncFailsafeTest.java +++ b/src/test/java/net/jodah/failsafe/AsyncFailsafeTest.java @@ -398,7 +398,7 @@ public void shouldHandleInitialSchedulingFailure() { // When Future future = Failsafe.with(new CircuitBreaker<>()) .with(new RetryPolicy<>()) - .withFallback(false) + .with(Fallback.of(false)) .with(executor) .runAsync(() -> waiter.fail("Should not execute supplier since executor has been shutdown")); diff --git a/src/test/java/net/jodah/failsafe/functional/ComputedDelayTest.java b/src/test/java/net/jodah/failsafe/functional/ComputedDelayTest.java index 80e2f2ae..29b6f31b 100644 --- a/src/test/java/net/jodah/failsafe/functional/ComputedDelayTest.java +++ b/src/test/java/net/jodah/failsafe/functional/ComputedDelayTest.java @@ -17,6 +17,7 @@ import net.jodah.failsafe.ExecutionContext; import net.jodah.failsafe.Failsafe; +import net.jodah.failsafe.Fallback; import net.jodah.failsafe.RetryPolicy; import org.testng.annotations.Test; @@ -67,7 +68,7 @@ public void shouldDelayOnMatchingResult() { }, "expected"); AtomicInteger attempts = new AtomicInteger(0); - Object result = Failsafe.with(retryPolicy).withFallback(123).get(() -> { + Object result = Failsafe.with(retryPolicy).with(Fallback.of(123)).get(() -> { int i = attempts.getAndIncrement(); switch (i) { case 0: @@ -94,7 +95,7 @@ public void shouldDelayOnMatchingFailureType() { }, DelayException.class); AtomicInteger attempts = new AtomicInteger(0); - int result = Failsafe.with(retryPolicy).withFallback(123).get(() -> { + int result = Failsafe.with(retryPolicy).with(Fallback.of(123)).get(() -> { int i = attempts.getAndIncrement(); switch (i) { case 0: diff --git a/src/test/java/net/jodah/failsafe/issues/Issue55Test.java b/src/test/java/net/jodah/failsafe/issues/Issue55Test.java index 539226f2..bad452f4 100644 --- a/src/test/java/net/jodah/failsafe/issues/Issue55Test.java +++ b/src/test/java/net/jodah/failsafe/issues/Issue55Test.java @@ -15,16 +15,16 @@ */ package net.jodah.failsafe.issues; -import static org.testng.Assert.assertEquals; +import net.jodah.failsafe.Failsafe; +import net.jodah.failsafe.Fallback; +import net.jodah.failsafe.RetryPolicy; +import org.testng.annotations.Test; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.atomic.AtomicInteger; -import org.testng.annotations.Test; - -import net.jodah.failsafe.Failsafe; -import net.jodah.failsafe.RetryPolicy; +import static org.testng.Assert.assertEquals; @Test public class Issue55Test { @@ -32,14 +32,14 @@ public void shouldOnlyFallbackOnFailure() throws Throwable { ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); AtomicInteger counter = new AtomicInteger(); - Failsafe.with(new RetryPolicy()).with(executor).withFallback(() -> counter.incrementAndGet()).getAsync(() -> null); + Failsafe.with(new RetryPolicy<>()).with(executor).with(Fallback.of(counter::incrementAndGet)).getAsync(() -> null); Thread.sleep(100); assertEquals(counter.get(), 0); - Failsafe.with(new RetryPolicy().withMaxRetries(1)) + Failsafe.with(new RetryPolicy<>().withMaxRetries(1)) .with(executor) - .withFallback(() -> counter.incrementAndGet()) + .with(Fallback.of(counter::incrementAndGet)) .runAsync(() -> { throw new RuntimeException(); }); diff --git a/src/test/java/net/jodah/failsafe/issues/Issue75Test.java b/src/test/java/net/jodah/failsafe/issues/Issue75Test.java index d9331b2e..269b8d8f 100644 --- a/src/test/java/net/jodah/failsafe/issues/Issue75Test.java +++ b/src/test/java/net/jodah/failsafe/issues/Issue75Test.java @@ -2,6 +2,7 @@ import net.jodah.failsafe.CircuitBreaker; import net.jodah.failsafe.Failsafe; +import net.jodah.failsafe.Fallback; import org.testng.Assert; import org.testng.annotations.Test; @@ -18,7 +19,7 @@ public void testThatFailSafeIsBrokenWithFallback() throws Exception { ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); int result = Failsafe.with(breaker) .with(service) - .withFallback((a, b) -> 999) + .with(Fallback.of((a, b) -> 999)) .futureAsync(() -> CompletableFuture.completedFuture(223)) .get(); diff --git a/src/test/java/net/jodah/failsafe/issues/Issue84Test.java b/src/test/java/net/jodah/failsafe/issues/Issue84Test.java index c4d8dc09..0cd8f391 100644 --- a/src/test/java/net/jodah/failsafe/issues/Issue84Test.java +++ b/src/test/java/net/jodah/failsafe/issues/Issue84Test.java @@ -1,9 +1,6 @@ package net.jodah.failsafe.issues; -import net.jodah.failsafe.Asserts; -import net.jodah.failsafe.CircuitBreaker; -import net.jodah.failsafe.CircuitBreakerOpenException; -import net.jodah.failsafe.Failsafe; +import net.jodah.failsafe.*; import org.testng.annotations.Test; import java.util.concurrent.*; @@ -21,14 +18,14 @@ public void shouldHandleCircuitBreakerOpenException() throws Throwable { Asserts.assertThrows(() -> Failsafe.with(circuitBreaker).get(() -> true), CircuitBreakerOpenException.class); // Synchronous with fallback - assertFalse(Failsafe.with(circuitBreaker).withFallback(false).get(() -> true)); + assertFalse(Failsafe.with(circuitBreaker).with(Fallback.of(false)).get(() -> true)); // Asynchronous Future future1 = Failsafe.with(circuitBreaker).with(executor).getAsync(() -> true); Asserts.assertThrows(future1::get, ExecutionException.class, CircuitBreakerOpenException.class); // Asynchronous with fallback - Future future2 = Failsafe.with(circuitBreaker).with(executor).withFallback(false).getAsync(() -> true); + Future future2 = Failsafe.with(circuitBreaker).with(executor).with(Fallback.of(false)).getAsync(() -> true); assertFalse(future2.get()); // Future @@ -38,7 +35,7 @@ public void shouldHandleCircuitBreakerOpenException() throws Throwable { // Future with fallback Future future4 = Failsafe.with(circuitBreaker) .with(executor) - .withFallback(false) + .with(Fallback.of(false)) .futureAsync(() -> CompletableFuture.completedFuture(false)); assertFalse(future4.get()); }