|
1 | 1 | package com.parse;
|
2 | 2 |
|
| 3 | +import org.assertj.core.api.Assertions; |
3 | 4 | import org.json.JSONException;
|
4 | 5 | import org.json.JSONObject;
|
5 | 6 | import org.junit.After;
|
@@ -82,6 +83,42 @@ public void tearDown() throws Exception {
|
82 | 83 | ParsePlugins.reset();
|
83 | 84 | }
|
84 | 85 |
|
| 86 | + @Test |
| 87 | + public void testSubscribeBetweenSocketConnectAndConnectResponse() throws Exception { |
| 88 | + ParseQuery<ParseObject> queryA = ParseQuery.getQuery("objA"); |
| 89 | + ParseQuery<ParseObject> queryB = ParseQuery.getQuery("objB"); |
| 90 | + clearConnection(); |
| 91 | + |
| 92 | + // This will trigger connectIfNeeded(), which calls reconnect() |
| 93 | + SubscriptionHandling<ParseObject> subA = parseLiveQueryClient.subscribe(queryA); |
| 94 | + |
| 95 | + verify(webSocketClient, times(1)).open(); |
| 96 | + verify(webSocketClient, never()).send(anyString()); |
| 97 | + |
| 98 | + // Now the socket is open |
| 99 | + webSocketClientCallback.onOpen(); |
| 100 | + when(webSocketClient.getState()).thenReturn(WebSocketClient.State.CONNECTED); |
| 101 | + // and we send op=connect |
| 102 | + verify(webSocketClient, times(1)).send(contains("\"op\":\"connect\"")); |
| 103 | + |
| 104 | + // Now if we subscribe to queryB, we SHOULD NOT send the subscribe yet, until we get op=connected |
| 105 | + SubscriptionHandling<ParseObject> subB = parseLiveQueryClient.subscribe(queryB); // TODO: fix this state |
| 106 | + verify(webSocketClient, never()).send(contains("\"op\":\"subscribe\"")); |
| 107 | + |
| 108 | + // on op=connected, _then_ we should send both subscriptions |
| 109 | + webSocketClientCallback.onMessage(createConnectedMessage().toString()); |
| 110 | + verify(webSocketClient, times(2)).send(contains("\"op\":\"subscribe\"")); |
| 111 | + |
| 112 | + // 1. Subscribe to queryA |
| 113 | + // - it is not connected yet, so it will trigger reconnect. |
| 114 | + // 2. Socket opens & connects; initiate op=connect |
| 115 | + // 3. subscribe to queryB |
| 116 | + // - SOCKET is connected, but we haven't received op=connected yet. |
| 117 | + // - BUG: it will call sendSubscription now |
| 118 | + // 4. Server responds to #2 with op=connected |
| 119 | + // 5. On op=connected, we replay pending subscriptions, including the one that was already sent in #3 |
| 120 | + } |
| 121 | + |
85 | 122 | @Test
|
86 | 123 | public void testSubscribeWhenSubscribedToCallback() throws Exception {
|
87 | 124 | SubscriptionHandling.HandleSubscribeCallback<ParseObject> subscribeMockCallback = mock(SubscriptionHandling.HandleSubscribeCallback.class);
|
@@ -459,6 +496,11 @@ private void validateSameObject(SubscriptionHandling.HandleEventCallback<ParseOb
|
459 | 496 | assertEquals(originalParseObject.getObjectId(), newParseObject.getObjectId());
|
460 | 497 | }
|
461 | 498 |
|
| 499 | + private void clearConnection() { |
| 500 | + webSocketClient = null; |
| 501 | + webSocketClientCallback = null; |
| 502 | + } |
| 503 | + |
462 | 504 | private void reconnect() {
|
463 | 505 | parseLiveQueryClient.reconnect();
|
464 | 506 | webSocketClientCallback.onOpen();
|
|
0 commit comments