Skip to content

Commit 06d63e4

Browse files
committed
Merge branch 'master' into v2-release
2 parents 01977c3 + a025630 commit 06d63e4

File tree

8 files changed

+55
-29
lines changed

8 files changed

+55
-29
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ repositories {
4343

4444
dependencies {
4545
compile "com.google.code.gson:gson:2.2.2"
46-
compile "com.pusher:java-websocket:1.4.1"
46+
compile "org.java-websocket:Java-WebSocket:1.4.0"
4747
testCompile "org.mockito:mockito-all:1.8.5"
4848
testCompile "org.powermock:powermock-module-junit4:1.4.11"
4949
testCompile "org.powermock:powermock-api-mockito:1.4.11"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import javax.net.ssl.SSLException;
1111
import javax.net.ssl.SSLSocketFactory;
1212

13-
import com.pusher.java_websocket.client.WebSocketClient;
14-
import com.pusher.java_websocket.handshake.ServerHandshake;
13+
import org.java_websocket.client.WebSocketClient;
14+
import org.java_websocket.handshake.ServerHandshake;
1515

1616
/**
1717
* A thin wrapper around the WebSocketClient class from the Java-WebSocket

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import javax.net.ssl.SSLException;
1616

17-
import com.pusher.java_websocket.handshake.ServerHandshake;
17+
import org.java_websocket.handshake.ServerHandshake;
1818

1919
import com.google.gson.Gson;
2020

@@ -323,6 +323,7 @@ public void run() {
323323
factory.shutdownThreads();
324324
}
325325
});
326+
reconnectAttempts = 0;
326327
}
327328

328329
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.pusher.client.connection.websocket;
22

3-
import com.pusher.java_websocket.handshake.ServerHandshake;
3+
import org.java_websocket.handshake.ServerHandshake;
44

55
public interface WebSocketListener {
66

src/main/java/com/pusher/client/example/SimpleWebSocket.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import java.net.URI;
44
import java.net.URISyntaxException;
55

6-
import com.pusher.java_websocket.client.WebSocketClient;
7-
import com.pusher.java_websocket.handshake.ServerHandshake;
6+
import org.java_websocket.client.WebSocketClient;
7+
import org.java_websocket.handshake.ServerHandshake;
88

99
public class SimpleWebSocket extends WebSocketClient {
1010
public static void main(final String[] args) throws URISyntaxException {

src/test/java/com/pusher/client/EndToEndTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import com.pusher.client.connection.websocket.WebSocketListener;
3232
import com.pusher.client.util.DoNothingExecutor;
3333
import com.pusher.client.util.Factory;
34-
import com.pusher.java_websocket.handshake.ServerHandshake;
34+
import org.java_websocket.handshake.ServerHandshake;
3535

3636
@RunWith(MockitoJUnitRunner.class)
3737
public class EndToEndTest {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import javax.net.ssl.SSLException;
1010

11-
import com.pusher.java_websocket.handshake.ServerHandshake;
11+
import org.java_websocket.handshake.ServerHandshake;
1212
import org.junit.Before;
1313
import org.junit.Test;
1414
import org.junit.runner.RunWith;

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)