@Test public void webSocketClosesAfterTimeElapsesFromStart() { watchdog.start(webSocket, 100); ShadowLooper.idleMainLooper(101, TimeUnit.MILLISECONDS); verify(webSocket).close(eq(1000), anyString()); }
synchronized void start(final WebSocket webSocket, long connectionTimeoutMs) { if (webSocket == null) { throw new NullPointerException("Passed null webSocket to watchdog."); } else if (connectionTimeoutMs <= 0) { throw new IllegalArgumentException("connectionTimeoutMs must be > 0."); } stop(); this.connectionTimeoutMs = connectionTimeoutMs; cleanupRunnable = new Runnable() { @Override public void run() { Log.d(TAG, "WebSocket closed gracefully due to timeout."); webSocket.close(NORMAL_CLOSURE_STATUS, "WebSocket closed due to timeout."); } }; handler.postDelayed(cleanupRunnable, connectionTimeoutMs); }
TimeoutWatchdog { synchronized void start(final WebSocket webSocket, long connectionTimeoutMs) { if (webSocket == null) { throw new NullPointerException("Passed null webSocket to watchdog."); } else if (connectionTimeoutMs <= 0) { throw new IllegalArgumentException("connectionTimeoutMs must be > 0."); } stop(); this.connectionTimeoutMs = connectionTimeoutMs; cleanupRunnable = new Runnable() { @Override public void run() { Log.d(TAG, "WebSocket closed gracefully due to timeout."); webSocket.close(NORMAL_CLOSURE_STATUS, "WebSocket closed due to timeout."); } }; handler.postDelayed(cleanupRunnable, connectionTimeoutMs); } }
TimeoutWatchdog { synchronized void start(final WebSocket webSocket, long connectionTimeoutMs) { if (webSocket == null) { throw new NullPointerException("Passed null webSocket to watchdog."); } else if (connectionTimeoutMs <= 0) { throw new IllegalArgumentException("connectionTimeoutMs must be > 0."); } stop(); this.connectionTimeoutMs = connectionTimeoutMs; cleanupRunnable = new Runnable() { @Override public void run() { Log.d(TAG, "WebSocket closed gracefully due to timeout."); webSocket.close(NORMAL_CLOSURE_STATUS, "WebSocket closed due to timeout."); } }; handler.postDelayed(cleanupRunnable, connectionTimeoutMs); } TimeoutWatchdog(); }
TimeoutWatchdog { synchronized void start(final WebSocket webSocket, long connectionTimeoutMs) { if (webSocket == null) { throw new NullPointerException("Passed null webSocket to watchdog."); } else if (connectionTimeoutMs <= 0) { throw new IllegalArgumentException("connectionTimeoutMs must be > 0."); } stop(); this.connectionTimeoutMs = connectionTimeoutMs; cleanupRunnable = new Runnable() { @Override public void run() { Log.d(TAG, "WebSocket closed gracefully due to timeout."); webSocket.close(NORMAL_CLOSURE_STATUS, "WebSocket closed due to timeout."); } }; handler.postDelayed(cleanupRunnable, connectionTimeoutMs); } TimeoutWatchdog();  }
TimeoutWatchdog { synchronized void start(final WebSocket webSocket, long connectionTimeoutMs) { if (webSocket == null) { throw new NullPointerException("Passed null webSocket to watchdog."); } else if (connectionTimeoutMs <= 0) { throw new IllegalArgumentException("connectionTimeoutMs must be > 0."); } stop(); this.connectionTimeoutMs = connectionTimeoutMs; cleanupRunnable = new Runnable() { @Override public void run() { Log.d(TAG, "WebSocket closed gracefully due to timeout."); webSocket.close(NORMAL_CLOSURE_STATUS, "WebSocket closed due to timeout."); } }; handler.postDelayed(cleanupRunnable, connectionTimeoutMs); } TimeoutWatchdog();   }