|
8 | 8 | import java.net.Proxy; |
9 | 9 | import java.net.URI; |
10 | 10 | import java.net.URISyntaxException; |
| 11 | +import java.util.concurrent.ScheduledExecutorService; |
11 | 12 | import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 13 | +import java.util.concurrent.TimeUnit; |
12 | 14 |
|
13 | 15 | import javax.net.ssl.SSLException; |
14 | 16 |
|
@@ -49,6 +51,8 @@ public class WebSocketConnectionTest { |
49 | 51 | private ConnectionEventListener mockEventListener; |
50 | 52 | @Mock |
51 | 53 | private Factory factory; |
| 54 | + @Mock |
| 55 | + private ScheduledExecutorService scheduledExecutorService; |
52 | 56 |
|
53 | 57 | private WebSocketConnection connection; |
54 | 58 |
|
@@ -360,26 +364,47 @@ public void stateIsReconnectingAfterTryingToConnectForTheFirstTime() throws Inte |
360 | 364 | assertEquals(ConnectionState.RECONNECTING, connection.getState()); |
361 | 365 | } |
362 | 366 |
|
363 | | -// TODO: leaving the following tests commented out just for reference. The lib needs to be rearchitected before we can hope to get any of these in |
364 | | -// @Test |
365 | | -// public void reconnectingLogicActuallyBeingCalled(){ |
366 | | -// fail("not implemented"); |
367 | | -// } |
368 | | -// |
369 | | -// @Test |
370 | | -// public void retryMaximumNumberOfTimes(){ |
371 | | -// fail("not implemented"); |
372 | | -// } |
373 | | -// |
374 | | -// @Test |
375 | | -// public void disconnectAfterTooManyRetries(){ |
376 | | -// fail("not implemented"); |
377 | | -// } |
378 | | -// |
379 | | -// @Test |
380 | | -// public void retryWithTimeout(){ |
381 | | -// fail("not implemented"); |
382 | | -// } |
| 367 | + @Test |
| 368 | + @SuppressWarnings("rawtypes") |
| 369 | + public void testStopsReconnectingAfterMaxReconnectionAttemptsIsReached() throws URISyntaxException, InterruptedException, SSLException { |
| 370 | + when(factory.getTimers()).thenReturn(scheduledExecutorService); |
| 371 | + // Run the reconnect functionality synchronously |
| 372 | + doAnswer(new Answer() { |
| 373 | + @Override |
| 374 | + public Object answer(InvocationOnMock invocation) throws Throwable { |
| 375 | + final Runnable r = (Runnable) invocation.getArguments()[0]; |
| 376 | + r.run(); |
| 377 | + return null; |
| 378 | + } |
| 379 | + }).when(scheduledExecutorService).schedule(any(Runnable.class), any(Long.class), any(TimeUnit.class)); |
| 380 | + |
| 381 | + // Reconnect a single time (maxReconnectionAttempts = 1) |
| 382 | + connection = new WebSocketConnection(URL, ACTIVITY_TIMEOUT, PONG_TIMEOUT, 1, MAX_GAP, PROXY, factory); |
| 383 | + |
| 384 | + connection.connect(); |
| 385 | + |
| 386 | + // After the first close, we expect a reconnect |
| 387 | + connection.onClose(500, "reason", true); |
| 388 | + assertEquals(ConnectionState.CONNECTING, connection.getState()); |
| 389 | + |
| 390 | + // It should give up on the second attempt |
| 391 | + connection.onClose(500, "reason", true); |
| 392 | + assertEquals(ConnectionState.DISCONNECTED, connection.getState()); |
| 393 | + |
| 394 | + // Test that behavior is the same after `connect()` is called |
| 395 | + // This guards against a bug fixed in https://github.com/pusher/pusher-websocket-java/pull/201 |
| 396 | + // where the number of retries was not reset after calling `connect()`. |
| 397 | + |
| 398 | + connection.connect(); |
| 399 | + |
| 400 | + // After the first close, we expect a reconnect |
| 401 | + connection.onClose(500, "reason", true); |
| 402 | + assertEquals(ConnectionState.CONNECTING, connection.getState()); |
| 403 | + |
| 404 | + // It should give up on the second attempt |
| 405 | + connection.onClose(500, "reason", true); |
| 406 | + assertEquals(ConnectionState.DISCONNECTED, connection.getState()); |
| 407 | + } |
383 | 408 |
|
384 | 409 | /* end of tests */ |
385 | 410 |
|
|
0 commit comments