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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations