use of io.github.resilience4j.circuitbreaker.CircuitBreaker 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();
}
use of io.github.resilience4j.circuitbreaker.CircuitBreaker in project resilience4j by resilience4j.
the class DecoratorsTest method testDecorateSupplier.
@Test
public void testDecorateSupplier() {
// Given the HelloWorldService returns Hello world
given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
Supplier<String> decoratedSupplier = Decorators.ofSupplier(() -> helloWorldService.returnHelloWorld()).withCircuitBreaker(circuitBreaker).withRetry(Retry.ofDefaults("id")).withRateLimiter(RateLimiter.ofDefaults("testName")).withBulkhead(Bulkhead.ofDefaults("testName")).decorate();
String result = decoratedSupplier.get();
assertThat(result).isEqualTo("Hello world");
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)).returnHelloWorld();
}
use of io.github.resilience4j.circuitbreaker.CircuitBreaker in project resilience4j by resilience4j.
the class DecoratorsTest method testDecorateCheckedFunction.
@Test
public void testDecorateCheckedFunction() throws IOException {
// Given the HelloWorldService returns Hello world
given(helloWorldService.returnHelloWorldWithNameWithException("Name")).willReturn("Hello world Name");
CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
CheckedFunction1<String, String> decoratedFunction = Decorators.ofCheckedFunction(helloWorldService::returnHelloWorldWithNameWithException).withCircuitBreaker(circuitBreaker).withRetry(Retry.ofDefaults("id")).withRateLimiter(RateLimiter.ofDefaults("testName")).withBulkhead(Bulkhead.ofDefaults("testName")).decorate();
String result = Try.of(() -> decoratedFunction.apply("Name")).get();
assertThat(result).isEqualTo("Hello world Name");
CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1);
}
use of io.github.resilience4j.circuitbreaker.CircuitBreaker in project resilience4j by resilience4j.
the class DecoratorsTest method testDecorateCheckedSupplier.
@Test
public void testDecorateCheckedSupplier() throws IOException {
// Given the HelloWorldService returns Hello world
given(helloWorldService.returnHelloWorldWithException()).willReturn("Hello world");
CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
CheckedFunction0<String> decoratedSupplier = Decorators.ofCheckedSupplier(() -> helloWorldService.returnHelloWorldWithException()).withCircuitBreaker(circuitBreaker).withRetry(Retry.ofDefaults("id")).withRateLimiter(RateLimiter.ofDefaults("testName")).withBulkhead(Bulkhead.ofDefaults("testName")).decorate();
String result = Try.of(decoratedSupplier).get();
assertThat(result).isEqualTo("Hello world");
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)).returnHelloWorldWithException();
}
use of io.github.resilience4j.circuitbreaker.CircuitBreaker in project resilience4j by resilience4j.
the class CircuitBreakerEventEmitterTest method testEmitter.
@Test
public void testEmitter() throws IOException {
CircuitBreakerConfig config = CircuitBreakerConfig.custom().ringBufferSizeInClosedState(3).ringBufferSizeInHalfOpenState(2).failureRateThreshold(66).waitDurationInOpenState(Duration.ofSeconds(1)).recordFailure(e -> !(e instanceof IllegalArgumentException)).build();
CircuitBreaker circuitBreaker = CircuitBreakerRegistry.ofDefaults().circuitBreaker("test", config);
Runnable run = decorateRunnable(circuitBreaker, () -> System.out.println("."));
Runnable fail = decorateRunnable(circuitBreaker, () -> {
throw new ConcurrentModificationException();
});
Runnable ignore = decorateRunnable(circuitBreaker, () -> {
throw new IllegalArgumentException();
});
SseEmitter sseEmitter = createSseEmitter(ReactorAdapter.toFlux(circuitBreaker.getEventPublisher()));
TestHandler handler = new TestHandler();
sseEmitter.initialize(handler);
exec(run, 2);
exec(ignore, 1);
exec(fail, 3);
circuitBreaker.reset();
exec(run, 2);
circuitBreaker.reset();
sseEmitter.complete();
assert handler.isCompleted;
exec(run, 2);
List<CircuitBreakerEvent.Type> events = handler.events.stream().map(CircuitBreakerEventDTO::getType).collect(toList());
then(events).containsExactly(SUCCESS, SUCCESS, IGNORED_ERROR, ERROR, ERROR, STATE_TRANSITION, NOT_PERMITTED, STATE_TRANSITION, RESET, SUCCESS, SUCCESS, RESET);
}
Aggregations