Yet another fix, added tests

This commit is contained in:
Inal Arsanukaev 2019-12-16 20:23:58 +03:00
parent 9a59bdd116
commit 999d5688af
2 changed files with 47 additions and 12 deletions

View File

@ -57,17 +57,16 @@ public class ExponentialBackOff implements BackOff {
private long computeNextInterval(int multiplier, Long startTime, Long currentTime) {
long diff = (currentTime - startTime) / 1000;
if (diff < 1) {
if (diff < 1 || multiplier == 1) {
return initialInterval;
}
int result = initialInterval;
long result = initialInterval;
int step = 0;
while (diff >= result) {
Long pow = (long) Math.pow(multiplier, step);
result += (initialInterval * pow);
long pow = (long) Math.pow(multiplier, step++);
result = initialInterval * pow;
}
return result;
}
}
}

View File

@ -1,6 +1,5 @@
package com.rbkmoney.adapter.common.utils.times;
import com.rbkmoney.adapter.common.model.AdapterContext;
import com.rbkmoney.adapter.common.model.PollingInfo;
import com.rbkmoney.adapter.common.state.backoff.BackOffExecution;
import org.junit.Assert;
@ -14,7 +13,7 @@ public class ExponentialBackOffPollingServiceTest {
ExponentialBackOffPollingService exponentialBackOffPollingService = new ExponentialBackOffPollingService<PollingInfo>();
@Test
public void prepareBackOffExecution() throws InterruptedException {
public void testPrepareBackOffExecution() throws InterruptedException {
PollingInfo pollingInfo = new PollingInfo();
BackOffExecution backOffExecution = exponentialBackOffPollingService.prepareBackOffExecution(pollingInfo, new HashMap<>());
Long result = backOffExecution.nextBackOff();
@ -26,23 +25,60 @@ public class ExponentialBackOffPollingServiceTest {
}
@Test
public void prepareNextPollingInterval() throws InterruptedException {
public void testPrepareNextPollingIntervalWithDefaultValues() throws InterruptedException {
PollingInfo pollingInfo = new PollingInfo();
Instant now = Instant.now();
pollingInfo.setStartDateTimePolling(now);
BackOffExecution backOffExecution = exponentialBackOffPollingService.prepareBackOffExecution(pollingInfo, new HashMap<>());
backOffExecution = backOffExecution;
Long result = backOffExecution.nextBackOff();
Assert.assertEquals(2, result.longValue());
Thread.sleep(2000L);
pollingInfo.setStartDateTimePolling(now);
Thread.sleep(result * 1000L);
int nextInterval = exponentialBackOffPollingService.prepareNextPollingInterval(pollingInfo, new HashMap<>());
Assert.assertEquals(4, nextInterval);
Thread.sleep(nextInterval * 1000L);
pollingInfo.setStartDateTimePolling(now);
nextInterval = exponentialBackOffPollingService.prepareNextPollingInterval(pollingInfo, new HashMap<>());
Assert.assertEquals(8, nextInterval);
}
@Test
public void testPrepareNextPollingIntervalWithInitialEq1() throws InterruptedException {
PollingInfo pollingInfo = new PollingInfo();
Instant now = Instant.now();
pollingInfo.setStartDateTimePolling(now);
HashMap<String, String> options = new HashMap<>();
options.put("default_initial_exponential", "1");
BackOffExecution backOffExecution = exponentialBackOffPollingService.prepareBackOffExecution(pollingInfo, options);
Long result = backOffExecution.nextBackOff();
Assert.assertEquals(1, result.longValue());
Thread.sleep(result * 1000L);
int nextInterval = exponentialBackOffPollingService.prepareNextPollingInterval(pollingInfo, options);
Assert.assertEquals(2, nextInterval);
Thread.sleep(nextInterval * 1000L);
nextInterval = exponentialBackOffPollingService.prepareNextPollingInterval(pollingInfo, options);
Assert.assertEquals(4, nextInterval);
}
@Test
public void testPrepareNextPollingIntervalWithExponentialEq1() throws InterruptedException {
PollingInfo pollingInfo = new PollingInfo();
Instant now = Instant.now();
pollingInfo.setStartDateTimePolling(now);
HashMap<String, String> options = new HashMap<>();
options.put("exponential", "1");
BackOffExecution backOffExecution = exponentialBackOffPollingService.prepareBackOffExecution(pollingInfo, options);
Long result = backOffExecution.nextBackOff();
Assert.assertEquals(2, result.longValue());
Thread.sleep(result * 1000L);
int nextInterval = exponentialBackOffPollingService.prepareNextPollingInterval(pollingInfo, options);
Assert.assertEquals(2, nextInterval);
Thread.sleep(nextInterval * 1000L);
nextInterval = exponentialBackOffPollingService.prepareNextPollingInterval(pollingInfo, options);
Assert.assertEquals(2, nextInterval);
}
}