Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bael 8953 - Difference Between Circuit Breaker and Retry in Spring Boot #18084

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions libraries-5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@
<artifactId>lanterna</artifactId>
<version>${lanterna.version}</version>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-retry</artifactId>
<version>${resilience4j.version}</version>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-circuitbreaker</artifactId>
<version>${resilience4j.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -149,6 +159,7 @@
<armeria.version>1.29.2</armeria.version>
<yauaa.version>7.28.1</yauaa.version>
<yavi.version>0.14.1</yavi.version>
<resilience4j.version>2.1.0</resilience4j.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.baeldung.resilience4j;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Before;
import org.junit.Test;

import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
import io.github.resilience4j.core.IntervalFunction;
import io.github.resilience4j.retry.Retry;
import io.github.resilience4j.retry.RetryConfig;

public class CircuitBreakerVsRetryUnitTest {

interface PaymentService {

String processPayment(int i);
}

private PaymentService paymentService;

@Before
public void setUp() {
paymentService = mock(PaymentService.class);
}

@Test
public void whenRetryWithExponentialBackoffIsUsed_thenItRetriesAndSucceeds() {
IntervalFunction intervalFn = IntervalFunction.ofExponentialBackoff(1000, 2);
RetryConfig retryConfig = RetryConfig.custom()
.maxAttempts(5)
.intervalFunction(intervalFn)
.build();

Retry retry = Retry.of("paymentRetry", retryConfig);

when(paymentService.processPayment(1)).thenThrow(new RuntimeException("First Failure"))
.thenThrow(new RuntimeException("Second Failure"))
.thenReturn("Success");

Callable<String> decoratedCallable = Retry.decorateCallable(retry, () -> paymentService.processPayment(1));

try {
String result = decoratedCallable.call();
assertEquals("Success", result);
} catch (Exception ignored) {

}

verify(paymentService, times(3)).processPayment(1);
}

@Test
public void whenCircuitBreakerTransitionsThroughStates_thenBehaviorIsVerified() {
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
.failureRateThreshold(50)
.slidingWindowSize(5)
.permittedNumberOfCallsInHalfOpenState(3)
.build();

CircuitBreaker circuitBreaker = CircuitBreaker.of("paymentCircuitBreaker", circuitBreakerConfig);

AtomicInteger callCount = new AtomicInteger(0);

when(paymentService.processPayment(anyInt())).thenAnswer(invocationOnMock -> {
callCount.incrementAndGet();
throw new RuntimeException("Service Failure");
});

Callable<String> decoratedCallable = CircuitBreaker.decorateCallable(circuitBreaker, () -> paymentService.processPayment(1));

for (int i = 0; i < 10; i++) {
try {
decoratedCallable.call();
} catch (Exception ignored) {

}
}

assertEquals(5, callCount.get());
assertEquals(CircuitBreaker.State.OPEN, circuitBreaker.getState());

callCount.set(0);
circuitBreaker.transitionToHalfOpenState();

assertEquals(CircuitBreaker.State.HALF_OPEN, circuitBreaker.getState());
reset(paymentService);
when(paymentService.processPayment(anyInt())).thenAnswer(invocationOnMock -> {
callCount.incrementAndGet();
return "Success";
});

for (int i = 0; i < 3; i++) {
try {
decoratedCallable.call();
} catch (Exception ignored) {

}
}

assertEquals(3, callCount.get());
assertEquals(CircuitBreaker.State.CLOSED, circuitBreaker.getState());
}
}