Search in sources :

Example 1 with CheckedFunction0

use of io.vavr.CheckedFunction0 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 2 with CheckedFunction0

use of io.vavr.CheckedFunction0 in project resilience4j by resilience4j.

the class RateLimiterTest method decorateCheckedSupplier.

@Test
public void decorateCheckedSupplier() throws Throwable {
    CheckedFunction0 supplier = mock(CheckedFunction0.class);
    CheckedFunction0 decorated = RateLimiter.decorateCheckedSupplier(limit, supplier);
    when(limit.getPermission(config.getTimeoutDuration())).thenReturn(false);
    Try decoratedSupplierResult = Try.of(decorated);
    then(decoratedSupplierResult.isFailure()).isTrue();
    then(decoratedSupplierResult.getCause()).isInstanceOf(RequestNotPermitted.class);
    verify(supplier, never()).apply();
    when(limit.getPermission(config.getTimeoutDuration())).thenReturn(true);
    Try secondSupplierResult = Try.of(decorated);
    then(secondSupplierResult.isSuccess()).isTrue();
    verify(supplier, times(1)).apply();
}
Also used : CheckedFunction0(io.vavr.CheckedFunction0) Try(io.vavr.control.Try) Test(org.junit.Test)

Aggregations

CheckedFunction0 (io.vavr.CheckedFunction0)2 Try (io.vavr.control.Try)2 Test (org.junit.Test)2 IntervalFunction (io.github.resilience4j.retry.IntervalFunction)1 Retry (io.github.resilience4j.retry.Retry)1 RetryConfig (io.github.resilience4j.retry.RetryConfig)1 HelloWorldService (io.github.resilience4j.test.HelloWorldService)1 API (io.vavr.API)1 API.$ (io.vavr.API.$)1 Predicates (io.vavr.Predicates)1 Callable (java.util.concurrent.Callable)1 Supplier (java.util.function.Supplier)1 WebServiceException (javax.xml.ws.WebServiceException)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Before (org.junit.Before)1 BDDMockito (org.mockito.BDDMockito)1 Mockito (org.mockito.Mockito)1