Search in sources :

Example 1 with HelloWorldService

use of io.github.resilience4j.test.HelloWorldService in project resilience4j by resilience4j.

the class BulkheadMetricsTest method setUp.

@Before
public void setUp() {
    metricRegistry = new MetricRegistry();
    helloWorldService = mock(HelloWorldService.class);
    executorService = Executors.newSingleThreadExecutor();
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) HelloWorldService(io.github.resilience4j.test.HelloWorldService) Before(org.junit.Before)

Example 2 with HelloWorldService

use of io.github.resilience4j.test.HelloWorldService 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 3 with HelloWorldService

use of io.github.resilience4j.test.HelloWorldService 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 4 with HelloWorldService

use of io.github.resilience4j.test.HelloWorldService in project resilience4j by resilience4j.

the class RetryEventPublisherTest method shouldConsumeIgnoredErrorEvent.

@Test
public void shouldConsumeIgnoredErrorEvent() {
    given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
    RetryConfig retryConfig = RetryConfig.custom().retryOnException(throwable -> Match(throwable).of(Case($(instanceOf(WebServiceException.class)), false), Case($(), true))).build();
    retry = Retry.of("testName", retryConfig);
    retry.getEventPublisher().onIgnoredError(event -> logger.info(event.getEventType().toString()));
    Try.ofSupplier(Retry.decorateSupplier(retry, helloWorldService::returnHelloWorld));
    then(logger).should(times(1)).info("IGNORED_ERROR");
    then(helloWorldService).should(times(1)).returnHelloWorld();
}
Also used : Logger(org.slf4j.Logger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BDDMockito.then(org.mockito.BDDMockito.then) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) API(io.vavr.API) Try(io.vavr.control.Try) WebServiceException(javax.xml.ws.WebServiceException) Predicates.instanceOf(io.vavr.Predicates.instanceOf) BDDMockito.given(org.mockito.BDDMockito.given) HelloWorldService(io.github.resilience4j.test.HelloWorldService) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) WebServiceException(javax.xml.ws.WebServiceException) Test(org.junit.Test)

Example 5 with HelloWorldService

use of io.github.resilience4j.test.HelloWorldService in project resilience4j by resilience4j.

the class CircuitBreakerMetricsTest method setUp.

@Before
public void setUp() {
    metricRegistry = new MetricRegistry();
    helloWorldService = mock(HelloWorldService.class);
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) HelloWorldService(io.github.resilience4j.test.HelloWorldService) Before(org.junit.Before)

Aggregations

HelloWorldService (io.github.resilience4j.test.HelloWorldService)8 Before (org.junit.Before)8 MetricRegistry (com.codahale.metrics.MetricRegistry)5 API (io.vavr.API)3 Try (io.vavr.control.Try)3 WebServiceException (javax.xml.ws.WebServiceException)3 Test (org.junit.Test)3 IntervalFunction (io.github.resilience4j.retry.IntervalFunction)2 Retry (io.github.resilience4j.retry.Retry)2 RetryConfig (io.github.resilience4j.retry.RetryConfig)2 Predicates (io.vavr.Predicates)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 BDDMockito (org.mockito.BDDMockito)2 Mockito (org.mockito.Mockito)2 API.$ (io.vavr.API.$)1 CheckedFunction0 (io.vavr.CheckedFunction0)1 CheckedRunnable (io.vavr.CheckedRunnable)1 Predicates.instanceOf (io.vavr.Predicates.instanceOf)1 Duration (java.time.Duration)1 Callable (java.util.concurrent.Callable)1