FKeepSyncedTest.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #import "FTestBase.h"
  18. #import "FTestHelpers.h"
  19. @interface FKeepSyncedTest : FTestBase
  20. @end
  21. @implementation FKeepSyncedTest
  22. static NSUInteger fGlobalKeepSyncedTestCounter = 0;
  23. - (void)assertIsKeptSynced:(FIRDatabaseQuery *)query {
  24. FIRDatabaseReference *ref = query.ref;
  25. // First set a unique value to the value of child
  26. fGlobalKeepSyncedTestCounter++;
  27. NSNumber *currentValue = @(fGlobalKeepSyncedTestCounter);
  28. __block BOOL done = NO;
  29. [ref setValue:@{@"child" : currentValue}
  30. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  31. XCTAssertNil(error);
  32. done = YES;
  33. }];
  34. WAIT_FOR(done);
  35. done = NO;
  36. // Next go offline, if it's kept synced we should have kept the value, after going offline no way
  37. // to get the value except from cache
  38. [FIRDatabaseReference goOffline];
  39. [query observeSingleEventOfType:FIRDataEventTypeValue
  40. withBlock:^(FIRDataSnapshot *snapshot) {
  41. // We should receive an event
  42. XCTAssertEqualObjects(snapshot.value, @{@"child" : currentValue});
  43. done = YES;
  44. }];
  45. WAIT_FOR(done);
  46. // All good, go back online
  47. [FIRDatabaseReference goOnline];
  48. }
  49. - (void)assertNotKeptSynced:(FIRDatabaseQuery *)query {
  50. FIRDatabaseReference *ref = query.ref;
  51. // First set a unique value to the value of child
  52. fGlobalKeepSyncedTestCounter++;
  53. NSNumber *currentValue = @(fGlobalKeepSyncedTestCounter);
  54. fGlobalKeepSyncedTestCounter++;
  55. NSNumber *newValue = @(fGlobalKeepSyncedTestCounter);
  56. __block BOOL done = NO;
  57. [ref setValue:@{@"child" : currentValue}
  58. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  59. XCTAssertNil(error);
  60. done = YES;
  61. }];
  62. WAIT_FOR(done);
  63. done = NO;
  64. // Next go offline, if it's kept synced we should have kept the value, after going offline no way
  65. // to get the value except from cache
  66. [FIRDatabaseReference goOffline];
  67. [query observeSingleEventOfType:FIRDataEventTypeValue
  68. withBlock:^(FIRDataSnapshot *snapshot) {
  69. // We should receive an event
  70. XCTAssertEqualObjects(snapshot.value, @{@"child" : newValue});
  71. done = YES;
  72. }];
  73. // By now, if we had it synced we should have gotten an event with the wrong value
  74. // Write a new value so the value event listener will be triggered
  75. [ref setValue:@{@"child" : newValue}];
  76. WAIT_FOR(done);
  77. // All good, go back online
  78. [FIRDatabaseReference goOnline];
  79. }
  80. - (void)testKeepSynced {
  81. FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
  82. [ref keepSynced:YES];
  83. [self assertIsKeptSynced:ref];
  84. [ref keepSynced:NO];
  85. [self assertNotKeptSynced:ref];
  86. }
  87. - (void)testManyKeepSyncedCallsDontAccumulate {
  88. FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
  89. [ref keepSynced:YES];
  90. [ref keepSynced:YES];
  91. [ref keepSynced:YES];
  92. [self assertIsKeptSynced:ref];
  93. // If it were balanced, this would not be enough
  94. [ref keepSynced:NO];
  95. [ref keepSynced:NO];
  96. [self assertNotKeptSynced:ref];
  97. // If it were balanced, this would not be enough
  98. [ref keepSynced:YES];
  99. [self assertIsKeptSynced:ref];
  100. // cleanup
  101. [ref keepSynced:NO];
  102. }
  103. - (void)testRemoveAllObserversDoesNotAffectKeepSynced {
  104. FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
  105. [ref keepSynced:YES];
  106. [self assertIsKeptSynced:ref];
  107. [ref removeAllObservers];
  108. [self assertIsKeptSynced:ref];
  109. // cleanup
  110. [ref keepSynced:NO];
  111. }
  112. - (void)testRemoveSingleObserverDoesNotAffectKeepSynced {
  113. FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
  114. [ref keepSynced:YES];
  115. [self assertIsKeptSynced:ref];
  116. __block BOOL done = NO;
  117. FIRDatabaseHandle handle = [ref observeEventType:FIRDataEventTypeValue
  118. withBlock:^(FIRDataSnapshot *snapshot) {
  119. done = YES;
  120. }];
  121. WAIT_FOR(done);
  122. [ref removeObserverWithHandle:handle];
  123. [self assertIsKeptSynced:ref];
  124. // cleanup
  125. [ref keepSynced:NO];
  126. }
  127. - (void)testKeepSyncedNoDoesNotAffectExistingObserver {
  128. FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
  129. [ref keepSynced:YES];
  130. [self assertIsKeptSynced:ref];
  131. __block BOOL done = NO;
  132. FIRDatabaseHandle handle = [ref observeEventType:FIRDataEventTypeValue
  133. withBlock:^(FIRDataSnapshot *snapshot) {
  134. done = [snapshot.value isEqual:@"done"];
  135. }];
  136. // cleanup
  137. [ref keepSynced:NO];
  138. [ref setValue:@"done"];
  139. WAIT_FOR(done);
  140. [ref removeObserverWithHandle:handle];
  141. }
  142. - (void)testDifferentQueriesAreIndependent {
  143. FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
  144. FIRDatabaseQuery *query1 = [ref queryLimitedToFirst:1];
  145. FIRDatabaseQuery *query2 = [ref queryLimitedToFirst:2];
  146. [query1 keepSynced:YES];
  147. [self assertIsKeptSynced:query1];
  148. [self assertNotKeptSynced:query2];
  149. [query2 keepSynced:YES];
  150. [self assertIsKeptSynced:query1];
  151. [self assertIsKeptSynced:query2];
  152. [query1 keepSynced:NO];
  153. [self assertIsKeptSynced:query2];
  154. [self assertNotKeptSynced:query1];
  155. [query2 keepSynced:NO];
  156. [self assertNotKeptSynced:query1];
  157. [self assertNotKeptSynced:query2];
  158. }
  159. - (void)testChildIsKeptSynced {
  160. FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
  161. FIRDatabaseReference *child = [ref child:@"random-child"];
  162. [ref keepSynced:YES];
  163. [self assertIsKeptSynced:child];
  164. // cleanup
  165. [ref keepSynced:NO];
  166. }
  167. - (void)testRootIsKeptSynced {
  168. FIRDatabaseReference *ref = [[FTestHelpers getRandomNodeWithoutPersistence] root];
  169. [ref keepSynced:YES];
  170. // Run on random child to make sure writes from this test doesn't interfere with any other tests.
  171. [self assertIsKeptSynced:[ref childByAutoId]];
  172. // cleanup
  173. [ref keepSynced:NO];
  174. }
  175. // TODO[offline]: Cancel listens for keep synced....
  176. @end