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