Skip to content

Commit

Permalink
Change conditions for connect and disconnect calls (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonologico authored Jun 30, 2022
1 parent 8c738ef commit 032325c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/pusher/client/Pusher.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public Connection getConnection() {
* {@link Connection#bind(ConnectionState, ConnectionEventListener)} method
* will receive connection events.
*
* <p>Calls are ignored (a connection is not attempted) if the {@link Connection#getState()} is not {@link com.pusher.client.connection.ConnectionState#DISCONNECTED}.</p>
* <p>Calls are ignored (a connection is not attempted) if the {@link Connection#getState()} is not {@link com.pusher.client.connection.ConnectionState#DISCONNECTED} or {@link com.pusher.client.connection.ConnectionState#DISCONNECTING}.</p>
*/
public void connect() {
connect(null);
Expand Down Expand Up @@ -191,12 +191,12 @@ public void connect(final ConnectionEventListener eventListener, ConnectionState
* Disconnect from Pusher.
*
* <p>
* Calls are ignored if the {@link Connection#getState()}, retrieved from {@link Pusher#getConnection}, is not
* {@link com.pusher.client.connection.ConnectionState#CONNECTED}.
* Calls are ignored if the {@link Connection#getState()}, retrieved from {@link Pusher#getConnection}, is
* {@link com.pusher.client.connection.ConnectionState#DISCONNECTING} or {@link com.pusher.client.connection.ConnectionState#DISCONNECTED}.
* </p>
*/
public void disconnect() {
if (connection.getState() == ConnectionState.CONNECTED) {
if (connection.getState() != ConnectionState.DISCONNECTING && connection.getState() != ConnectionState.DISCONNECTED) {
connection.disconnect();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ public void connect() {

@Override
public void run() {
if (state == ConnectionState.DISCONNECTED) {
if (canConnect()) {
tryConnecting();
}
}
});
}

private void tryConnecting(){
private void tryConnecting() {
try {
underlyingConnection = factory
.newWebSocketClientWrapper(webSocketUri, proxy, WebSocketConnection.this);
Expand All @@ -99,7 +99,7 @@ public void disconnect() {
factory.queueOnEventThread(new Runnable() {
@Override
public void run() {
if (state == ConnectionState.CONNECTED) {
if (canDisconnect()) {
updateState(ConnectionState.DISCONNECTING);
underlyingConnection.close();
}
Expand Down Expand Up @@ -294,8 +294,10 @@ private void tryReconnecting() {
factory.getTimers().schedule(new Runnable() {
@Override
public void run() {
underlyingConnection.removeWebSocketListener();
tryConnecting();
if (state == ConnectionState.RECONNECTING) {
underlyingConnection.removeWebSocketListener();
tryConnecting();
}
}
}, reconnectInterval, TimeUnit.SECONDS);
}
Expand All @@ -312,8 +314,10 @@ private void cancelTimeoutsAndTransitionToDisconnected() {
factory.queueOnEventThread(new Runnable() {
@Override
public void run() {
updateState(ConnectionState.DISCONNECTED);
factory.shutdownThreads();
if (state == ConnectionState.DISCONNECTING) {
updateState(ConnectionState.DISCONNECTED);
factory.shutdownThreads();
}
}
});
reconnectAttempts = 0;
Expand All @@ -334,6 +338,14 @@ public void run() {
});
}

private boolean canConnect() {
return state == ConnectionState.DISCONNECTING || state == ConnectionState.DISCONNECTED;
}

private boolean canDisconnect() {
return state != ConnectionState.DISCONNECTING && state != ConnectionState.DISCONNECTED;
}

private class ActivityTimer {
private final long activityTimeout;
private final long pongTimeout;
Expand Down
8 changes: 0 additions & 8 deletions src/test/java/com/pusher/client/PusherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,6 @@ public void testDisconnectCallDoesNothingIfStateIsDisconnected() {
verify(mockConnection, never()).disconnect();
}

@Test
public void testDisconnectCallDoesNothingIfStateIsConnecting() {
when(mockConnection.getState()).thenReturn(ConnectionState.CONNECTING);

pusher.disconnect();
verify(mockConnection, never()).disconnect();
}

@Test
public void testDisconnectCallDoesNothingIfStateIsDisconnecting() {
when(mockConnection.getState()).thenReturn(ConnectionState.DISCONNECTING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ public void testConnectUpdatesStateAndNotifiesListener() {
assertEquals(ConnectionState.CONNECTING, connection.getState());
}

@Test
public void testConnectAfterDisconnect() {
connection.connect();
connection.disconnect();
assertEquals(ConnectionState.DISCONNECTING, connection.getState());
connection.connect();
assertEquals(ConnectionState.CONNECTING, connection.getState());
verify(mockUnderlyingConnection, times(2)).connect();
}

@Test
public void testConnectDoesNotCallConnectOnUnderlyingConnectionIfAlreadyInConnectingState() {
connection.connect();
Expand Down Expand Up @@ -274,15 +284,6 @@ public void testDisconnectInDisconnectedStateIsIgnored() {
verify(mockEventListener, times(0)).onConnectionStateChange(any(ConnectionStateChange.class));
}

@Test
public void testDisconnectInConnectingStateIsIgnored() {
connection.connect();

connection.disconnect();

verify(mockUnderlyingConnection, times(0)).close();
verify(mockEventListener, times(1)).onConnectionStateChange(any(ConnectionStateChange.class));
}

@Test
public void testDisconnectInDisconnectingStateIsIgnored() {
Expand Down

0 comments on commit 032325c

Please sign in to comment.