Search in sources :

Example 1 with CheckedRunnable

use of io.github.resilience4j.core.functions.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.run());
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
    assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1);
    then(helloWorldService).should(times(1)).sayHelloWorldWithException();
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CheckedRunnable(io.github.resilience4j.core.functions.CheckedRunnable) Test(org.junit.Test)

Example 2 with CheckedRunnable

use of io.github.resilience4j.core.functions.CheckedRunnable in project resilience4j by resilience4j.

the class CircuitBreakerTest method shouldReturnFailureWithRuntimeException.

@Test
public void shouldReturnFailureWithRuntimeException() {
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");
    assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED);
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0);
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(0);
    CheckedRunnable checkedRunnable = circuitBreaker.decorateCheckedRunnable(() -> {
        throw new RuntimeException("BAM!");
    });
    Try result = Try.run(() -> checkedRunnable.run());
    assertThat(result.isFailure()).isTrue();
    assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
}
Also used : CheckedRunnable(io.github.resilience4j.core.functions.CheckedRunnable) Try(io.vavr.control.Try) Test(org.junit.Test)

Example 3 with CheckedRunnable

use of io.github.resilience4j.core.functions.CheckedRunnable in project resilience4j by resilience4j.

the class CircuitBreakerTest method shouldNotRecordIOExceptionAsAFailure.

@Test
public void shouldNotRecordIOExceptionAsAFailure() {
    // tag::shouldNotRecordIOExceptionAsAFailure[]
    CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom().slidingWindowSize(2).permittedNumberOfCallsInHalfOpenState(2).waitDurationInOpenState(Duration.ofMillis(1000)).ignoreExceptions(IOException.class).build();
    CircuitBreaker circuitBreaker = CircuitBreaker.of("testName", circuitBreakerConfig);
    // Simulate a failure attempt
    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new HelloWorldException());
    // CircuitBreaker is still CLOSED, because 1 failure is allowed
    assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED);
    CheckedRunnable checkedRunnable = circuitBreaker.decorateCheckedRunnable(() -> {
        throw new SocketTimeoutException("BAM!");
    });
    Try result = Try.run(() -> checkedRunnable.run());
    assertThat(result.isFailure()).isTrue();
    // CircuitBreaker is still CLOSED, because SocketTimeoutException has not been recorded as a failure
    assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED);
    assertThat(result.failed().get()).isInstanceOf(IOException.class);
    // end::shouldNotRecordIOExceptionAsAFailure[]
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) CheckedRunnable(io.github.resilience4j.core.functions.CheckedRunnable) HelloWorldException(io.github.resilience4j.test.HelloWorldException) Try(io.vavr.control.Try) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with CheckedRunnable

use of io.github.resilience4j.core.functions.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);
    given(limit.acquirePermission(1)).willReturn(false);
    Try decoratedRunnableResult = Try.run(() -> decorated.run());
    assertThat(decoratedRunnableResult.isFailure()).isTrue();
    assertThat(decoratedRunnableResult.getCause()).isInstanceOf(RequestNotPermitted.class);
    then(runnable).should(never()).run();
    given(limit.acquirePermission(1)).willReturn(true);
    Try secondRunnableResult = Try.run(() -> decorated.run());
    assertThat(secondRunnableResult.isSuccess()).isTrue();
    then(runnable).should().run();
}
Also used : CheckedRunnable(io.github.resilience4j.core.functions.CheckedRunnable) Try(io.vavr.control.Try) Test(org.junit.Test)

Example 5 with CheckedRunnable

use of io.github.resilience4j.core.functions.CheckedRunnable in project resilience4j by resilience4j.

the class EventPublisherTest method shouldIgnoreError.

@Test
public void shouldIgnoreError() {
    willThrow(new HelloWorldException()).willDoNothing().given(helloWorldService).sayHelloWorld();
    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();
    CheckedRunnable retryableRunnable = Retry.decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld);
    Try<Void> result = Try.run(() -> retryableRunnable.run());
    then(helloWorldService).should().sayHelloWorld();
    assertThat(result.isFailure()).isTrue();
    assertThat(sleptTime).isEqualTo(0);
    testSubscriber.assertValueCount(1).assertValues(RetryEvent.Type.IGNORED_ERROR);
}
Also used : RetryConfig(io.github.resilience4j.retry.RetryConfig) CheckedRunnable(io.github.resilience4j.core.functions.CheckedRunnable) HelloWorldException(io.github.resilience4j.test.HelloWorldException) IOException(java.io.IOException) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Aggregations

CheckedRunnable (io.github.resilience4j.core.functions.CheckedRunnable)20 Test (org.junit.Test)20 Try (io.vavr.control.Try)9 HelloWorldException (io.github.resilience4j.test.HelloWorldException)8 Retry (io.github.resilience4j.retry.Retry)7 RetryConfig (io.github.resilience4j.retry.RetryConfig)4 IOException (java.io.IOException)2 CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)1 IntervalFunction (io.github.resilience4j.core.IntervalFunction)1 HelloWorldService (io.github.resilience4j.test.HelloWorldService)1 API (io.vavr.API)1 Predicates (io.vavr.Predicates)1 SocketTimeoutException (java.net.SocketTimeoutException)1 Duration (java.time.Duration)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Before (org.junit.Before)1 BDDMockito.then (org.mockito.BDDMockito.then)1 BDDMockito.willThrow (org.mockito.BDDMockito.willThrow)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.times (org.mockito.Mockito.times)1