Skip to content

Commit 032325c

Browse files
authored
Change conditions for connect and disconnect calls (#328)
1 parent 8c738ef commit 032325c

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

src/main/java/com/pusher/client/Pusher.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public Connection getConnection() {
134134
* {@link Connection#bind(ConnectionState, ConnectionEventListener)} method
135135
* will receive connection events.
136136
*
137-
* <p>Calls are ignored (a connection is not attempted) if the {@link Connection#getState()} is not {@link com.pusher.client.connection.ConnectionState#DISCONNECTED}.</p>
137+
* <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>
138138
*/
139139
public void connect() {
140140
connect(null);
@@ -191,12 +191,12 @@ public void connect(final ConnectionEventListener eventListener, ConnectionState
191191
* Disconnect from Pusher.
192192
*
193193
* <p>
194-
* Calls are ignored if the {@link Connection#getState()}, retrieved from {@link Pusher#getConnection}, is not
195-
* {@link com.pusher.client.connection.ConnectionState#CONNECTED}.
194+
* Calls are ignored if the {@link Connection#getState()}, retrieved from {@link Pusher#getConnection}, is
195+
* {@link com.pusher.client.connection.ConnectionState#DISCONNECTING} or {@link com.pusher.client.connection.ConnectionState#DISCONNECTED}.
196196
* </p>
197197
*/
198198
public void disconnect() {
199-
if (connection.getState() == ConnectionState.CONNECTED) {
199+
if (connection.getState() != ConnectionState.DISCONNECTING && connection.getState() != ConnectionState.DISCONNECTED) {
200200
connection.disconnect();
201201
}
202202
}

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ public void connect() {
7575

7676
@Override
7777
public void run() {
78-
if (state == ConnectionState.DISCONNECTED) {
78+
if (canConnect()) {
7979
tryConnecting();
8080
}
8181
}
8282
});
8383
}
8484

85-
private void tryConnecting(){
85+
private void tryConnecting() {
8686
try {
8787
underlyingConnection = factory
8888
.newWebSocketClientWrapper(webSocketUri, proxy, WebSocketConnection.this);
@@ -99,7 +99,7 @@ public void disconnect() {
9999
factory.queueOnEventThread(new Runnable() {
100100
@Override
101101
public void run() {
102-
if (state == ConnectionState.CONNECTED) {
102+
if (canDisconnect()) {
103103
updateState(ConnectionState.DISCONNECTING);
104104
underlyingConnection.close();
105105
}
@@ -294,8 +294,10 @@ private void tryReconnecting() {
294294
factory.getTimers().schedule(new Runnable() {
295295
@Override
296296
public void run() {
297-
underlyingConnection.removeWebSocketListener();
298-
tryConnecting();
297+
if (state == ConnectionState.RECONNECTING) {
298+
underlyingConnection.removeWebSocketListener();
299+
tryConnecting();
300+
}
299301
}
300302
}, reconnectInterval, TimeUnit.SECONDS);
301303
}
@@ -312,8 +314,10 @@ private void cancelTimeoutsAndTransitionToDisconnected() {
312314
factory.queueOnEventThread(new Runnable() {
313315
@Override
314316
public void run() {
315-
updateState(ConnectionState.DISCONNECTED);
316-
factory.shutdownThreads();
317+
if (state == ConnectionState.DISCONNECTING) {
318+
updateState(ConnectionState.DISCONNECTED);
319+
factory.shutdownThreads();
320+
}
317321
}
318322
});
319323
reconnectAttempts = 0;
@@ -334,6 +338,14 @@ public void run() {
334338
});
335339
}
336340

341+
private boolean canConnect() {
342+
return state == ConnectionState.DISCONNECTING || state == ConnectionState.DISCONNECTED;
343+
}
344+
345+
private boolean canDisconnect() {
346+
return state != ConnectionState.DISCONNECTING && state != ConnectionState.DISCONNECTED;
347+
}
348+
337349
private class ActivityTimer {
338350
private final long activityTimeout;
339351
private final long pongTimeout;

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,6 @@ public void testDisconnectCallDoesNothingIfStateIsDisconnected() {
118118
verify(mockConnection, never()).disconnect();
119119
}
120120

121-
@Test
122-
public void testDisconnectCallDoesNothingIfStateIsConnecting() {
123-
when(mockConnection.getState()).thenReturn(ConnectionState.CONNECTING);
124-
125-
pusher.disconnect();
126-
verify(mockConnection, never()).disconnect();
127-
}
128-
129121
@Test
130122
public void testDisconnectCallDoesNothingIfStateIsDisconnecting() {
131123
when(mockConnection.getState()).thenReturn(ConnectionState.DISCONNECTING);

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ public void testConnectUpdatesStateAndNotifiesListener() {
113113
assertEquals(ConnectionState.CONNECTING, connection.getState());
114114
}
115115

116+
@Test
117+
public void testConnectAfterDisconnect() {
118+
connection.connect();
119+
connection.disconnect();
120+
assertEquals(ConnectionState.DISCONNECTING, connection.getState());
121+
connection.connect();
122+
assertEquals(ConnectionState.CONNECTING, connection.getState());
123+
verify(mockUnderlyingConnection, times(2)).connect();
124+
}
125+
116126
@Test
117127
public void testConnectDoesNotCallConnectOnUnderlyingConnectionIfAlreadyInConnectingState() {
118128
connection.connect();
@@ -274,15 +284,6 @@ public void testDisconnectInDisconnectedStateIsIgnored() {
274284
verify(mockEventListener, times(0)).onConnectionStateChange(any(ConnectionStateChange.class));
275285
}
276286

277-
@Test
278-
public void testDisconnectInConnectingStateIsIgnored() {
279-
connection.connect();
280-
281-
connection.disconnect();
282-
283-
verify(mockUnderlyingConnection, times(0)).close();
284-
verify(mockEventListener, times(1)).onConnectionStateChange(any(ConnectionStateChange.class));
285-
}
286287

287288
@Test
288289
public void testDisconnectInDisconnectingStateIsIgnored() {

0 commit comments

Comments
 (0)