Search in sources :

Example 11 with CheckedRunnable

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

the class EventPublisherTest method shouldReturnAfterTwoAttempts.

@Test
public void shouldReturnAfterTwoAttempts() {
    // Given the HelloWorldService throws an exception
    BDDMockito.willThrow(new WebServiceException("BAM!")).willNothing().given(helloWorldService).sayHelloWorld();
    // Create a Retry with default configuration
    Retry retry = Retry.ofDefaults("id");
    TestSubscriber<RetryEvent.Type> testSubscriber = toFlowable(retry.getEventPublisher()).map(RetryEvent::getEventType).test();
    // 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 2 times
    BDDMockito.then(helloWorldService).should(Mockito.times(2)).sayHelloWorld();
    // and the result should be a sucess
    Assertions.assertThat(result.isSuccess()).isTrue();
    Assertions.assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION);
    testSubscriber.assertValueCount(1).assertValues(RetryEvent.Type.SUCCESS);
}
Also used : WebServiceException(javax.xml.ws.WebServiceException) CheckedRunnable(io.vavr.CheckedRunnable) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 12 with CheckedRunnable

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

the class RunnableRetryTest method shouldReturnAfterOneAttempt.

@Test
public void shouldReturnAfterOneAttempt() {
    // Given the HelloWorldService throws an exception
    BDDMockito.willThrow(new WebServiceException("BAM!")).given(helloWorldService).sayHelloWorld();
    // Create a Retry with default configuration
    RetryConfig config = RetryConfig.custom().maxAttempts(1).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 1 time
    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 : 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 13 with CheckedRunnable

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

the class RunnableRetryTest method shouldReturnAfterThreeAttempts.

@Test
public void shouldReturnAfterThreeAttempts() {
    // Given the HelloWorldService throws an exception
    BDDMockito.willThrow(new WebServiceException("BAM!")).given(helloWorldService).sayHelloWorld();
    // Create a Retry with default configuration
    Retry retry = Retry.ofDefaults("id");
    // 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 3 times
    BDDMockito.then(helloWorldService).should(Mockito.times(3)).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(RetryConfig.DEFAULT_WAIT_DURATION * 2);
}
Also used : WebServiceException(javax.xml.ws.WebServiceException) CheckedRunnable(io.vavr.CheckedRunnable) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Aggregations

CheckedRunnable (io.vavr.CheckedRunnable)13 Test (org.junit.Test)13 Retry (io.github.resilience4j.retry.Retry)7 WebServiceException (javax.xml.ws.WebServiceException)7 RetryConfig (io.github.resilience4j.retry.RetryConfig)4 Try (io.vavr.control.Try)4 CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)1 IntervalFunction (io.github.resilience4j.retry.IntervalFunction)1 HelloWorldService (io.github.resilience4j.test.HelloWorldService)1 API (io.vavr.API)1 Predicates (io.vavr.Predicates)1 IOException (java.io.IOException)1 Duration (java.time.Duration)1 Assertions (org.assertj.core.api.Assertions)1 Before (org.junit.Before)1 BDDMockito (org.mockito.BDDMockito)1 Mockito (org.mockito.Mockito)1