Skip to content

Commit

Permalink
Rename Failsafe.futureAsync -> Failsafe.getStageAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
jhalterman committed Jan 20, 2019
1 parent 826b252 commit 0161c19
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 46 deletions.
65 changes: 31 additions & 34 deletions src/main/java/net/jodah/failsafe/FailsafeExecutor.java
Expand Up @@ -53,8 +53,7 @@ public class FailsafeExecutor<R> extends PolicyListeners<FailsafeExecutor<R>, R>
}

/**
* Executes the {@code supplier} until a successful result is returned or the configured {@link RetryPolicy} is
* exceeded.
* Executes the {@code supplier} until a successful result is returned or the configured policies are exceeded.
*
* @throws NullPointerException if the {@code supplier} is null
* @throws FailsafeException if the {@code supplier} fails with a checked Exception or if interrupted while
Expand All @@ -66,8 +65,7 @@ public <T extends R> T get(CheckedSupplier<T> supplier) {
}

/**
* Executes the {@code supplier} until a successful result is returned or the configured {@link RetryPolicy} is
* exceeded.
* Executes the {@code supplier} until a successful result is returned or the configured policies are exceeded.
*
* @throws NullPointerException if the {@code supplier} is null
* @throws FailsafeException if the {@code supplier} fails with a checked Exception or if interrupted while
Expand All @@ -79,8 +77,8 @@ public <T extends R> T get(ContextualSupplier<T> supplier) {
}

/**
* Executes the {@code supplier} asynchronously until a successful result is returned or the configured {@link
* RetryPolicy} is exceeded.
* Executes the {@code supplier} asynchronously until a successful result is returned or the configured policies are
* exceeded.
* <p>
* If a configured circuit breaker is open, the resulting future is completed with {@link
* CircuitBreakerOpenException}.
Expand All @@ -93,8 +91,8 @@ public <T extends R> CompletableFuture<T> getAsync(CheckedSupplier<T> supplier)
}

/**
* Executes the {@code supplier} asynchronously until a successful result is returned or the configured {@link
* RetryPolicy} is exceeded.
* Executes the {@code supplier} asynchronously until a successful result is returned or the configured policies are
* exceeded.
* <p>
* If a configured circuit breaker is open, the resulting future is completed with {@link
* CircuitBreakerOpenException}.
Expand All @@ -107,9 +105,9 @@ public <T extends R> CompletableFuture<T> getAsync(ContextualSupplier<T> supplie
}

/**
* Executes the {@code supplier} asynchronously until a successful result is returned or the configured {@link
* RetryPolicy} is exceeded. This method is intended for integration with asynchronous code. Retries must be manually
* scheduled via one of the {@code AsyncExecution.retry} methods.
* Executes the {@code supplier} asynchronously until a successful result is returned or the configured policies are
* exceeded. This method is intended for integration with asynchronous code. Retries must be manually scheduled via
* one of the {@code AsyncExecution.retry} methods.
* <p>
* If a configured circuit breaker is open, the resulting future is completed with {@link
* CircuitBreakerOpenException}.
Expand All @@ -131,8 +129,8 @@ else if (failureListener != null && !result.getSuccessAll())
}

/**
* Registers the {@code listener} to be called when an execution is complete for all of the configured {@link Policy
* policies}.
* Registers the {@code listener} to be called when an execution is complete for all of the configured policies are
* exceeded.
*/
public FailsafeExecutor<R> onComplete(CheckedConsumer<? extends ExecutionCompletedEvent<R>> listener) {
completeListener = EventListener.of(Assert.notNull(listener, "listener"));
Expand Down Expand Up @@ -160,49 +158,50 @@ public FailsafeExecutor<R> onSuccess(CheckedConsumer<? extends ExecutionComplete

/**
* Executes the {@code supplier} asynchronously until the resulting future is successfully completed or the configured
* {@link RetryPolicy} is exceeded.
* policies are exceeded.
* <p>
* If a configured circuit breaker is open, the resulting future is completed exceptionally with {@link
* CircuitBreakerOpenException}.
*
* @throws NullPointerException if the {@code supplier} is null
* @throws RejectedExecutionException if the {@code supplier} cannot be scheduled for execution
*/
public <T extends R> CompletableFuture<T> futureAsync(CheckedSupplier<? extends CompletionStage<T>> supplier) {
public <T extends R> CompletableFuture<T> getStageAsync(CheckedSupplier<? extends CompletionStage<T>> supplier) {
return callAsync(execution -> Functions.promiseOfStage(supplier, execution));
}

/**
* Executes the {@code supplier} asynchronously until the resulting future is successfully completed or the configured
* {@link RetryPolicy} is exceeded.
* policies are exceeded.
* <p>
* If a configured circuit breaker is open, the resulting future is completed exceptionally with {@link
* CircuitBreakerOpenException}.
*
* @throws NullPointerException if the {@code supplier} is null
* @throws RejectedExecutionException if the {@code supplier} cannot be scheduled for execution
*/
public <T extends R> CompletableFuture<T> futureAsync(ContextualSupplier<? extends CompletionStage<T>> supplier) {
public <T extends R> CompletableFuture<T> getStageAsync(ContextualSupplier<? extends CompletionStage<T>> supplier) {
return callAsync(execution -> Functions.promiseOfStage(supplier, execution));
}

/**
* Executes the {@code supplier} asynchronously until the resulting future is successfully completed or the configured
* {@link RetryPolicy} is exceeded. This method is intended for integration with asynchronous code. Retries must be
* manually scheduled via one of the {@code AsyncExecution.retry} methods.
* policies are exceeded. This method is intended for integration with asynchronous code. Retries must be manually
* scheduled via one of the {@code AsyncExecution.retry} methods.
* <p>
* If a configured circuit breaker is open, the resulting future is completed exceptionally with {@link
* CircuitBreakerOpenException}.
*
* @throws NullPointerException if the {@code supplier} is null
* @throws RejectedExecutionException if the {@code supplier} cannot be scheduled for execution
*/
public <T extends R> CompletableFuture<T> futureAsyncExecution(AsyncSupplier<? extends CompletionStage<T>> supplier) {
public <T extends R> CompletableFuture<T> getStageAsyncExecution(
AsyncSupplier<? extends CompletionStage<T>> supplier) {
return callAsyncExecution(execution -> Functions.asyncOfFutureExecution(supplier, execution));
}

/**
* Executes the {@code runnable} until successful or until the configured {@link RetryPolicy} is exceeded.
* Executes the {@code runnable} until successful or until the configured policies are exceeded.
*
* @throws NullPointerException if the {@code runnable} is null
* @throws FailsafeException if the {@code supplier} fails with a checked Exception or if interrupted while
Expand All @@ -214,7 +213,7 @@ public void run(CheckedRunnable runnable) {
}

/**
* Executes the {@code runnable} until successful or until the configured {@link RetryPolicy} is exceeded.
* Executes the {@code runnable} until successful or until the configured policies are exceeded.
*
* @throws NullPointerException if the {@code runnable} is null
* @throws FailsafeException if the {@code runnable} fails with a checked Exception or if interrupted while
Expand All @@ -226,8 +225,7 @@ public void run(ContextualRunnable runnable) {
}

/**
* Executes the {@code runnable} asynchronously until successful or until the configured {@link RetryPolicy} is
* exceeded.
* Executes the {@code runnable} asynchronously until successful or until the configured policies are exceeded.
* <p>
* If a configured circuit breaker is open, the resulting future is completed with {@link
* CircuitBreakerOpenException}.
Expand All @@ -240,8 +238,7 @@ public CompletableFuture<Void> runAsync(CheckedRunnable runnable) {
}

/**
* Executes the {@code runnable} asynchronously until successful or until the configured {@link RetryPolicy} is
* exceeded.
* Executes the {@code runnable} asynchronously until successful or until the configured policies are exceeded.
* <p>
* If a configured circuit breaker is open, the resulting future is completed with {@link
* CircuitBreakerOpenException}.
Expand All @@ -254,9 +251,9 @@ public CompletableFuture<Void> runAsync(ContextualRunnable runnable) {
}

/**
* Executes the {@code runnable} asynchronously until successful or until the configured {@link RetryPolicy} is
* exceeded. This method is intended for integration with asynchronous code. Retries must be manually scheduled via
* one of the {@code AsyncExecution.retry} methods.
* Executes the {@code runnable} asynchronously until successful or until the configured policies are exceeded. This
* method is intended for integration with asynchronous code. Retries must be manually scheduled via one of the {@code
* AsyncExecution.retry} methods.
* <p>
* If a configured circuit breaker is open, the resulting future is completed with {@link
* CircuitBreakerOpenException}.
Expand Down Expand Up @@ -289,7 +286,7 @@ public FailsafeExecutor<R> with(Scheduler scheduler) {
}

/**
* Calls the {@code supplier} synchronously, performing retries according to the {@code retryPolicy}.
* Calls the {@code supplier} synchronously, handling results according to the configured policies.
*
* @throws FailsafeException if the {@code supplier} fails with a checked Exception or if interrupted while
* waiting to perform a retry.
Expand All @@ -309,8 +306,8 @@ private <T> T call(Function<Execution, CheckedSupplier<?>> supplierFn) {
}

/**
* Calls the asynchronous {@code supplier} via the configured Scheduler, performing retries according to the
* configured RetryPolicy.
* Calls the asynchronous {@code supplier} via the configured Scheduler, handling results according to the configured
* policies.
* <p>
* If a configured circuit breaker is open, the resulting future is completed with {@link
* CircuitBreakerOpenException}.
Expand All @@ -329,8 +326,8 @@ private <T> CompletableFuture<T> callAsync(
}

/**
* Calls the asynchronous {@code supplier} via the configured Scheduler, performing retries according to the
* configured RetryPolicy, until any configured RetryPolicy is exceeded or the AsyncExecution is completed.
* Calls the asynchronous {@code supplier} via the configured Scheduler, handling results according to the configured
* policies, until any configured policy is exceeded or the AsyncExecution is completed.
* <p>
* If a configured circuit breaker is open, the resulting future is completed with {@link
* CircuitBreakerOpenException}.
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/net/jodah/failsafe/AsyncFailsafeTest.java
Expand Up @@ -327,15 +327,15 @@ public void shouldManuallyRetryAndComplete() throws Throwable {
* Assert handles a supplier that throws instead of returning a future.
*/
public void shouldHandleThrowingFutureSupplier() {
assertThrows(() -> Failsafe.with(retryTwice).with(executor).futureAsync(() -> {
assertThrows(() -> Failsafe.with(retryTwice).with(executor).getStageAsync(() -> {
throw new IllegalArgumentException();
}).get(), ExecutionException.class, IllegalArgumentException.class);

assertThrows(() -> Failsafe.with(retryTwice).with(executor).futureAsync(context -> {
assertThrows(() -> Failsafe.with(retryTwice).with(executor).getStageAsync(context -> {
throw new IllegalArgumentException();
}).get(), ExecutionException.class, IllegalArgumentException.class);

assertThrows(() -> Failsafe.with(retryTwice).with(executor).futureAsyncExecution(exec -> {
assertThrows(() -> Failsafe.with(retryTwice).with(executor).getStageAsyncExecution(exec -> {
throw new IllegalArgumentException();
}).get(), ExecutionException.class, IllegalArgumentException.class);
}
Expand Down Expand Up @@ -459,10 +459,10 @@ else if (supplier instanceof ContextualSupplier)
@SuppressWarnings("unchecked")
private <T> CompletableFuture<T> futureAsync(FailsafeExecutor<T> failsafe, Object supplier) {
if (supplier instanceof CheckedSupplier)
return failsafe.futureAsync((CheckedSupplier<CompletableFuture<T>>) supplier);
return failsafe.getStageAsync((CheckedSupplier<CompletableFuture<T>>) supplier);
else if (supplier instanceof ContextualSupplier)
return failsafe.futureAsync((ContextualSupplier<CompletableFuture<T>>) supplier);
return failsafe.getStageAsync((ContextualSupplier<CompletableFuture<T>>) supplier);
else
return failsafe.futureAsyncExecution((AsyncSupplier<CompletableFuture<T>>) supplier);
return failsafe.getStageAsyncExecution((AsyncSupplier<CompletableFuture<T>>) supplier);
}
}
Expand Up @@ -46,7 +46,7 @@ public static void main(String... args) {
Stream.of("foo").map(value -> Failsafe.with(retryPolicy).get(() -> value + "bar")).forEach(System.out::println);

// Create a retryable CompletableFuture
Failsafe.with(retryPolicy).with(executor).futureAsync(() -> CompletableFuture.supplyAsync(() -> "foo")
Failsafe.with(retryPolicy).with(executor).getStageAsync(() -> CompletableFuture.supplyAsync(() -> "foo")
.thenApplyAsync(value -> value + "bar")
.thenAccept(System.out::println));

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/net/jodah/failsafe/issues/Issue131Test.java
Expand Up @@ -65,7 +65,7 @@ public void asyncShouldCompleteTheFuture() throws Throwable {
Waiter waiter = new Waiter();

failsafe
.futureAsync(() -> {
.getStageAsync(() -> {
CompletableFuture<String> future = new CompletableFuture<>();
future.completeExceptionally(new IOException("let's blame it on network error"));
return future;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/net/jodah/failsafe/issues/Issue52Test.java
Expand Up @@ -47,7 +47,7 @@ public void shouldCancelExecutionViaCompletableFuture() throws Throwable {
AtomicInteger counter = new AtomicInteger();
CompletableFuture<String> proxyFuture = Failsafe.with(new RetryPolicy<>().withDelay(Duration.ofMillis(10)))
.with(scheduler)
.futureAsync(exec -> {
.getStageAsync(exec -> {
Thread.sleep(100);
counter.incrementAndGet();
CompletableFuture<String> result = new CompletableFuture<>();
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/net/jodah/failsafe/issues/Issue75Test.java
Expand Up @@ -19,7 +19,7 @@ public void testThatFailSafeIsBrokenWithFallback() throws Exception {
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
int result = Failsafe.with(Fallback.of((a, b) -> 999), breaker)
.with(service)
.futureAsync(() -> CompletableFuture.completedFuture(223))
.getStageAsync(() -> CompletableFuture.completedFuture(223))
.get();

Assert.assertEquals(result, 223);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/net/jodah/failsafe/issues/Issue84Test.java
Expand Up @@ -30,12 +30,12 @@ public void shouldHandleCircuitBreakerOpenException() throws Throwable {
assertFalse(future2.get());

// Future
Future<Boolean> future3 = Failsafe.with(circuitBreaker).with(executor).futureAsync(() -> CompletableFuture.completedFuture(false));
Future<Boolean> future3 = Failsafe.with(circuitBreaker).with(executor).getStageAsync(() -> CompletableFuture.completedFuture(false));
Asserts.assertThrows(future3::get, ExecutionException.class, CircuitBreakerOpenException.class);

// Future with fallback
Future<Boolean> future4 = Failsafe.with(Fallback.of(false), circuitBreaker)
.futureAsync(() -> CompletableFuture.completedFuture(false));
.getStageAsync(() -> CompletableFuture.completedFuture(false));
assertFalse(future4.get());
}
}

0 comments on commit 0161c19

Please sign in to comment.