|
| 1 | +package com.baeldung.resilience4j; |
| 2 | + |
| 3 | +import io.github.resilience4j.bulkhead.Bulkhead; |
| 4 | +import io.github.resilience4j.bulkhead.BulkheadConfig; |
| 5 | +import io.github.resilience4j.bulkhead.BulkheadRegistry; |
| 6 | +import io.github.resilience4j.circuitbreaker.CircuitBreaker; |
| 7 | +import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig; |
| 8 | +import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry; |
| 9 | +import io.github.resilience4j.retry.Retry; |
| 10 | +import io.github.resilience4j.retry.RetryConfig; |
| 11 | +import io.github.resilience4j.retry.RetryRegistry; |
| 12 | +import io.github.resilience4j.timelimiter.TimeLimiter; |
| 13 | +import io.github.resilience4j.timelimiter.TimeLimiterConfig; |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +import java.time.Duration; |
| 18 | +import java.util.concurrent.*; |
| 19 | +import java.util.function.Function; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.assertj.core.api.Assertions.fail; |
| 23 | +import static org.mockito.ArgumentMatchers.any; |
| 24 | +import static org.mockito.Mockito.*; |
| 25 | + |
| 26 | +public class Resilience4jUnitTest { |
| 27 | + |
| 28 | + interface RemoteService { |
| 29 | + |
| 30 | + int process(int i); |
| 31 | + } |
| 32 | + |
| 33 | + private RemoteService service; |
| 34 | + |
| 35 | + @Before |
| 36 | + public void setUp() { |
| 37 | + service = mock(RemoteService.class); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void whenCircuitBreakerIsUsed_thenItWorksAsExpected() { |
| 42 | + CircuitBreakerConfig config = CircuitBreakerConfig.custom() |
| 43 | + // Percentage of failures to start short-circuit |
| 44 | + .failureRateThreshold(20) |
| 45 | + // Min number of call attempts |
| 46 | + .ringBufferSizeInClosedState(5) |
| 47 | + .build(); |
| 48 | + CircuitBreakerRegistry registry = CircuitBreakerRegistry.of(config); |
| 49 | + CircuitBreaker circuitBreaker = registry.circuitBreaker("my"); |
| 50 | + Function<Integer, Integer> decorated = CircuitBreaker.decorateFunction(circuitBreaker, service::process); |
| 51 | + |
| 52 | + when(service.process(anyInt())).thenThrow(new RuntimeException()); |
| 53 | + |
| 54 | + for (int i = 0; i < 10; i++) { |
| 55 | + try { |
| 56 | + decorated.apply(i); |
| 57 | + } catch (Exception ignore) { |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + verify(service, times(5)).process(any(Integer.class)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void whenBulkheadIsUsed_thenItWorksAsExpected() throws InterruptedException { |
| 66 | + BulkheadConfig config = BulkheadConfig.custom().maxConcurrentCalls(1).build(); |
| 67 | + BulkheadRegistry registry = BulkheadRegistry.of(config); |
| 68 | + Bulkhead bulkhead = registry.bulkhead("my"); |
| 69 | + Function<Integer, Integer> decorated = Bulkhead.decorateFunction(bulkhead, service::process); |
| 70 | + |
| 71 | + Future<?> taskInProgress = callAndBlock(decorated); |
| 72 | + try { |
| 73 | + assertThat(bulkhead.isCallPermitted()).isFalse(); |
| 74 | + } finally { |
| 75 | + taskInProgress.cancel(true); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private Future<?> callAndBlock(Function<Integer, Integer> decoratedService) throws InterruptedException { |
| 80 | + CountDownLatch latch = new CountDownLatch(1); |
| 81 | + when(service.process(anyInt())).thenAnswer(invocation -> { |
| 82 | + latch.countDown(); |
| 83 | + Thread.currentThread().join(); |
| 84 | + return null; |
| 85 | + }); |
| 86 | + |
| 87 | + ForkJoinTask<?> result = ForkJoinPool.commonPool().submit(() -> { |
| 88 | + decoratedService.apply(1); |
| 89 | + }); |
| 90 | + latch.await(); |
| 91 | + return result; |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void whenRetryIsUsed_thenItWorksAsExpected() { |
| 96 | + RetryConfig config = RetryConfig.custom().maxAttempts(2).build(); |
| 97 | + RetryRegistry registry = RetryRegistry.of(config); |
| 98 | + Retry retry = registry.retry("my"); |
| 99 | + Function<Integer, Void> decorated = Retry.decorateFunction(retry, (Integer s) -> { |
| 100 | + service.process(s); |
| 101 | + return null; |
| 102 | + }); |
| 103 | + |
| 104 | + when(service.process(anyInt())).thenThrow(new RuntimeException()); |
| 105 | + try { |
| 106 | + decorated.apply(1); |
| 107 | + fail("Expected an exception to be thrown if all retries failed"); |
| 108 | + } catch (Exception e) { |
| 109 | + verify(service, times(2)).process(any(Integer.class)); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @SuppressWarnings("unchecked") |
| 114 | + @Test |
| 115 | + public void whenTimeLimiterIsUsed_thenItWorksAsExpected() throws Exception { |
| 116 | + long ttl = 1; |
| 117 | + TimeLimiterConfig config = TimeLimiterConfig.custom().timeoutDuration(Duration.ofMillis(ttl)).build(); |
| 118 | + TimeLimiter timeLimiter = TimeLimiter.of(config); |
| 119 | + |
| 120 | + Future futureMock = mock(Future.class); |
| 121 | + Callable restrictedCall = TimeLimiter.decorateFutureSupplier(timeLimiter, () -> futureMock); |
| 122 | + restrictedCall.call(); |
| 123 | + |
| 124 | + verify(futureMock).get(ttl, TimeUnit.MILLISECONDS); |
| 125 | + } |
| 126 | +} |
0 commit comments