Skip to content

Instantly share code, notes, and snippets.

View xlucasdemelo's full-sized avatar
🇪🇸

Lucas Aguiar xlucasdemelo

🇪🇸
View GitHub Profile
package com.lucasaguiar.resilience;
import io.github.resilience4j.timelimiter.TimeLimiter;
import io.github.resilience4j.timelimiter.TimeLimiterConfig;
import io.vavr.control.Try;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.util.concurrent.Callable;
@Test
public void countBased_example() throws Exception {
MockResponse mockResponse = new MockResponse()
.setResponseCode(500)
.addHeader("Content-Type", "application/json;charset=utf-8");
CircuitBreaker circuitBreaker = CircuitBreaker.of("books", CircuitBreakerConfig.custom()
.slidingWindowSize(4)
.permittedNumberOfCallsInHalfOpenState(2)
.waitDurationInOpenState(Duration.ofMillis(100))
@Test
public void circuitBreaker_recover(){
MockResponse mockResponse = new MockResponse()
.setResponseCode(500)
.addHeader("Content-Type", "application/json;charset=utf-8");
CircuitBreaker circuitBreaker = CircuitBreaker.of("books", CircuitBreakerConfig.custom()
.slidingWindowSize(1)
.waitDurationInOpenState(Duration.ofMillis(100))
.build());
@Test
public void retry(){
MockResponse mockResponse = new MockResponse()
.setResponseCode(500)
.addHeader("Content-Type", "application/json;charset=utf-8");
MockResponse mockResponseSuccess = new MockResponse()
.setResponseCode(200)
.addHeader("Content-Type", "application/json;charset=utf-8");
@Test
void threadPoolBulkhead() throws InterruptedException {
//Only 5 Threads will be the hability to execute a code
ThreadPoolBulkhead threadPoolBulkhead = ThreadPoolBulkhead.of("threads", ThreadPoolBulkheadConfig.custom()
.coreThreadPoolSize(5)
.maxThreadPoolSize(5)
.build());
//Decorating the execution of the method and the configuration of the threadPool
Supplier<CompletionStage<Void>> withBulkhead = ThreadPoolBulkhead.decorateRunnable(threadPoolBulkhead, this::takeBook);