Skip to content
Permalink
Browse files
CURATOR-505 - fix elapsed calculation in the circuit breaker
  • Loading branch information
randgalt committed Feb 13, 2019
1 parent 2e980e7 commit 6e4466ffae5f0a354de8bdc98ac82f20009cd630
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
@@ -20,6 +20,7 @@

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 @@ boolean tryToRetry(Runnable completion)

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);
@@ -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 void testVariousOpenRetryFails()
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));
}
}

0 comments on commit 6e4466f

Please sign in to comment.