-
-
Notifications
You must be signed in to change notification settings - Fork 32
Add callbacks to be notified of connect/disconnect/error events #34
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.parse; | ||
|
||
public interface ParseLiveQueryClientCallbacks { | ||
void onLiveQueryClientConnected(ParseLiveQueryClient client); | ||
|
||
void onLiveQueryClientDisconnected(ParseLiveQueryClient client); | ||
|
||
void onLiveQueryError(ParseLiveQueryClient client, LiveQueryException reason); | ||
|
||
void onSocketError(ParseLiveQueryClient client, Throwable reason); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,13 @@ | |
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Mockito; | ||
import org.mockito.invocation.InvocationOnMock; | ||
import org.mockito.stubbing.Answer; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
import org.robolectric.util.Transcript; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
|
@@ -398,6 +399,46 @@ public void testDisconnectOnBackgroundThread() throws Exception { | |
verify(webSocketClient, times(1)).close(); | ||
} | ||
|
||
@Test | ||
public void testCallbackNotifiedOnDisconnect() throws Exception { | ||
LoggingCallbacks callbacks = new LoggingCallbacks(); | ||
parseLiveQueryClient.registerListener(callbacks); | ||
callbacks.transcript.assertNoEventsSoFar(); | ||
|
||
webSocketClientCallback.onClose(); | ||
callbacks.transcript.assertEventsSoFar("onLiveQueryClientDisconnected"); | ||
} | ||
|
||
@Test | ||
public void testCallbackNotifiedOnConnect() throws Exception { | ||
LoggingCallbacks callbacks = new LoggingCallbacks(); | ||
parseLiveQueryClient.registerListener(callbacks); | ||
callbacks.transcript.assertNoEventsSoFar(); | ||
|
||
reconnect(); | ||
callbacks.transcript.assertEventsSoFar("onLiveQueryClientConnected"); | ||
} | ||
|
||
@Test | ||
public void testCallbackNotifiedOnSocketError() throws Exception { | ||
LoggingCallbacks callbacks = new LoggingCallbacks(); | ||
parseLiveQueryClient.registerListener(callbacks); | ||
callbacks.transcript.assertNoEventsSoFar(); | ||
|
||
webSocketClientCallback.onError(new IOException("bad things happened")); | ||
callbacks.transcript.assertEventsSoFar("onSocketError: java.io.IOException: bad things happened"); | ||
} | ||
|
||
@Test | ||
public void testCallbackNotifiedOnServerError() throws Exception { | ||
LoggingCallbacks callbacks = new LoggingCallbacks(); | ||
parseLiveQueryClient.registerListener(callbacks); | ||
callbacks.transcript.assertNoEventsSoFar(); | ||
|
||
webSocketClientCallback.onMessage(createErrorMessage(1).toString()); | ||
callbacks.transcript.assertEventsSoFar("onLiveQueryError: com.parse.LiveQueryException$ServerReportedException: Server reported error; code: 1, error: testError, reconnect: true"); | ||
} | ||
|
||
private SubscriptionHandling<ParseObject> createSubscription(ParseQuery<ParseObject> parseQuery, | ||
SubscriptionHandling.HandleSubscribeCallback<ParseObject> subscribeMockCallback) throws Exception { | ||
SubscriptionHandling<ParseObject> subscriptionHandling = parseLiveQueryClient.subscribe(parseQuery).handleSubscribe(subscribeMockCallback); | ||
|
@@ -498,6 +539,30 @@ private static JSONObject createObjectDeleteMessage(int requestId, ParseObject p | |
return jsonObject; | ||
} | ||
|
||
private static class LoggingCallbacks implements ParseLiveQueryClientCallbacks { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be non-static given how you instantiate it in each test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think static vs non-static is relevant based on how it's instantiated here. It's declared static because it does not rely on anything from the outer/containing class (so it doesn't require an instance of the containing class). It's probably not relevant in a unit test, but in general I prefer static inner class, unless it is explicitly required that the inner class instance must be associated with an instance of the outer class (such as a callback method that has to invoke something from the containing class). Not declaring the class static, even if you don't need make any calls to the outer class, can result in memory leaks since that instance of the inner class necessarily holds a reference to the instance of the outer class that created it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was more worried that the use of transcript could be misused if the class isn't instantiated but it looks to be declared private so can't be accessed directly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah. Yeah the class still needs to be instantiated in order to use Transcript. It's declared Just want to make sure we're on the same page about the class and field declarations. The class being declared There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha, thanks for the clarification. |
||
final Transcript transcript = new Transcript(); | ||
|
||
@Override | ||
public void onLiveQueryClientConnected(ParseLiveQueryClient client) { | ||
transcript.add("onLiveQueryClientConnected"); | ||
} | ||
|
||
@Override | ||
public void onLiveQueryClientDisconnected(ParseLiveQueryClient client) { | ||
transcript.add("onLiveQueryClientDisconnected"); | ||
} | ||
|
||
@Override | ||
public void onLiveQueryError(ParseLiveQueryClient client, LiveQueryException reason) { | ||
transcript.add("onLiveQueryError: " + reason); | ||
} | ||
|
||
@Override | ||
public void onSocketError(ParseLiveQueryClient client, Throwable reason) { | ||
transcript.add("onSocketError: " + reason); | ||
} | ||
} | ||
|
||
private static class PauseableExecutor implements Executor { | ||
private boolean isPaused = false; | ||
private final Queue<Runnable> queue = new LinkedList<>(); | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need to put dispatchConnected() on
onOpen()
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dispatchConnected() happens only when the server responds with the
op=connected
messageThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gotcha