CURATOR-505 - fix elapsed calculation in the circuit breaker CURATOR-505
authorrandgalt <randgalt@apache.org>
Wed, 13 Feb 2019 04:30:03 +0000 (23:30 -0500)
committerrandgalt <randgalt@apache.org>
Wed, 13 Feb 2019 04:30:03 +0000 (23:30 -0500)
curator-framework/src/main/java/org/apache/curator/framework/state/CircuitBreaker.java
curator-framework/src/test/java/org/apache/curator/framework/state/TestCircuitBreaker.java

index 78dc9af..03e44f8 100644 (file)
@@ -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);
index 37833b9..f4096cb 100644 (file)
  */
 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));
+    }
 }