Search in sources :

Example 6 with Try

use of io.vavr.control.Try in project resilience4j by resilience4j.

the class RateLimiterTest method decorateSupplier.

@Test
public void decorateSupplier() throws Exception {
    Supplier supplier = mock(Supplier.class);
    Supplier decorated = RateLimiter.decorateSupplier(limit, supplier);
    when(limit.getPermission(config.getTimeoutDuration())).thenReturn(false);
    Try decoratedSupplierResult = Try.success(decorated).map(Supplier::get);
    then(decoratedSupplierResult.isFailure()).isTrue();
    then(decoratedSupplierResult.getCause()).isInstanceOf(RequestNotPermitted.class);
    verify(supplier, never()).get();
    when(limit.getPermission(config.getTimeoutDuration())).thenReturn(true);
    Try secondSupplierResult = Try.success(decorated).map(Supplier::get);
    then(secondSupplierResult.isSuccess()).isTrue();
    verify(supplier, times(1)).get();
}
Also used : Supplier(java.util.function.Supplier) Try(io.vavr.control.Try) Test(org.junit.Test)

Example 7 with Try

use of io.vavr.control.Try in project resilience4j by resilience4j.

the class RunnableRetryTest method shouldReturnAfterOneAttemptAndIgnoreException.

@Test
public void shouldReturnAfterOneAttemptAndIgnoreException() {
    // Given the HelloWorldService throws an exception
    BDDMockito.willThrow(new WebServiceException("BAM!")).given(helloWorldService).sayHelloWorld();
    // Create a Retry with default configuration
    RetryConfig config = RetryConfig.custom().retryOnException(throwable -> Match(throwable).of(Case($(Predicates.instanceOf(WebServiceException.class)), false), Case($(), true))).build();
    Retry retry = Retry.of("id", config);
    // Decorate the invocation of the HelloWorldService
    CheckedRunnable retryableRunnable = Retry.decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld);
    // When
    Try<Void> result = Try.run(retryableRunnable);
    // Then the helloWorldService should be invoked only once, because the exception should be rethrown immediately.
    BDDMockito.then(helloWorldService).should(Mockito.times(1)).sayHelloWorld();
    // and the result should be a failure
    Assertions.assertThat(result.isFailure()).isTrue();
    // and the returned exception should be of type RuntimeException
    Assertions.assertThat(result.failed().get()).isInstanceOf(WebServiceException.class);
    Assertions.assertThat(sleptTime).isEqualTo(0);
}
Also used : Predicates(io.vavr.Predicates) Retry(io.github.resilience4j.retry.Retry) RetryConfig(io.github.resilience4j.retry.RetryConfig) Test(org.junit.Test) API(io.vavr.API) IntervalFunction(io.github.resilience4j.retry.IntervalFunction) BDDMockito(org.mockito.BDDMockito) Mockito(org.mockito.Mockito) Try(io.vavr.control.Try) WebServiceException(javax.xml.ws.WebServiceException) CheckedRunnable(io.vavr.CheckedRunnable) Duration(java.time.Duration) HelloWorldService(io.github.resilience4j.test.HelloWorldService) Assertions(org.assertj.core.api.Assertions) Before(org.junit.Before) RetryConfig(io.github.resilience4j.retry.RetryConfig) WebServiceException(javax.xml.ws.WebServiceException) CheckedRunnable(io.vavr.CheckedRunnable) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 8 with Try

use of io.vavr.control.Try in project resilience4j by resilience4j.

the class SupplierRetryTest method shouldReturnAfterOneAttemptAndIgnoreException.

