Skip to content

Commit

Permalink
Dispatch better disconnect events (#39)
Browse files Browse the repository at this point in the history
* Fix #38 by sending a proper close() status code

See https://tools.ietf.org/html/rfc6455#section-7.4

* Add userInitiated boolean to the on-disconnected callback

That lets us distinguish between an expected and unexpected disconnect.

* Dispatch onDisconnected after a socket error

A socket failure implies that the socket also died, so it makes sense to also
dispatch a disconnected event.

* Fix CR comments
  • Loading branch information
Joe authored Mar 29, 2017
1 parent 5ee79d9 commit 98a919f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public WebSocketClient createInstance(WebSocketClient.WebSocketClientCallback we
return new OkHttp3WebSocketClient(mClient, webSocketClientCallback, hostUrl);
}

class OkHttp3WebSocketClient implements WebSocketClient {
static class OkHttp3WebSocketClient implements WebSocketClient {

private static final String LOG_TAG = "OkHttpWebSocketClient";

Expand All @@ -38,7 +38,7 @@ class OkHttp3WebSocketClient implements WebSocketClient {
private State state = State.NONE;
private final OkHttpClient client;
private final String url;
private final int STATUS_CODE = 200;
private final int STATUS_CODE = 1000;
private final String CLOSING_MSG = "User invoked close";

private final WebSocketListener handler = new WebSocketListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public interface ParseLiveQueryClientCallbacks {
void onLiveQueryClientConnected(ParseLiveQueryClient client);

void onLiveQueryClientDisconnected(ParseLiveQueryClient client);
void onLiveQueryClientDisconnected(ParseLiveQueryClient client, boolean userInitiated);

void onLiveQueryError(ParseLiveQueryClient client, LiveQueryException reason);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ public Void then(Task<Void> task) throws Exception {
@Override
public void disconnect() {
if (webSocketClient != null) {
disconnectAsync();
userInitiatedDisconnect = true;
disconnectAsync();
}
}

Expand Down Expand Up @@ -254,7 +254,7 @@ private void dispatchConnected() {

private void dispatchDisconnected() {
for (ParseLiveQueryClientCallbacks callback : mCallbacks) {
callback.onLiveQueryClientDisconnected(this);
callback.onLiveQueryClientDisconnected(this, userInitiatedDisconnect);
}
}

Expand All @@ -266,9 +266,13 @@ private void dispatchServerError(LiveQueryException exc) {
}

private void dispatchSocketError(Throwable reason) {
userInitiatedDisconnect = false;

for (ParseLiveQueryClientCallbacks callback : mCallbacks) {
callback.onSocketError(this, reason);
}

dispatchDisconnected();
}

private <T extends ParseObject> void handleSubscribedEvent(JSONObject jsonObject) throws JSONException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,29 @@ public void testDisconnectOnBackgroundThread() throws Exception {
}

@Test
public void testCallbackNotifiedOnDisconnect() throws Exception {
public void testCallbackNotifiedOnUnexpectedDisconnect() throws Exception {
LoggingCallbacks callbacks = new LoggingCallbacks();
parseLiveQueryClient.registerListener(callbacks);
callbacks.transcript.assertNoEventsSoFar();

// Unexpected close from the server:
webSocketClientCallback.onClose();
callbacks.transcript.assertEventsSoFar("onLiveQueryClientDisconnected");
callbacks.transcript.assertEventsSoFar("onLiveQueryClientDisconnected: false");
}

@Test
public void testCallbackNotifiedOnExpectedDisconnect() throws Exception {
LoggingCallbacks callbacks = new LoggingCallbacks();
parseLiveQueryClient.registerListener(callbacks);
callbacks.transcript.assertNoEventsSoFar();

parseLiveQueryClient.disconnect();
verify(webSocketClient, times(1)).close();

callbacks.transcript.assertNoEventsSoFar();
// the client is a mock, so it won't actually invoke the callback automatically
webSocketClientCallback.onClose();
callbacks.transcript.assertEventsSoFar("onLiveQueryClientDisconnected: true");
}

@Test
Expand All @@ -426,7 +442,8 @@ public void testCallbackNotifiedOnSocketError() throws Exception {
callbacks.transcript.assertNoEventsSoFar();

webSocketClientCallback.onError(new IOException("bad things happened"));
callbacks.transcript.assertEventsSoFar("onSocketError: java.io.IOException: bad things happened");
callbacks.transcript.assertEventsSoFar("onSocketError: java.io.IOException: bad things happened",
"onLiveQueryClientDisconnected: false");
}

@Test
Expand Down Expand Up @@ -548,8 +565,8 @@ public void onLiveQueryClientConnected(ParseLiveQueryClient client) {
}

@Override
public void onLiveQueryClientDisconnected(ParseLiveQueryClient client) {
transcript.add("onLiveQueryClientDisconnected");
public void onLiveQueryClientDisconnected(ParseLiveQueryClient client, boolean userInitiated) {
transcript.add("onLiveQueryClientDisconnected: " + userInitiated);
}

@Override
Expand Down

0 comments on commit 98a919f

Please sign in to comment.