Skip to content

Commit a025630

Browse files
authored
Merge pull request #201 from pusher/reconnect_fix
Reconnect fix
2 parents 90d4ee7 + b32079e commit a025630

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

src/main/java/com/pusher/client/connection/websocket/WebSocketConnection.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ public void run() {
323323
factory.shutdownThreads();
324324
}
325325
});
326+
reconnectAttempts = 0;
326327
}
327328

328329
@Override

src/test/java/com/pusher/client/connection/websocket/WebSocketConnectionTest.java

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import java.net.Proxy;
99
import java.net.URI;
1010
import java.net.URISyntaxException;
11+
import java.util.concurrent.ScheduledExecutorService;
1112
import java.util.concurrent.ScheduledThreadPoolExecutor;
13+
import java.util.concurrent.TimeUnit;
1214

1315
import javax.net.ssl.SSLException;
1416

@@ -49,6 +51,8 @@ public class WebSocketConnectionTest {
4951
private ConnectionEventListener mockEventListener;
5052
@Mock
5153
private Factory factory;
54+
@Mock
55+
private ScheduledExecutorService scheduledExecutorService;
5256

5357
private WebSocketConnection connection;
5458

@@ -360,26 +364,47 @@ public void stateIsReconnectingAfterTryingToConnectForTheFirstTime() throws Inte
360364
assertEquals(ConnectionState.RECONNECTING, connection.getState());
361365
}
362366

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+
}
383408

384409
/* end of tests */
385410

0 commit comments

Comments
 (0)