@Test
public void shouldReturnAfterOneAttemptAndIgnoreException() {
    // Given the HelloWorldService throws an exception
    BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
    // Create a Retry with default configuration
    RetryConfig config = RetryConfig.custom().retryOnException(throwable -> API.Match(throwable).of(API.Case($(Predicates.instanceOf(WebServiceException.class)), false), API.Case($(), true))).build();
    Retry retry = Retry.of("id", config);
    // Decorate the invocation of the HelloWorldService
    CheckedFunction0<String> retryableSupplier = Retry.decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld);
    // When
    Try<String> result = Try.of(retryableSupplier);
    // Then the helloWorldService should be invoked only once, because the exception should be rethrown immediately.
    BDDMockito.then(helloWorldService).should(Mockito.times(1)).returnHelloWorld();
    // and the result should be a failure
    assertThat(result.isFailure()).isTrue();
    // and the returned exception should be of type RuntimeException
    assertThat(result.failed().get()).isInstanceOf(WebServiceException.class);
    assertThat(sleptTime).isEqualTo(0);
}
Also used : Predicates(io.vavr.Predicates) Retry(io.github.resilience4j.retry.Retry) RetryConfig(io.github.resilience4j.retry.RetryConfig) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Callable(java.util.concurrent.Callable) API(io.vavr.API) IntervalFunction(io.github.resilience4j.retry.IntervalFunction) CheckedFunction0(io.vavr.CheckedFunction0) Supplier(java.util.function.Supplier) BDDMockito(org.mockito.BDDMockito) Mockito(org.mockito.Mockito) Try(io.vavr.control.Try) WebServiceException(javax.xml.ws.WebServiceException) HelloWorldService(io.github.resilience4j.test.HelloWorldService) Before(org.junit.Before) API.$(io.vavr.API.$) RetryConfig(io.github.resilience4j.retry.RetryConfig) WebServiceException(javax.xml.ws.WebServiceException) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 9 with Try

use of io.vavr.control.Try in project resilience4j by resilience4j.

the class IntervalFunctionTest method shouldRejectNonPositiveDuration.

@Test
public void shouldRejectNonPositiveDuration() {
    // Given
    final Duration negativeDuration = Duration.ofMillis(0);
    final Duration zeroDuration = Duration.ofMillis(0);
    final Duration smallDuration = Duration.ofMillis(9);
    final long negativeInterval = -1;
    final long zeroInterval = 0;
    final long smallInterval = 9;
    // When
    List<Try> tries = List.of(Try.of(() -> IntervalFunction.of(negativeDuration)), Try.of(() -> IntervalFunction.of(zeroDuration)), Try.of(() -> IntervalFunction.of(smallDuration)), Try.of(() -> IntervalFunction.of(negativeInterval)), Try.of(() -> IntervalFunction.of(zeroInterval)), Try.of(() -> IntervalFunction.of(smallInterval)));
    // Then
    Assertions.assertThat(tries.forAll(Try::isFailure)).isTrue();
    Assertions.assertThat(tries.map(Try::getCause).forAll(t -> t instanceof IllegalArgumentException)).isTrue();
}
Also used : Duration(java.time.Duration) Try(io.vavr.control.Try) Test(org.junit.Test)

Example 10 with Try

use of io.vavr.control.Try in project resilience4j by resilience4j.

the class CircuitBreakerTest method shouldReturnFailureWithRuntimeException.

@Test
public void shouldReturnFailureWithRuntimeException() {
    // Given
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");
    assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED);
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0);
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(0);
    // When
    CheckedRunnable checkedRunnable = CircuitBreaker.decorateCheckedRunnable(circuitBreaker, () -> {
        throw new RuntimeException("BAM!");
    });
    Try result = Try.run(checkedRunnable);
    // Then
    assertThat(result.isFailure()).isTrue();
    assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
}
Also used : Try(io.vavr.control.Try) Test(org.junit.Test)

Aggregations

Try (io.vavr.control.Try)20 Test (org.junit.Test)20 CheckedRunnable (io.vavr.CheckedRunnable)6 Duration (java.time.Duration)6 Supplier (java.util.function.Supplier)4 Before (org.junit.Before)4 BDDMockito (org.mockito.BDDMockito)4 HelloWorldService (io.github.resilience4j.test.HelloWorldService)3 API (io.vavr.API)3 API.$ (io.vavr.API.$)3 CheckedFunction0 (io.vavr.CheckedFunction0)3 Predicates (io.vavr.Predicates)3 WebServiceException (javax.xml.ws.WebServiceException)3 Mockito (org.mockito.Mockito)3 IntervalFunction (io.github.resilience4j.retry.IntervalFunction)2 Retry (io.github.resilience4j.retry.Retry)2 RetryConfig (io.github.resilience4j.retry.RetryConfig)2 Callable (java.util.concurrent.Callable)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletionStage (java.util.concurrent.CompletionStage)2