/*
* Test the ExponentialReconnectionPolicy.
*/
@Test(groups = "long")
public void exponentialReconnectionPolicyTest() throws Throwable {
Cluster.Builder builder = Cluster.builder().withReconnectionPolicy(new ExponentialReconnectionPolicy(2 * 1000, 5 * 60 * 1000));
// Ensure that ExponentialReconnectionPolicy is what we should be testing
if (!(builder.getConfiguration().getPolicies().getReconnectionPolicy() instanceof ExponentialReconnectionPolicy)) {
fail("Set policy does not match retrieved policy.");
}
// Test basic getters
ExponentialReconnectionPolicy reconnectionPolicy = (ExponentialReconnectionPolicy) builder.getConfiguration().getPolicies().getReconnectionPolicy();
assertTrue(reconnectionPolicy.getBaseDelayMs() == 2 * 1000);
assertTrue(reconnectionPolicy.getMaxDelayMs() == 5 * 60 * 1000);
// Test erroneous instantiations
try {
new ExponentialReconnectionPolicy(-1, 1);
fail();
} catch (IllegalArgumentException e) {}
try {
new ExponentialReconnectionPolicy(1, -1);
fail();
} catch (IllegalArgumentException e) {}
try {
new ExponentialReconnectionPolicy(-1, -1);
fail();
} catch (IllegalArgumentException e) {}
try {
new ExponentialReconnectionPolicy(2, 1);
fail();
} catch (IllegalArgumentException e) {}
// Test nextDelays()
ReconnectionPolicy.ReconnectionSchedule schedule = new ExponentialReconnectionPolicy(2 * 1000, 5 * 60 * 1000).newSchedule();
assertTrue(schedule.nextDelayMs() == 2000);
assertTrue(schedule.nextDelayMs() == 4000);
assertTrue(schedule.nextDelayMs() == 8000);
assertTrue(schedule.nextDelayMs() == 16000);
assertTrue(schedule.nextDelayMs() == 32000);
for (int i = 0; i < 64; ++i)
schedule.nextDelayMs();
assertTrue(schedule.nextDelayMs() == reconnectionPolicy.getMaxDelayMs());
// Run integration test
long restartTime = 2 + 4 + 8 + 2; // 16: 3 full cycles + 2 seconds
long retryTime = 30; // 4th cycle start time
long breakTime = 62; // time until next reconnection attempt