|
13 | 13 | import org.robolectric.annotation.Config;
|
14 | 14 |
|
15 | 15 | import java.net.URI;
|
| 16 | +import java.util.LinkedList; |
| 17 | +import java.util.Queue; |
16 | 18 | import java.util.concurrent.Executor;
|
17 | 19 |
|
18 | 20 | import bolts.Task;
|
|
23 | 25 | import static org.mockito.AdditionalMatchers.and;
|
24 | 26 | import static org.mockito.AdditionalMatchers.not;
|
25 | 27 | import static org.mockito.Matchers.any;
|
26 |
| -import static org.mockito.Matchers.anyString; |
27 | 28 | import static org.mockito.Matchers.anyBoolean;
|
| 29 | +import static org.mockito.Matchers.anyString; |
28 | 30 | import static org.mockito.Matchers.contains;
|
29 | 31 | import static org.mockito.Matchers.eq;
|
30 | 32 | import static org.mockito.Mockito.mock;
|
@@ -345,6 +347,28 @@ public void testEmptySessionTokenOnSubscribe() {
|
345 | 347 | contains("\"sessionToken\":\"the token\"")));
|
346 | 348 | }
|
347 | 349 |
|
| 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 | + |
348 | 372 | private SubscriptionHandling<ParseObject> createSubscription(ParseQuery<ParseObject> parseQuery,
|
349 | 373 | SubscriptionHandling.HandleSubscribeCallback<ParseObject> subscribeMockCallback) throws Exception {
|
350 | 374 | SubscriptionHandling<ParseObject> subscriptionHandling = parseLiveQueryClient.subscribe(parseQuery).handleSubscribe(subscribeMockCallback);
|
@@ -443,4 +467,39 @@ private static JSONObject createObjectDeleteMessage(int requestId, ParseObject p
|
443 | 467 | jsonObject.put("object", PointerEncoder.get().encodeRelatedObject(parseObject));
|
444 | 468 | return jsonObject;
|
445 | 469 | }
|
| 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 | + } |
446 | 505 | }
|
0 commit comments