Search in sources :

Example 6 with CircuitBreaker

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();
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CheckedRunnable(io.vavr.CheckedRunnable) Test(org.junit.Test)

Example 7 with CircuitBreaker

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();
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) Test(org.junit.Test)

Example 8 with CircuitBreaker

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);
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) Test(org.junit.Test)

Example 9 with CircuitBreaker

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();
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) Test(org.junit.Test)

Example 10 with CircuitBreaker

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);
}
Also used : CircuitBreakerEventEmitter.createSseEmitter(io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventEmitter.createSseEmitter) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CircuitBreaker.decorateRunnable(io.github.resilience4j.circuitbreaker.CircuitBreaker.decorateRunnable) MediaType(org.springframework.http.MediaType) Test(org.junit.Test) IOException(java.io.IOException) BDDAssertions.then(org.assertj.core.api.BDDAssertions.then) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) ArrayList(java.util.ArrayList) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) Try(io.vavr.control.Try) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) CircuitBreakerEventDTO(io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventDTO) Duration(java.time.Duration) ReactorAdapter(io.github.resilience4j.adapter.ReactorAdapter) Type(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent.Type) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) ConcurrentModificationException(java.util.ConcurrentModificationException) CircuitBreakerEvent(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) ConcurrentModificationException(java.util.ConcurrentModificationException) MediaType(org.springframework.http.MediaType) Type(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent.Type) CircuitBreaker.decorateRunnable(io.github.resilience4j.circuitbreaker.CircuitBreaker.decorateRunnable) CircuitBreakerEventEmitter.createSseEmitter(io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventEmitter.createSseEmitter) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) Test(org.junit.Test)

Aggregations

CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)26 Test (org.junit.Test)19 CircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry)9 CircuitBreakerConfig (io.github.resilience4j.circuitbreaker.CircuitBreakerConfig)6 CircuitBreakerEvent (io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent)6 CircuitBreakerOpenException (io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException)3 InMemoryCircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.internal.InMemoryCircuitBreakerRegistry)3 Type (io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent.Type)2 CircularEventConsumer (io.github.resilience4j.consumer.CircularEventConsumer)2 EventConsumerRegistry (io.github.resilience4j.consumer.EventConsumerRegistry)2 CollectorRegistry (io.prometheus.client.CollectorRegistry)2 CheckedRunnable (io.vavr.CheckedRunnable)2 Tuple (io.vavr.Tuple)2 HashMap (io.vavr.collection.HashMap)2 HashSet (io.vavr.collection.HashSet)2 Map (io.vavr.collection.Map)2 Seq (io.vavr.collection.Seq)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 Comparator (java.util.Comparator)2