This is an automated email from the ASF dual-hosted git repository.
randgalt pushed a commit to branch CURATOR-505
in repository https://gitbox.apache.org/repos/asf/curator.git
The following commit(s) were added to refs/heads/CURATOR-505 by this push:
new 6e4466f CURATOR-505 - fix elapsed calculation in the circuit breaker
6e4466f is described below
commit 6e4466ffae5f0a354de8bdc98ac82f20009cd630
Author: randgalt <[email protected]>
AuthorDate: Tue Feb 12 23:30:03 2019 -0500
CURATOR-505 - fix elapsed calculation in the circuit breaker
---
.../org/apache/curator/framework/state/CircuitBreaker.java | 4 +++-
.../apache/curator/framework/state/TestCircuitBreaker.java | 11 +++++++++++
2 files changed, 14 insertions(+), 1 deletion(-)
diff --git
a/curator-framework/src/main/java/org/apache/curator/framework/state/CircuitBreaker.java
b/curator-framework/src/main/java/org/apache/curator/framework/state/CircuitBreaker.java
index 78dc9af..03e44f8 100644
---
a/curator-framework/src/main/java/org/apache/curator/framework/state/CircuitBreaker.java
+++
b/curator-framework/src/main/java/org/apache/curator/framework/state/CircuitBreaker.java
@@ -20,6 +20,7 @@ package org.apache.curator.framework.state;
import org.apache.curator.RetryPolicy;
import org.apache.curator.RetrySleeper;
+import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@@ -77,7 +78,8 @@ class CircuitBreaker
long[] sleepTimeNanos = new long[]{0L};
RetrySleeper retrySleeper = (time, unit) -> sleepTimeNanos[0] =
unit.toNanos(time);
- if ( retryPolicy.allowRetry(retryCount, System.nanoTime() -
startNanos, retrySleeper) )
+ Duration elapsedTime = Duration.ofNanos(System.nanoTime() -
startNanos);
+ if ( retryPolicy.allowRetry(retryCount, elapsedTime.toMillis(),
retrySleeper) )
{
++retryCount;
service.schedule(completion, sleepTimeNanos[0],
TimeUnit.NANOSECONDS);
diff --git
a/curator-framework/src/test/java/org/apache/curator/framework/state/TestCircuitBreaker.java
b/curator-framework/src/test/java/org/apache/curator/framework/state/TestCircuitBreaker.java
index 37833b9..f4096cb 100644
---
a/curator-framework/src/test/java/org/apache/curator/framework/state/TestCircuitBreaker.java
+++
b/curator-framework/src/test/java/org/apache/curator/framework/state/TestCircuitBreaker.java
@@ -18,8 +18,10 @@
*/
package org.apache.curator.framework.state;
+import org.apache.curator.RetryPolicy;
import org.apache.curator.retry.RetryForever;
import org.apache.curator.retry.RetryNTimes;
+import org.apache.curator.retry.RetryUntilElapsed;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
@@ -84,4 +86,13 @@ public class TestCircuitBreaker
Assert.assertTrue(circuitBreaker.close());
Assert.assertFalse(circuitBreaker.close());
}
+
+ @Test
+ public void testWithRetryUntilElapsed()
+ {
+ RetryPolicy retryPolicy = new RetryUntilElapsed(10000, 10000);
+ CircuitBreaker circuitBreaker = new CircuitBreaker(retryPolicy,
service);
+ Assert.assertTrue(circuitBreaker.tryToOpen(() -> {}));
+ Assert.assertEquals(lastDelay[0], Duration.ofMillis(10000));
+ }
}