Skip to content

Commit

Permalink
Issue Netflix#421 - Fixed sleep timeout going negative
Browse files Browse the repository at this point in the history
  • Loading branch information
peterox committed Oct 28, 2013
1 parent 69bd4d0 commit 76c6572
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class BoundedExponentialBackoff extends ExponentialBackoff {

private final int maxSleepTimeMs;

public BoundedExponentialBackoff(int baseSleepTimeMs, int maxSleepTimeMs, int max) {
public BoundedExponentialBackoff(long baseSleepTimeMs, int maxSleepTimeMs, int max) {
super(baseSleepTimeMs, max);
this.maxSleepTimeMs = maxSleepTimeMs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public class ExponentialBackoff extends SleepingRetryPolicy {
private final int MAX_SHIFT = 30;

private final Random random = new Random();
private final int baseSleepTimeMs;
private final long baseSleepTimeMs;

public ExponentialBackoff(int baseSleepTimeMs, int maxAttempts) {
public ExponentialBackoff(long baseSleepTimeMs, int maxAttempts) {
super(maxAttempts);
this.baseSleepTimeMs = baseSleepTimeMs;
}
Expand All @@ -46,7 +46,7 @@ public long getSleepTimeMs() {
return baseSleepTimeMs * Math.max(1, random.nextInt(1 << attempt));
}

public int getBaseSleepTimeMs() {
public long getBaseSleepTimeMs() {
return baseSleepTimeMs;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.netflix.astyanax.retry;

import static com.netflix.astyanax.retry.ExponentialBackoffTest.setAttemptCount;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

public final class BoundedExponentialBackoffTest {

@Test
public void testSleepTimeNeverNegative() throws NoSuchFieldException, IllegalAccessException {
BoundedExponentialBackoff backoff = new BoundedExponentialBackoff(500, 5000, -1);

for(int i = 0; i < 1000; i++) {
setAttemptCount(backoff, i);
assertTrue("Backoff at retry " + i + " was not positive", backoff.getSleepTimeMs() >= 0);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.netflix.astyanax.retry;

import java.lang.reflect.Field;

import static org.junit.Assert.assertTrue;
import org.junit.Test;

public final class ExponentialBackoffTest {


@Test
public void testSleepTimeNeverNegative() throws NoSuchFieldException, IllegalAccessException {
ExponentialBackoff backoff = new ExponentialBackoff(500, -1);

for(int i = 22; i < 1000; i++) {
setAttemptCount(backoff, i);
assertTrue("Backoff at retry " + i + " was not positive", backoff.getSleepTimeMs() >= 0);
}
}

public static void setAttemptCount(SleepingRetryPolicy backoff, int attempt)
throws NoSuchFieldException, IllegalAccessException {
Field attempts = SleepingRetryPolicy.class.getDeclaredField("attempts");
attempts.setAccessible(true);
attempts.setInt(backoff, attempt);
}
}

0 comments on commit 76c6572

Please sign in to comment.