|
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; |
|
37 | 39 | @Config(constants = BuildConfig.class, sdk = 21) |
38 | 40 | public class TestParseLiveQueryClient { |
39 | 41 |
|
| 42 | + private PauseableExecutor executor; |
40 | 43 | private WebSocketClient webSocketClient; |
41 | 44 | private WebSocketClient.WebSocketClientCallback webSocketClientCallback; |
42 | 45 | private ParseLiveQueryClient<ParseObject> parseLiveQueryClient; |
@@ -64,19 +67,16 @@ public Task<String> answer(InvocationOnMock invocation) throws Throwable { |
64 | 67 | }); |
65 | 68 | ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); |
66 | 69 |
|
| 70 | + executor = new PauseableExecutor(); |
| 71 | + |
67 | 72 | parseLiveQueryClient = ParseLiveQueryClient.Factory.getClient(new URI(""), new WebSocketClientFactory() { |
68 | 73 | @Override |
69 | 74 | public WebSocketClient createInstance(WebSocketClient.WebSocketClientCallback webSocketClientCallback, URI hostUrl) { |
70 | 75 | TestParseLiveQueryClient.this.webSocketClientCallback = webSocketClientCallback; |
71 | 76 | webSocketClient = mock(WebSocketClient.class); |
72 | 77 | return webSocketClient; |
73 | 78 | } |
74 | | - }, new Executor() { |
75 | | - @Override |
76 | | - public void execute(Runnable command) { |
77 | | - command.run(); |
78 | | - } |
79 | | - }); |
| 79 | + }, executor); |
80 | 80 | reconnect(); |
81 | 81 | } |
82 | 82 |
|
@@ -345,6 +345,16 @@ public void testEmptySessionTokenOnSubscribe() { |
345 | 345 | contains("\"sessionToken\":\"the token\""))); |
346 | 346 | } |
347 | 347 |
|
| 348 | + @Test |
| 349 | + public void testDisconnectOnBackgroundThread() throws Exception { |
| 350 | + executor.pause(); |
| 351 | + |
| 352 | + parseLiveQueryClient.disconnect(); |
| 353 | + verify(webSocketClient, never()).close(); |
| 354 | + assertTrue(executor.advanceOne()); |
| 355 | + verify(webSocketClient, times(1)).close(); |
| 356 | + } |
| 357 | + |
348 | 358 | private SubscriptionHandling<ParseObject> createSubscription(ParseQuery<ParseObject> parseQuery, |
349 | 359 | SubscriptionHandling.HandleSubscribeCallback<ParseObject> subscribeMockCallback) throws Exception { |
350 | 360 | SubscriptionHandling<ParseObject> subscriptionHandling = parseLiveQueryClient.subscribe(parseQuery).handleSubscribe(subscribeMockCallback); |
@@ -443,4 +453,39 @@ private static JSONObject createObjectDeleteMessage(int requestId, ParseObject p |
443 | 453 | jsonObject.put("object", PointerEncoder.get().encodeRelatedObject(parseObject)); |
444 | 454 | return jsonObject; |
445 | 455 | } |
| 456 | + |
| 457 | + private static class PauseableExecutor implements Executor { |
| 458 | + private boolean isPaused = false; |
| 459 | + private final Queue<Runnable> queue = new LinkedList<>(); |
| 460 | + |
| 461 | + void pause() { |
| 462 | + isPaused = true; |
| 463 | + } |
| 464 | + |
| 465 | + void unpause() { |
| 466 | + if (isPaused) { |
| 467 | + isPaused = false; |
| 468 | + |
| 469 | + //noinspection StatementWithEmptyBody |
| 470 | + while (advanceOne()) { |
| 471 | + // keep going |
| 472 | + } |
| 473 | + } |
| 474 | + } |
| 475 | + |
| 476 | + boolean advanceOne() { |
| 477 | + Runnable next = queue.poll(); |
| 478 | + if (next != null) next.run(); |
| 479 | + return next != null; |
| 480 | + } |
| 481 | + |
| 482 | + @Override |
| 483 | + public void execute(Runnable runnable) { |
| 484 | + if (isPaused) { |
| 485 | + queue.add(runnable); |
| 486 | + } else { |
| 487 | + runnable.run(); |
| 488 | + } |
| 489 | + } |
| 490 | + } |
446 | 491 | } |
0 commit comments