Search in sources :

Example 1 with CheckedRunnable

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

the class RateLimiterTest method decorateCheckedRunnable.

@Test
public void decorateCheckedRunnable() throws Throwable {
    CheckedRunnable runnable = mock(CheckedRunnable.class);
    CheckedRunnable decorated = RateLimiter.decorateCheckedRunnable(limit, runnable);
    when(limit.getPermission(config.getTimeoutDuration())).thenReturn(false);
    Try decoratedRunnableResult = Try.run(decorated);
    then(decoratedRunnableResult.isFailure()).isTrue();
    then(decoratedRunnableResult.getCause()).isInstanceOf(RequestNotPermitted.class);
    verify(runnable, never()).run();
    when(limit.getPermission(config.getTimeoutDuration())).thenReturn(true);
    Try secondRunnableResult = Try.run(decorated);
    then(secondRunnableResult.isSuccess()).isTrue();
    verify(runnable, times(1)).run();
}
Also used : CheckedRunnable(io.vavr.CheckedRunnable) Try(io.vavr.control.Try) Test(org.junit.Test)

Example 2 with CheckedRunnable

use of io.vavr.CheckedRunnable 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 3 with CheckedRunnable

use of io.vavr.CheckedRunnable 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 4 with CheckedRunnable

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

the class RunnableRetryTest method shouldTakeIntoAccountBackoffFunction.

@Test
public void shouldTakeIntoAccountBackoffFunction() {
    // Given the HelloWorldService throws an exception
    BDDMockito.willThrow(new WebServiceException("BAM!")).given(helloWorldService).sayHelloWorld();
    // Create a Retry with a backoff function squaring the interval
    RetryConfig config = RetryConfig.custom().intervalFunction(IntervalFunction.of(Duration.ofMillis(500), x -> x * x)).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 slept time should be according to the backoff function
    BDDMockito.then(helloWorldService).should(Mockito.times(3)).sayHelloWorld();
    Assertions.assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION + RetryConfig.DEFAULT_WAIT_DURATION * RetryConfig.DEFAULT_WAIT_DURATION);
}
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 5 with CheckedRunnable

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

the class DecoratorsTest method testDecorateCheckedRunnable.

@Test
public void testDecorateCheckedRunnable() throws IOException {
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    CheckedRunnable decoratedRunnable = Decorators.ofCheckedRunnable(() -> helloWorldService.sayHelloWorldWithException()).withCircuitBreaker(circuitBreaker).withRetry(Retry.ofDefaults("id")).withRateLimiter(RateLimiter.ofDefaults("testName")).withBulkhead(Bulkhead.ofDefaults("testName")).decorate();
    Try.run(decoratedRunnable);
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
    assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1);
    // Then the helloWorldService should be invoked 1 time
    BDDMockito.then(helloWorldService).should(times(1)).sayHelloWorldWithException();
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CheckedRunnable(io.vavr.CheckedRunnable) 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