Last active
June 21, 2024 04:26
-
-
Save GSuaki/6d7bb7228417c37a0afcd5241d11d71f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void testCB() { | |
final CircuitBreaker breaker = CircuitBreaker.of("", CircuitBreakerConfig.custom() | |
.slidingWindowSize(60) | |
.slidingWindowType(SlidingWindowType.TIME_BASED) | |
.failureRateThreshold(80.0f) | |
.minimumNumberOfCalls(200) | |
.permittedNumberOfCallsInHalfOpenState(50) | |
.waitDurationInOpenState(Duration.ofSeconds(30)) | |
.build()); | |
final AtomicInteger calls = new AtomicInteger(); | |
final AtomicInteger errors = new AtomicInteger(); | |
final AtomicInteger notPermitted = new AtomicInteger(); | |
final AtomicBoolean ahopen = new AtomicBoolean(); | |
final Supplier<String> supplier = breaker.decorateSupplier(() -> { | |
boolean hopen = State.HALF_OPEN.equals(breaker.getState()); | |
boolean odd = calls.incrementAndGet() % (hopen ? 5 : 5) == 0; | |
if (hopen) { | |
ahopen.set(true); | |
System.out.println("CALL IN HALF-OPEN"); | |
} | |
if (!odd) { | |
throw new IllegalArgumentException(); | |
} else { | |
return ""; | |
} | |
}); | |
for (int i = 0; i < 1000; i++) { | |
try { | |
Thread.sleep(130); | |
supplier.get(); | |
} catch (Throwable t) { | |
System.out.println("Calls count: " + calls.get()); | |
System.out.println("Errors count: " + errors.get()); | |
System.out.println("Failure rate: " + breaker.getMetrics().getFailureRate()); | |
System.out.println(); | |
System.out.println(); | |
if (t instanceof IllegalArgumentException) { | |
errors.incrementAndGet(); | |
} else if (t instanceof CallNotPermittedException) { | |
notPermitted.incrementAndGet(); | |
} | |
} | |
} | |
System.out.println(); | |
System.out.println(); | |
System.out.println("Calls count: " + calls.get()); | |
System.out.println("Errors count: " + errors.get()); | |
System.out.println("Not permitted count: " + notPermitted.get()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment