}
Map<Long, Network> timesForNextConnects = new TreeMap<>(missingNetworks.stream()
- .collect(Collectors.toMap(connectionBackoff::getBackoff, Function.identity(), (network, ignore) -> network)));
+ .collect(Collectors.toMap(connectionBackoff::getConnectionTime, Function.identity(), (network, ignore) -> network)));
Optional<Entry<Long, Network>> firstNetwork = Optional.fromNullable(timesForNextConnects.entrySet().stream().findFirst().orElse(null));
if (!firstNetwork.isPresent()) {
continue;
}
- if (firstNetwork.get().getKey() > 0) {
- eventBus.post(new GenericMessage(String.format("Waiting %d minutes to connect to %s...", TimeUnit.MILLISECONDS.toMinutes(firstNetwork.get().getKey()), firstNetwork.get().getValue().name())));
+ if (firstNetwork.get().getKey() > System.currentTimeMillis()) {
+ eventBus.post(new GenericMessage(String.format("Waiting %d minutes to connect to %s...", TimeUnit.MILLISECONDS.toMinutes(firstNetwork.get().getKey() - System.currentTimeMillis()), firstNetwork.get().getValue().name())));
synchronized (syncObject) {
try {
- syncObject.wait(firstNetwork.get().getKey());
+ syncObject.wait(firstNetwork.get().getKey() - System.currentTimeMillis());
} catch (InterruptedException ie1) {
/* ignore. */
}
if (!isRunning()) {
break;
}
+ if (firstNetwork.get().getKey() > System.currentTimeMillis()) {
+ continue;
+ }
}
connectNetwork(firstNetwork.get().getValue());
public class ConnectionBackoffTest {
private final Network network = Mockito.mock(Network.class);
- private final Clock fixedClock = Clock.fixed(Instant.now(), ZoneId.systemDefault());
+ private final Instant now = Instant.now();
+ private final Clock fixedClock = Clock.fixed(now, ZoneId.systemDefault());
private final ConnectionBackoff connectionBackoff = new ConnectionBackoff(fixedClock);
@Test
@Test
public void firstConnectionIsImmediatelyPossible() {
MatcherAssert.assertThat(connectionBackoff.getBackoff(network), Matchers.is(0L));
+ MatcherAssert.assertThat(connectionBackoff.getConnectionTime(network), Matchers.is(now.toEpochMilli()));
}
@Test
public void afterTheFirstFailedConnectionBackoffIsOneMinute() {
connectionBackoff.connectionFailed(network);
MatcherAssert.assertThat(connectionBackoff.getBackoff(network), Matchers.is(60000L));
+ MatcherAssert.assertThat(connectionBackoff.getConnectionTime(network), Matchers.is(now.toEpochMilli() + 60000));
}
@Test
connectionBackoff.connectionFailed(network);
connectionBackoff.connectionFailed(network);
MatcherAssert.assertThat(connectionBackoff.getBackoff(network), Matchers.is(72000L));
+ MatcherAssert.assertThat(connectionBackoff.getConnectionTime(network), Matchers.is(now.toEpochMilli() + 72000));
}
@Test
connectionBackoff.connectionFailed(network);
connectionBackoff.connectionSuccessful(network);
MatcherAssert.assertThat(connectionBackoff.getBackoff(network), Matchers.is(0L));
+ MatcherAssert.assertThat(connectionBackoff.getConnectionTime(network), Matchers.is(now.toEpochMilli()));
}
@Test
connectionBackoff.connectionFailed(network);
}
MatcherAssert.assertThat(connectionBackoff.getBackoff(network), Matchers.is(3600000L));
+ MatcherAssert.assertThat(connectionBackoff.getConnectionTime(network), Matchers.is(now.toEpochMilli() + 3600000));
}
}