@Override public void run() { sendKeepAlive(); }
/** * Checks whether an attempt to reconnect should be executed. This method also handles the * counter variables to keep track of the state. * * @return If we should execute reconnect. */ private boolean shouldReconnect() { noConnectionCount++; boolean shouldReconnect = noConnectionCount >= nextReconnectAt; if (shouldReconnect) { noConnectionCount = 0; increaseNextReconnectAt(); } return shouldReconnect; }
connection.sendKeepAlive(platformManager.getPlatformId()); } else { if (shouldReconnect()) { log.info("Trying to reconnect to the server."); try {
@Test public void reconnectExponential() throws Exception { when(connection.isConnected()).thenReturn(false); doThrow(ConnectException.class).when(connection).reconnect(); manager.sendKeepAlive(); manager.sendKeepAlive(); verify(connection, times(1)).reconnect(); manager.sendKeepAlive(); manager.sendKeepAlive(); manager.sendKeepAlive(); manager.sendKeepAlive(); verify(connection, times(2)).reconnect(); manager.sendKeepAlive(); manager.sendKeepAlive(); manager.sendKeepAlive(); manager.sendKeepAlive(); manager.sendKeepAlive(); manager.sendKeepAlive(); manager.sendKeepAlive(); manager.sendKeepAlive(); verify(connection, times(3)).reconnect(); verify(connection, atLeast(1)).isConnected(); verifyNoMoreInteractions(connection); }
@Test public void connectedThenDisconnected() throws Exception { when(connection.isConnected()).thenReturn(true).thenReturn(false); when(platformManager.getPlatformId()).thenReturn(PLATFORM_ID); manager.sendKeepAlive(); manager.sendKeepAlive(); verify(connection, times(2)).isConnected(); verify(connection).sendKeepAlive(PLATFORM_ID); // no reconnect at first disconnect verifyNoMoreInteractions(connection); }
@Test public void reconnect() throws Exception { when(connection.isConnected()).thenReturn(false); doThrow(ConnectException.class).when(connection).reconnect(); manager.sendKeepAlive(); manager.sendKeepAlive(); verify(connection, times(2)).isConnected(); verify(connection).reconnect(); verifyNoMoreInteractions(connection); }
@Test public void connected() throws Exception { when(connection.isConnected()).thenReturn(true); when(platformManager.getPlatformId()).thenReturn(PLATFORM_ID); manager.sendKeepAlive(); verify(connection).isConnected(); verify(connection).sendKeepAlive(PLATFORM_ID); verifyNoMoreInteractions(connection); }
@Test public void reconnectKeepAliveReconnect() throws Exception { when(connection.isConnected()).thenReturn(false); when(platformManager.getPlatformId()).thenReturn(PLATFORM_ID); doThrow(ConnectException.class).when(connection).reconnect(); // first make sure we fire one reconnect manager.sendKeepAlive(); manager.sendKeepAlive(); verify(connection, times(1)).reconnect(); // then switch to connected when(connection.isConnected()).thenReturn(true); manager.sendKeepAlive(); verify(connection, times(1)).reconnect(); verify(connection, times(1)).sendKeepAlive(PLATFORM_ID); // then back to reconnect, but not called on first call when(connection.isConnected()).thenReturn(false); manager.sendKeepAlive(); verify(connection, times(1)).reconnect(); verify(connection, atLeast(1)).isConnected(); verifyNoMoreInteractions(connection); }