VpnWatchdog { void handleTimeout() throws AdVpnThread.VpnNetworkException { if (!enabled) return; Log.d(TAG, "handleTimeout: Milliseconds elapsed between last receive and sent: " + (lastPacketReceived - lastPacketSent)); if (lastPacketReceived < lastPacketSent && lastPacketSent != 0) { initPenalty += INIT_PENALTY_INC; if (initPenalty > INIT_PENALTY_END) initPenalty = INIT_PENALTY_END; throw new AdVpnThread.VpnNetworkException("Watchdog timed out"); } pollTimeout *= POLL_TIMEOUT_GROW; if (pollTimeout > POLL_TIMEOUT_END) pollTimeout = POLL_TIMEOUT_END; sendPacket(); }    }
@Test public void testHandleTimeout() throws Exception { doNothing().when(watchdog, "sendPacket"); assertEquals(1000, watchdog.getPollTimeout()); watchdog.handleTimeout(); assertEquals(4 * 1000, watchdog.getPollTimeout()); watchdog.handleTimeout(); assertEquals(4 * 4 * 1000, watchdog.getPollTimeout()); watchdog.handleTimeout(); watchdog.pollTimeout = 4 * 4 * 4 * 4 * 4 * 4 * 1000; watchdog.handleTimeout(); assertEquals(4 * 4 * 4 * 4 * 4 * 4 * 1000, watchdog.getPollTimeout()); }
@Test(expected = AdVpnThread.VpnNetworkException.class) public void testHandleTimeout_error() throws Exception { watchdog.lastPacketReceived = 1; watchdog.lastPacketSent = 2; watchdog.handleTimeout(); }
@Test public void testHandleTimeout_errorInitPenalty() throws Exception { watchdog.lastPacketReceived = 1; watchdog.lastPacketSent = 2; assertEquals(0, watchdog.initPenalty); try { watchdog.handleTimeout(); } catch (AdVpnThread.VpnNetworkException e) { } assertEquals(200, watchdog.initPenalty); for (int i = 1; i < 25; i++) { assertEquals(200 * i, watchdog.initPenalty); try { watchdog.handleTimeout(); } catch (AdVpnThread.VpnNetworkException e) { } } assertEquals(5000, watchdog.initPenalty); try { watchdog.handleTimeout(); } catch (AdVpnThread.VpnNetworkException e) { } assertEquals(5000, watchdog.initPenalty); }