From 4a21b47f426e799397775dc10ce9e65fb86c9c6d Mon Sep 17 00:00:00 2001 From: kn100 Date: Wed, 12 Jun 2019 15:22:49 +0100 Subject: [PATCH 1/2] reconnection loop should happen again whenever disconnected state is reached --- .../pusher/client/connection/websocket/WebSocketConnection.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/pusher/client/connection/websocket/WebSocketConnection.java b/src/main/java/com/pusher/client/connection/websocket/WebSocketConnection.java index 15d41315..06a278a2 100644 --- a/src/main/java/com/pusher/client/connection/websocket/WebSocketConnection.java +++ b/src/main/java/com/pusher/client/connection/websocket/WebSocketConnection.java @@ -323,6 +323,7 @@ public void run() { factory.shutdownThreads(); } }); + reconnectAttempts = 0; } @Override From b32079ed6120ad18dcd0fe7f602227d780fabb68 Mon Sep 17 00:00:00 2001 From: Will Sewell Date: Fri, 9 Aug 2019 17:47:35 +0100 Subject: [PATCH 2/2] Test the connection reconnect behaviour --- .../websocket/WebSocketConnectionTest.java | 65 +++++++++++++------ 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/src/test/java/com/pusher/client/connection/websocket/WebSocketConnectionTest.java b/src/test/java/com/pusher/client/connection/websocket/WebSocketConnectionTest.java index a3943a2c..d307eeb5 100644 --- a/src/test/java/com/pusher/client/connection/websocket/WebSocketConnectionTest.java +++ b/src/test/java/com/pusher/client/connection/websocket/WebSocketConnectionTest.java @@ -8,7 +8,9 @@ import java.net.Proxy; import java.net.URI; import java.net.URISyntaxException; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import javax.net.ssl.SSLException; @@ -49,6 +51,8 @@ public class WebSocketConnectionTest { private ConnectionEventListener mockEventListener; @Mock private Factory factory; + @Mock + private ScheduledExecutorService scheduledExecutorService; private WebSocketConnection connection; @@ -360,26 +364,47 @@ public void stateIsReconnectingAfterTryingToConnectForTheFirstTime() throws Inte assertEquals(ConnectionState.RECONNECTING, connection.getState()); } -// 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 -// @Test -// public void reconnectingLogicActuallyBeingCalled(){ -// fail("not implemented"); -// } -// -// @Test -// public void retryMaximumNumberOfTimes(){ -// fail("not implemented"); -// } -// -// @Test -// public void disconnectAfterTooManyRetries(){ -// fail("not implemented"); -// } -// -// @Test -// public void retryWithTimeout(){ -// fail("not implemented"); -// } + @Test + @SuppressWarnings("rawtypes") + public void testStopsReconnectingAfterMaxReconnectionAttemptsIsReached() throws URISyntaxException, InterruptedException, SSLException { + when(factory.getTimers()).thenReturn(scheduledExecutorService); + // Run the reconnect functionality synchronously + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + final Runnable r = (Runnable) invocation.getArguments()[0]; + r.run(); + return null; + } + }).when(scheduledExecutorService).schedule(any(Runnable.class), any(Long.class), any(TimeUnit.class)); + + // Reconnect a single time (maxReconnectionAttempts = 1) + connection = new WebSocketConnection(URL, ACTIVITY_TIMEOUT, PONG_TIMEOUT, 1, MAX_GAP, PROXY, factory); + + connection.connect(); + + // After the first close, we expect a reconnect + connection.onClose(500, "reason", true); + assertEquals(ConnectionState.CONNECTING, connection.getState()); + + // It should give up on the second attempt + connection.onClose(500, "reason", true); + assertEquals(ConnectionState.DISCONNECTED, connection.getState()); + + // Test that behavior is the same after `connect()` is called + // This guards against a bug fixed in https://github.com/pusher/pusher-websocket-java/pull/201 + // where the number of retries was not reset after calling `connect()`. + + connection.connect(); + + // After the first close, we expect a reconnect + connection.onClose(500, "reason", true); + assertEquals(ConnectionState.CONNECTING, connection.getState()); + + // It should give up on the second attempt + connection.onClose(500, "reason", true); + assertEquals(ConnectionState.DISCONNECTED, connection.getState()); + } /* end of tests */