From 7d061e33e59e23dc4b03378f35f50a7d70f033b3 Mon Sep 17 00:00:00 2001 From: Andre Dieb Martins Date: Wed, 22 Nov 2017 13:44:23 -0500 Subject: [PATCH] LANG-1370 Fix EventCountCircuitBreaker increment batch Fixes #incrementAndCheckState(Integer increment) by passing the increment downstream. --- .../concurrent/EventCountCircuitBreaker.java | 2 +- .../concurrent/EventCountCircuitBreakerTest.java | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/lang3/concurrent/EventCountCircuitBreaker.java b/src/main/java/org/apache/commons/lang3/concurrent/EventCountCircuitBreaker.java index b40213e9b..dd282dc9b 100644 --- a/src/main/java/org/apache/commons/lang3/concurrent/EventCountCircuitBreaker.java +++ b/src/main/java/org/apache/commons/lang3/concurrent/EventCountCircuitBreaker.java @@ -271,7 +271,7 @@ public class EventCountCircuitBreaker extends AbstractCircuitBreaker { @Override public boolean incrementAndCheckState(final Integer increment) throws CircuitBreakingException { - return performStateCheck(1); + return performStateCheck(increment); } /** diff --git a/src/test/java/org/apache/commons/lang3/concurrent/EventCountCircuitBreakerTest.java b/src/test/java/org/apache/commons/lang3/concurrent/EventCountCircuitBreakerTest.java index 1c9e79411..0053554e1 100644 --- a/src/test/java/org/apache/commons/lang3/concurrent/EventCountCircuitBreakerTest.java +++ b/src/test/java/org/apache/commons/lang3/concurrent/EventCountCircuitBreakerTest.java @@ -154,6 +154,21 @@ public class EventCountCircuitBreakerTest { assertFalse("Closed", breaker.isClosed()); } + /** + * Tests that the circuit breaker opens if all conditions are met when using + * {@link EventCountCircuitBreaker#incrementAndCheckState(Integer increment)}. + */ + @Test + public void testOpeningWhenThresholdReachedThroughBatch() { + final long timeIncrement = NANO_FACTOR / OPENING_THRESHOLD - 1; + final EventCountCircuitBreakerTestImpl breaker = new EventCountCircuitBreakerTestImpl(OPENING_THRESHOLD, 1, + TimeUnit.SECONDS, CLOSING_THRESHOLD, 1, TimeUnit.SECONDS); + long startTime = timeIncrement * (OPENING_THRESHOLD + 1); + boolean open = !breaker.at(startTime).incrementAndCheckState(OPENING_THRESHOLD + 1); + assertTrue("Not open", open); + assertFalse("Closed", breaker.isClosed()); + } + /** * Tests that an open circuit breaker does not close itself when the number of events * received is over the threshold.