Skip to content

Commit

Permalink
Use disconnectAndSuppressRetries to pause connectionManager in RTN15g…
Browse files Browse the repository at this point in the history
…* tests
  • Loading branch information
funkyboy committed May 18, 2018
1 parent 3e41787 commit 810cef4
Showing 1 changed file with 18 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -465,38 +465,27 @@ public void onConnectionStateChanged(ConnectionStateChange state) {
connectionWaiter.waitFor(ConnectionState.connected);
final String firstConnectionId = ably.connection.id;

ably.connection.once(ConnectionEvent.disconnected, new ConnectionStateListener() {
@Override
public void onConnectionStateChanged(ConnectionStateChange state) {
synchronized (ably.connection.connectionManager) {
try {
/* The client will try to reconnect right away after disconnection.
* We want it to stay into a disconnected state long enough
* so that the connection becomes stale.
*/
ably.connection.connectionManager.wait(waitInDisconnectedState);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});

ably.connection.connectionManager.requestState(ConnectionState.disconnected);
/* suppress automatic retries by the connection manager and disconnect */
try {
Method method = ably.connection.connectionManager.getClass().getDeclaredMethod("disconnectAndSuppressRetries");
method.setAccessible(true);
method.invoke(ably.connection.connectionManager);
} catch (NoSuchMethodException|IllegalAccessException|InvocationTargetException e) {
fail("Unexpected exception in suppressing retries");
}
connectionWaiter.waitFor(ConnectionState.disconnected);
assertEquals("Disconnected state was not reached", ConnectionState.disconnected, ably.connection.state);

ably.connection.once(ConnectionEvent.connected, new ConnectionStateListener() {
@Override
public void onConnectionStateChanged(ConnectionStateChange state) {
assertEquals("Client has reconnected", ConnectionState.connected, state.current);
String secondConnectionId = ably.connection.id;
assertNotNull(secondConnectionId);
assertNotEquals("connection has a different id", firstConnectionId, secondConnectionId);
ably.close();
}
});
/* Wait for the connection to go stale, then reconnect */
try { Thread.sleep(waitInDisconnectedState); } catch(InterruptedException e) {}
ably.connection.connect();
connectionWaiter.waitFor(ConnectionState.connected);
assertEquals("Connected state was not reached", ConnectionState.connected, ably.connection.state);

connectionWaiter.waitFor(ConnectionState.closed);
/* Verify the connection is new */
assertNotNull(ably.connection.id);
assertNotEquals("Connection has the same id", firstConnectionId, ably.connection.id);
ably.close();

} catch (AblyException e) {
e.printStackTrace();
Expand Down

0 comments on commit 810cef4

Please sign in to comment.