Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tpr/issue167: Fixed explicit connect after connection has disconnected #172

Merged
merged 2 commits into from
Oct 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ public String getHost() {

public void connect() {
boolean connectionExist = state.state == ConnectionState.connected;
boolean connectionAttemptAvailable = (requestedState != null && requestedState.state != ConnectionState.connecting) ||
boolean connectionAttemptInProgress = (requestedState != null && requestedState.state == ConnectionState.connecting) ||
state.state == ConnectionState.connecting;

if(!connectionExist && !connectionAttemptAvailable && startThread()) {
if(!connectionExist && !connectionAttemptInProgress) {
startThread(); // Start thread if not already started.
requestState(ConnectionState.connecting);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,38 @@ public void connect_fail_suspended() {
}
}

/**
* Verify that the connection in the disconnected state (after attempts to
* connect to a non-existent ws host) allows an immediate explicit connect
* attempt, instead of ignoring the explicit connect and waiting till the
* next scheduled retry.
*/
@Test
public void connect_while_disconnected() {
try {
TestVars testVars = Setup.getTestVars();
ClientOptions opts = testVars.createOptions(testVars.keys[0].keyStr);
opts.realtimeHost = "non.existent.host";
AblyRealtime ably = new AblyRealtime(opts);
ConnectionWaiter connectionWaiter = new ConnectionWaiter(ably.connection);

connectionWaiter.waitFor(ConnectionState.disconnected);
assertEquals("Verify disconnected state is reached", ConnectionState.disconnected, ably.connection.state);

long before = System.currentTimeMillis();
ably.connection.connect();
connectionWaiter.waitFor(ConnectionState.connecting);
assertTrue("Verify explicit connect is actioned immediately", System.currentTimeMillis() - before < 1000L);

ably.close();
connectionWaiter.waitFor(ConnectionState.closed);
assertEquals("Verify closed state is reached", ConnectionState.closed, ably.connection.state);
} catch (AblyException e) {
e.printStackTrace();
fail("init0: Unexpected exception instantiating library");
}
}

/**
* Verify that the connection enters the disconnected state, after a token
* used for successful connection expires
Expand Down