converted to nanos as currenttimemillis is only accurate to 10ms

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2307 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-11-22 18:20:56 +00:00
parent 93cb460058
commit 839cf3d26c
2 changed files with 12 additions and 12 deletions

View File

@ -110,7 +110,7 @@ public class BackoffLimitedRetryHandler implements HttpRetryHandler {
public void imposeBackoffExponentialDelay(long period, int pow, int failureCount, public void imposeBackoffExponentialDelay(long period, int pow, int failureCount,
String commandDescription) { String commandDescription) {
long delayMs = (long) (period * Math.pow(failureCount, pow)); long delayMs = (long) (period * Math.pow(failureCount, pow));
logger.debug("Retry %1$d/%2$d after server error, delaying for %3$d ms: %4$s", failureCount, logger.debug("Retry %d/%d after server error, delaying for %d ms: %s", failureCount,
retryCountLimit, delayMs, commandDescription); retryCountLimit, delayMs, commandDescription);
try { try {
Thread.sleep(delayMs); Thread.sleep(delayMs);

View File

@ -56,34 +56,34 @@ public class BackoffLimitedRetryHandlerTest {
void testExponentialBackoffDelay() throws InterruptedException { void testExponentialBackoffDelay() throws InterruptedException {
long acceptableDelay = 25; // Delay to forgive if tests run long. long acceptableDelay = 25; // Delay to forgive if tests run long.
long startTime = System.currentTimeMillis(); long startTime = System.nanoTime();
handler.imposeBackoffExponentialDelay(1, "TEST FAILURE: 1"); handler.imposeBackoffExponentialDelay(1, "TEST FAILURE: 1");
long elapsedTime = System.currentTimeMillis() - startTime; long elapsedTime = (System.nanoTime() - startTime)/1000000;
assertTrue(elapsedTime >= 50); assertTrue(elapsedTime >= 50);
assertTrue(elapsedTime < 50 + acceptableDelay); assertTrue(elapsedTime < 50 + acceptableDelay);
startTime = System.currentTimeMillis(); startTime = System.nanoTime();
handler.imposeBackoffExponentialDelay(2, "TEST FAILURE: 2"); handler.imposeBackoffExponentialDelay(2, "TEST FAILURE: 2");
elapsedTime = System.currentTimeMillis() - startTime; elapsedTime = (System.nanoTime() - startTime)/1000000;
assertTrue(elapsedTime >= 200); assertTrue(elapsedTime >= 200);
assertTrue(elapsedTime < 200 + acceptableDelay); assertTrue(elapsedTime < 200 + acceptableDelay);
startTime = System.currentTimeMillis(); startTime = System.nanoTime();
handler.imposeBackoffExponentialDelay(3, "TEST FAILURE: 3"); handler.imposeBackoffExponentialDelay(3, "TEST FAILURE: 3");
elapsedTime = System.currentTimeMillis() - startTime; elapsedTime = (System.nanoTime() - startTime)/1000000;
assertTrue(elapsedTime >= 450); assertTrue(elapsedTime >= 450);
assertTrue(elapsedTime < 450 + acceptableDelay); assertTrue(elapsedTime < 450 + acceptableDelay);
startTime = System.currentTimeMillis(); startTime = System.nanoTime();
handler.imposeBackoffExponentialDelay(4, "TEST FAILURE: 4"); handler.imposeBackoffExponentialDelay(4, "TEST FAILURE: 4");
elapsedTime = System.currentTimeMillis() - startTime; elapsedTime = (System.nanoTime() - startTime)/1000000;
assertTrue(elapsedTime >= 800); assertTrue(elapsedTime >= 800);
assertTrue(elapsedTime < 800 + acceptableDelay); assertTrue(elapsedTime < 800 + acceptableDelay);
startTime = System.currentTimeMillis(); startTime = System.nanoTime();
handler.imposeBackoffExponentialDelay(5, "TEST FAILURE: 5"); handler.imposeBackoffExponentialDelay(5, "TEST FAILURE: 5");
elapsedTime = System.currentTimeMillis() - startTime; elapsedTime = (System.nanoTime() - startTime)/1000000;
assertTrue(elapsedTime >= 1250); assert (elapsedTime >= 1249) : elapsedTime ;
assertTrue(elapsedTime < 1250 + acceptableDelay); assertTrue(elapsedTime < 1250 + acceptableDelay);
} }