fix invalid range

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
This commit is contained in:
Abhishek Kumar 2024-10-08 17:36:55 +05:30
parent 864afda935
commit 3e830252ab
2 changed files with 23 additions and 5 deletions

View File

@ -44,12 +44,16 @@ public class RangeTimeBackoff extends AdapterBase implements BackoffAlgorithm {
@Override
public void waitBeforeRetry() {
long time = minTime * 1000L;
long time = Math.max(minTime, 0) * 1000L;
Thread current = Thread.currentThread();
try {
asleep.put(current.getName(), current);
time = ThreadLocalRandom.current().nextInt(minTime, maxTime) * 1000L;
LOG.debug("Waiting " + current.getName() + " for " + time);
if (maxTime > minTime) {
time = ThreadLocalRandom.current().nextInt(minTime, maxTime) * 1000L;
}
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Waiting %s for %d milliseconds", current.getName(), time));
}
Thread.sleep(time);
} catch (InterruptedException e) {
// JMX or other threads may interrupt this thread, but let's log it

View File

@ -31,8 +31,8 @@ public class RangeTimeBackoffTest {
public void testWaitValidValue() {
RangeTimeBackoff backoff = new RangeTimeBackoff();
Map<String, Object> map = new HashMap<>();
int min = 5;
int max = 10;
int min = 1;
int max = 3;
map.put("minSeconds", String.valueOf(min));
map.put("maxSeconds", String.valueOf(max));
backoff.configure("RangeTimeBackoff", map);
@ -67,4 +67,18 @@ public class RangeTimeBackoffTest {
Assert.assertTrue(timeTaken >= RangeTimeBackoff.DEFAULT_MIN_TIME * 1000L);
}
@Test
public void testWaitVMinHigherThanMax() {
RangeTimeBackoff backoff = new RangeTimeBackoff();
Map<String, Object> map = new HashMap<>();
int min = 3;
int max = 2;
map.put("minSeconds", String.valueOf(min));
map.put("maxSeconds", String.valueOf(max));
backoff.configure("RangeTimeBackoff", map);
long startTime = System.currentTimeMillis();
backoff.waitBeforeRetry();
long timeTaken = System.currentTimeMillis() - startTime;
Assert.assertTrue(timeTaken >= min * 1000L);
}
}