Esempio n. 1
0
  @Test
  public void shouldConvertFailureToFailedCompletableFuture() {

    final CompletableFuture<Object> future = failure().toCompletableFuture();
    assertThat(future.isDone());
    assertThat(future.isCompletedExceptionally());
    assertThatThrownBy(future::get)
        .isExactlyInstanceOf(ExecutionException.class)
        .hasCauseExactlyInstanceOf(RuntimeException.class);
  }
  @Test
  public void shouldAbortWhenTargetFutureWantsToAbort() throws Exception {
    // given
    final RetryExecutor executor = new AsyncRetryExecutor(schedulerMock);
    given(serviceMock.safeAsync()).willReturn(failedAsync(new AbortRetryException()));

    // when
    final CompletableFuture<String> future =
        executor.getFutureWithRetry(ctx -> serviceMock.safeAsync());

    // then
    assertThat(future.isCompletedExceptionally()).isTrue();

    try {
      future.get();
      failBecauseExceptionWasNotThrown(ExecutionException.class);
    } catch (ExecutionException e) {
      final Throwable cause = e.getCause();
      assertThat(cause).isInstanceOf(AbortRetryException.class);
    }
  }
  @Test
  public void shouldRethrowOriginalExceptionFromUserFutureCompletion() throws Exception {
    // given
    final RetryExecutor executor =
        new AsyncRetryExecutor(schedulerMock).abortOn(SocketException.class);
    given(serviceMock.safeAsync()).willReturn(failedAsync(new SocketException(DON_T_PANIC)));

    // when
    final CompletableFuture<String> future =
        executor.getFutureWithRetry(ctx -> serviceMock.safeAsync());

    // then
    assertThat(future.isCompletedExceptionally()).isTrue();

    try {
      future.get();
      failBecauseExceptionWasNotThrown(ExecutionException.class);
    } catch (ExecutionException e) {
      final Throwable cause = e.getCause();
      assertThat(cause).isInstanceOf(SocketException.class);
      assertThat(cause).hasMessage(DON_T_PANIC);
    }
  }
  @Test
  public void shouldRethrowExceptionThatWasThrownFromUserTaskBeforeReturningFuture()
      throws Exception {
    // given
    final RetryExecutor executor =
        new AsyncRetryExecutor(schedulerMock).abortOn(IllegalArgumentException.class);
    given(serviceMock.safeAsync()).willThrow(new IllegalArgumentException(DON_T_PANIC));

    // when
    final CompletableFuture<String> future =
        executor.getFutureWithRetry(ctx -> serviceMock.safeAsync());

    // then
    assertThat(future.isCompletedExceptionally()).isTrue();

    try {
      future.get();
      Assertions.failBecauseExceptionWasNotThrown(ExecutionException.class);
    } catch (ExecutionException e) {
      assertThat(e.getCause()).isInstanceOf(IllegalArgumentException.class);
      assertThat(e.getCause()).hasMessage(DON_T_PANIC);
    }
  }