use of io.github.resilience4j.retry.RetryConfig in project resilience4j by resilience4j.
the class RetryTransformerTest method shouldReturnOnCompleteUsingFlowable.
@Test
public void shouldReturnOnCompleteUsingFlowable() {
// Given
RetryConfig config = RetryConfig.ofDefaults();
Retry retry = Retry.of("testName", config);
RetryTransformer<Object> retryTransformer = RetryTransformer.of(retry);
given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
// When
Flowable.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertError(WebServiceException.class).assertNotComplete().assertSubscribed();
Flowable.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertError(WebServiceException.class).assertNotComplete().assertSubscribed();
// Then
BDDMockito.then(helloWorldService).should(Mockito.times(6)).returnHelloWorld();
Retry.Metrics metrics = retry.getMetrics();
assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(2);
assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
use of io.github.resilience4j.retry.RetryConfig 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);
}
use of io.github.resilience4j.retry.RetryConfig in project resilience4j by resilience4j.
the class SupplierRetryTest method shouldReturnAfterOneAttempt.
@Test
public void shouldReturnAfterOneAttempt() {
// Given the HelloWorldService throws an exception
BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
// Create a Retry with custom configuration
RetryConfig config = RetryConfig.custom().maxAttempts(1).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 1 time
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);
}
use of io.github.resilience4j.retry.RetryConfig in project resilience4j by resilience4j.
the class RetryTransformerTest method shouldReturnOnErrorUsingSingle.
@Test
public void shouldReturnOnErrorUsingSingle() {
// Given
RetryConfig config = RetryConfig.ofDefaults();
Retry retry = Retry.of("testName", config);
RetryTransformer<Object> retryTransformer = RetryTransformer.of(retry);
given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
// When
Single.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertError(WebServiceException.class).assertNotComplete().assertSubscribed();
Single.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertError(WebServiceException.class).assertNotComplete().assertSubscribed();
// Then
BDDMockito.then(helloWorldService).should(Mockito.times(6)).returnHelloWorld();
Retry.Metrics metrics = retry.getMetrics();
assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(2);
assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
use of io.github.resilience4j.retry.RetryConfig in project resilience4j by resilience4j.
the class RetryTransformerTest method shouldReturnOnErrorUsingObservable.
@Test
public void shouldReturnOnErrorUsingObservable() {
// Given
RetryConfig config = RetryConfig.ofDefaults();
Retry retry = Retry.of("testName", config);
RetryTransformer<Object> retryTransformer = RetryTransformer.of(retry);
given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
// When
Observable.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertError(WebServiceException.class).assertNotComplete().assertSubscribed();
Observable.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertError(WebServiceException.class).assertNotComplete().assertSubscribed();
// Then
BDDMockito.then(helloWorldService).should(Mockito.times(6)).returnHelloWorld();
Retry.Metrics metrics = retry.getMetrics();
assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(2);
assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
Aggregations