Skip to content

Commit

Permalink
Refactor ExponentialBackoffManager Tests to Remove Thread.sleep().
Browse files Browse the repository at this point in the history
This commit enhances the ExponentialBackoffManager unit tests by replacing the use of Thread.sleep() with direct manipulation of internal state to simulate the cooldown period. This change improves test reliability and ensures consistent behavior in resource-constrained environments.
  • Loading branch information
arturobernalg committed Oct 20, 2023
1 parent 777b1a4 commit fdddb8a
Showing 1 changed file with 47 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.time.Instant;
import java.util.Map;

public class TestExponentialBackoffManager {

private ExponentialBackoffManager impl;
Expand All @@ -62,14 +65,18 @@ public void exponentialBackoffApplied() {
}

@Test
public void exponentialGrowthRateIsConfigurable() throws InterruptedException {
public void exponentialGrowthRateIsConfigurable() {
final int customCoolDownMs = 500;
connPerRoute.setMaxPerRoute(route, 4);
impl.setBackoffFactor(0.5);
impl.setCoolDown(TimeValue.ofMilliseconds(customCoolDownMs));
impl.backOff(route);
assertEquals(2, connPerRoute.getMaxPerRoute(route));
Thread.sleep(customCoolDownMs + 100); // Sleep for a slightly longer than the custom cooldown period

// Manipulate lastRouteBackoffs to simulate that enough time has passed for the cooldown period
final Map<HttpRoute, Instant> lastRouteBackoffs = impl.getLastRouteBackoffs();
lastRouteBackoffs.put(route, Instant.now().minusMillis(customCoolDownMs + 1));

impl.backOff(route);
assertEquals(1, connPerRoute.getMaxPerRoute(route));
}
Expand All @@ -84,72 +91,92 @@ public void doesNotIncreaseBeyondPerHostMaxOnProbe() {
}

@Test
public void backoffDoesNotAdjustDuringCoolDownPeriod() throws InterruptedException {
public void backoffDoesNotAdjustDuringCoolDownPeriod() {
connPerRoute.setMaxPerRoute(route, 4);
impl.backOff(route);
final long max = connPerRoute.getMaxPerRoute(route);
Thread.sleep(1); // Sleep for 1 ms

// Manipulate lastRouteBackoffs to simulate that not enough time has passed for the cooldown period
final Map<HttpRoute, Instant> lastRouteBackoffs = impl.getLastRouteBackoffs();
lastRouteBackoffs.put(route, Instant.now().minusMillis(0));

impl.backOff(route);
assertEquals(max, connPerRoute.getMaxPerRoute(route));
}

@Test
public void backoffStillAdjustsAfterCoolDownPeriod() throws InterruptedException {
public void backoffStillAdjustsAfterCoolDownPeriod() {
connPerRoute.setMaxPerRoute(route, 8);
impl.backOff(route);
final long max = connPerRoute.getMaxPerRoute(route);
Thread.sleep(DEFAULT_COOL_DOWN_MS + 1); // Sleep for cooldown period + 1 ms

// Manipulate lastRouteBackoffs to simulate that enough time has passed for the cooldown period
final Map<HttpRoute, Instant> lastRouteBackoffs = impl.getLastRouteBackoffs();
lastRouteBackoffs.put(route, Instant.now().minusMillis(DEFAULT_COOL_DOWN_MS + 1));

// Perform another backoff
impl.backOff(route);

// Assert that the max connections have decreased
assertTrue(max == 1 || max > connPerRoute.getMaxPerRoute(route));
}


@Test
public void probeDoesNotAdjustDuringCooldownPeriod() throws InterruptedException {
public void probeDoesNotAdjustDuringCooldownPeriod() {
connPerRoute.setMaxPerRoute(route, 4);
impl.probe(route);
final long max = connPerRoute.getMaxPerRoute(route);
Thread.sleep(1); // Sleep for 1 ms

// Manipulate lastRouteProbes to simulate that not enough time has passed for the cooldown period
final Map<HttpRoute, Instant> lastRouteProbes = impl.getLastRouteProbes();
lastRouteProbes.put(route, Instant.now().minusMillis(0));

impl.probe(route);
assertEquals(max, connPerRoute.getMaxPerRoute(route));
}

@Test
public void probeStillAdjustsAfterCoolDownPeriod() throws InterruptedException {
public void probeStillAdjustsAfterCoolDownPeriod() {
connPerRoute.setMaxPerRoute(route, 8);
impl.probe(route);
final long max = connPerRoute.getMaxPerRoute(route);
Thread.sleep(DEFAULT_COOL_DOWN_MS + 1); // Sleep for cooldown period + 1 ms

// Manipulate lastRouteProbes to simulate that enough time has passed for the cooldown period
final Map<HttpRoute, Instant> lastRouteProbes = impl.getLastRouteProbes();
lastRouteProbes.put(route, Instant.now().minusMillis(DEFAULT_COOL_DOWN_MS + 1));

impl.probe(route);
assertTrue(max < connPerRoute.getMaxPerRoute(route));
}

@Test
public void willBackoffImmediatelyEvenAfterAProbe() {
connPerRoute.setMaxPerRoute(route, 8);
final long now = System.currentTimeMillis();
impl.probe(route);
final long max = connPerRoute.getMaxPerRoute(route);
impl.backOff(route);
assertTrue(connPerRoute.getMaxPerRoute(route) < max);
}

@Test
public void coolDownPeriodIsConfigurable() throws InterruptedException {
public void coolDownPeriodIsConfigurable() {
final long cd = 500; // Fixed cooldown period of 500 milliseconds
impl.setCoolDown(TimeValue.ofMilliseconds(cd));

// Sleep for a short duration before starting the test to reduce potential timing issues
Thread.sleep(100);

// Probe and check if the connection count remains the same during the cooldown period
connPerRoute.setMaxPerRoute(route, 4);
impl.probe(route);
final int max0 = connPerRoute.getMaxPerRoute(route);
Thread.sleep(cd / 2); // Sleep for half the cooldown period

// Manipulate lastRouteProbes to simulate that not enough time has passed for the cooldown period
final Map<HttpRoute, Instant> lastRouteProbes = impl.getLastRouteProbes();
lastRouteProbes.put(route, Instant.now().minusMillis(cd / 2));

impl.probe(route);
assertEquals(max0, connPerRoute.getMaxPerRoute(route));

// Probe and check if the connection count increases after the cooldown period
Thread.sleep(cd / 2 + 1); // Sleep for the remaining half of the cooldown period + 1 ms
// Manipulate lastRouteProbes again to simulate that enough time has passed for the cooldown period
lastRouteProbes.put(route, Instant.now().minusMillis(cd + 1));

impl.probe(route);
assertTrue(max0 < connPerRoute.getMaxPerRoute(route));
}
Expand Down

0 comments on commit fdddb8a

Please sign in to comment.