Skip to content

Commit 312c1b8

Browse files
author
Joe Hansche
committed
Add test for #21
1 parent 40191c0 commit 312c1b8

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

ParseLiveQuery/src/test/java/com/parse/TestParseLiveQueryClient.java

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import org.robolectric.annotation.Config;
1414

1515
import java.net.URI;
16+
import java.util.LinkedList;
17+
import java.util.Queue;
1618
import java.util.concurrent.Executor;
1719

1820
import bolts.Task;
@@ -23,8 +25,8 @@
2325
import static org.mockito.AdditionalMatchers.and;
2426
import static org.mockito.AdditionalMatchers.not;
2527
import static org.mockito.Matchers.any;
26-
import static org.mockito.Matchers.anyString;
2728
import static org.mockito.Matchers.anyBoolean;
29+
import static org.mockito.Matchers.anyString;
2830
import static org.mockito.Matchers.contains;
2931
import static org.mockito.Matchers.eq;
3032
import static org.mockito.Mockito.mock;
@@ -345,6 +347,28 @@ public void testEmptySessionTokenOnSubscribe() {
345347
contains("\"sessionToken\":\"the token\"")));
346348
}
347349

350+
@Test
351+
public void testDisconnectOnBackgroundThread() throws Exception {
352+
PauseableExecutor executor = new PauseableExecutor();
353+
// Recreate the client with a PauseableExecutor
354+
parseLiveQueryClient = ParseLiveQueryClient.Factory.getClient(new URI(""), new WebSocketClientFactory() {
355+
@Override
356+
public WebSocketClient createInstance(WebSocketClient.WebSocketClientCallback webSocketClientCallback, URI hostUrl) {
357+
TestParseLiveQueryClient.this.webSocketClientCallback = webSocketClientCallback;
358+
webSocketClient = mock(WebSocketClient.class);
359+
return webSocketClient;
360+
}
361+
}, executor);
362+
363+
reconnect();
364+
executor.pause();
365+
366+
parseLiveQueryClient.disconnect();
367+
verify(webSocketClient, never()).close();
368+
assertTrue(executor.advanceOne());
369+
verify(webSocketClient, times(1)).close();
370+
}
371+
348372
private SubscriptionHandling<ParseObject> createSubscription(ParseQuery<ParseObject> parseQuery,
349373
SubscriptionHandling.HandleSubscribeCallback<ParseObject> subscribeMockCallback) throws Exception {
350374
SubscriptionHandling<ParseObject> subscriptionHandling = parseLiveQueryClient.subscribe(parseQuery).handleSubscribe(subscribeMockCallback);
@@ -443,4 +467,39 @@ private static JSONObject createObjectDeleteMessage(int requestId, ParseObject p
443467
jsonObject.put("object", PointerEncoder.get().encodeRelatedObject(parseObject));
444468
return jsonObject;
445469
}
470+
471+
private static class PauseableExecutor implements Executor {
472+
private boolean isPaused = false;
473+
private final Queue<Runnable> queue = new LinkedList<>();
474+
475+
void pause() {
476+
isPaused = true;
477+
}
478+
479+
void unpause() {
480+
if (isPaused) {
481+
isPaused = false;
482+
483+
//noinspection StatementWithEmptyBody
484+
while (advanceOne()) {
485+
// keep going
486+
}
487+
}
488+
}
489+
490+
boolean advanceOne() {
491+
Runnable next = queue.poll();
492+
if (next != null) next.run();
493+
return next != null;
494+
}
495+
496+
@Override
497+
public void execute(Runnable runnable) {
498+
if (isPaused) {
499+
queue.add(runnable);
500+
} else {
501+
runnable.run();
502+
}
503+
}
504+
}
446505
}

0 commit comments

Comments
 (0)