Search in sources :

Example 1 with Retry

use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.

the class RetryMetricsTest method shouldUseCustomPrefix.

@Test
public void shouldUseCustomPrefix() throws Throwable {
    // Given
    RetryRegistry retryRegistry = RetryRegistry.ofDefaults();
    Retry retry = retryRegistry.retry("testName");
    metricRegistry.registerAll(RetryMetrics.ofRetryRegistry("testPrefix", retryRegistry));
    // Given the HelloWorldService returns Hello world
    BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
    String value = retry.executeSupplier(helloWorldService::returnHelloWorld);
    // Then
    assertThat(value).isEqualTo("Hello world");
    // Then the helloWorldService should be invoked 1 time
    BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
    assertThat(metricRegistry.getMetrics()).hasSize(4);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName." + SUCCESSFUL_CALLS_WITH_RETRY).getValue()).isEqualTo(0L);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName." + SUCCESSFUL_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(1L);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName." + FAILED_CALLS_WITH_RETRY).getValue()).isEqualTo(0L);
    assertThat(metricRegistry.getGauges().get("testPrefix.testName." + FAILED_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(0L);
}
Also used : RetryRegistry(io.github.resilience4j.retry.RetryRegistry) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 2 with Retry

use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.

the class RetryMetricsTest method shouldRegisterMetricsWithoutRetry.

@Test
public void shouldRegisterMetricsWithoutRetry() throws Throwable {
    // Given
    RetryRegistry retryRegistry = RetryRegistry.ofDefaults();
    Retry retry = retryRegistry.retry("testName");
    metricRegistry.registerAll(RetryMetrics.ofRetryRegistry(retryRegistry));
    // Given the HelloWorldService returns Hello world
    BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
    // Setup circuitbreaker with retry
    String value = retry.executeSupplier(helloWorldService::returnHelloWorld);
    // Then
    assertThat(value).isEqualTo("Hello world");
    // Then the helloWorldService should be invoked 1 time
    BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
    assertThat(metricRegistry.getMetrics()).hasSize(4);
    assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITH_RETRY).getValue()).isEqualTo(0L);
    assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(1L);
    assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + FAILED_CALLS_WITH_RETRY).getValue()).isEqualTo(0L);
    assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + FAILED_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(0L);
}
Also used : RetryRegistry(io.github.resilience4j.retry.RetryRegistry) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 3 with Retry

use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.

the class EventPublisherTest method shouldIgnoreError.

@Test
public void shouldIgnoreError() {
    // Given the HelloWorldService throws an exception
    BDDMockito.willThrow(new WebServiceException("BAM!")).willNothing().given(helloWorldService).sayHelloWorld();
    // Create a Retry with default configuration
    RetryConfig config = RetryConfig.custom().retryOnException(t -> t instanceof IOException).maxAttempts(3).build();
    Retry retry = Retry.of("id", config);
    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(1)).sayHelloWorld();
    // and the result should be a sucess
    Assertions.assertThat(result.isFailure()).isTrue();
    Assertions.assertThat(sleptTime).isEqualTo(0);
    testSubscriber.assertValueCount(1).assertValues(RetryEvent.Type.IGNORED_ERROR);
}
Also used : RetryConfig(io.github.resilience4j.retry.RetryConfig) WebServiceException(javax.xml.ws.WebServiceException) CheckedRunnable(io.vavr.CheckedRunnable) IOException(java.io.IOException) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 4 with Retry

use of io.github.resilience4j.retry.Retry 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 5 with Retry

use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.

the class RunnableRetryTest method shouldNotRetry.

@Test
public void shouldNotRetry() {
    // Create a Retry with default configuration
    Retry retryContext = Retry.ofDefaults("id");
    // Decorate the invocation of the HelloWorldService
    Runnable runnable = Retry.decorateRunnable(retryContext, helloWorldService::sayHelloWorld);
    // When
    runnable.run();
    // Then the helloWorldService should be invoked 1 time
    BDDMockito.then(helloWorldService).should(Mockito.times(1)).sayHelloWorld();
    Assertions.assertThat(sleptTime).isEqualTo(0);
}
Also used : CheckedRunnable(io.vavr.CheckedRunnable) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Aggregations

Retry (io.github.resilience4j.retry.Retry)39 Test (org.junit.Test)34 WebServiceException (javax.xml.ws.WebServiceException)29 RetryConfig (io.github.resilience4j.retry.RetryConfig)17 CheckedRunnable (io.vavr.CheckedRunnable)9 RetryRegistry (io.github.resilience4j.retry.RetryRegistry)6 IOException (java.io.IOException)4 IntervalFunction (io.github.resilience4j.retry.IntervalFunction)2 HelloWorldService (io.github.resilience4j.test.HelloWorldService)2 API (io.vavr.API)2 Predicates (io.vavr.Predicates)2 Try (io.vavr.control.Try)2 Duration (java.time.Duration)2 RetryHandler (org.apache.servicecomb.governance.handler.RetryHandler)2 Before (org.junit.Before)2 BDDMockito (org.mockito.BDDMockito)2 Mockito (org.mockito.Mockito)2 RxJava2Adapter.toFlowable (io.github.resilience4j.adapter.RxJava2Adapter.toFlowable)1 CircularEventConsumer (io.github.resilience4j.consumer.CircularEventConsumer)1 EventConsumerRegistry (io.github.resilience4j.consumer.EventConsumerRegistry)1