FKeepSyncedTest.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 "FTestHelpers.h"
  18. #import "FTestBase.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} withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  30. XCTAssertNil(error);
  31. done = YES;
  32. }];
  33. WAIT_FOR(done);
  34. done = NO;
  35. // Next go offline, if it's kept synced we should have kept the value, after going offline no way to get the value
  36. // except from cache
  37. [FIRDatabaseReference goOffline];
  38. [query observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  39. // We should receive an event
  40. XCTAssertEqualObjects(snapshot.value, @{@"child" : currentValue});
  41. done = YES;
  42. }];
  43. WAIT_FOR(done);
  44. // All good, go back online
  45. [FIRDatabaseReference goOnline];
  46. }
  47. - (void)assertNotKeptSynced:(FIRDatabaseQuery *)query {
  48. FIRDatabaseReference *ref = query.ref;
  49. // First set a unique value to the value of child
  50. fGlobalKeepSyncedTestCounter++;
  51. NSNumber *currentValue = @(fGlobalKeepSyncedTestCounter);
  52. fGlobalKeepSyncedTestCounter++;
  53. NSNumber *newValue = @(fGlobalKeepSyncedTestCounter);
  54. __block BOOL done = NO;
  55. [ref setValue:@{ @"child": currentValue} withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  56. XCTAssertNil(error);
  57. done = YES;
  58. }];
  59. WAIT_FOR(done);
  60. done = NO;
  61. // Next go offline, if it's kept synced we should have kept the value, after going offline no way to get the value
  62. // except from cache
  63. [FIRDatabaseReference goOffline];
  64. [query observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  65. // We should receive an event
  66. XCTAssertEqualObjects(snapshot.value, @{@"child" : newValue});
  67. done = YES;
  68. }];
  69. // By now, if we had it synced we should have gotten an event with the wrong value
  70. // Write a new value so the value event listener will be triggered
  71. [ref setValue:@{ @"child": newValue}];
  72. WAIT_FOR(done);
  73. // All good, go back online
  74. [FIRDatabaseReference goOnline];
  75. }
  76. - (void)testKeepSynced {
  77. FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
  78. [ref keepSynced:YES];
  79. [self assertIsKeptSynced:ref];
  80. [ref keepSynced:NO];
  81. [self assertNotKeptSynced:ref];
  82. }
  83. - (void)testManyKeepSyncedCallsDontAccumulate {
  84. FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
  85. [ref keepSynced:YES];
  86. [ref keepSynced:YES];
  87. [ref keepSynced:YES];
  88. [self assertIsKeptSynced:ref];
  89. // If it were balanced, this would not be enough
  90. [ref keepSynced:NO];
  91. [ref keepSynced:NO];
  92. [self assertNotKeptSynced:ref];
  93. // If it were balanced, this would not be enough
  94. [ref keepSynced:YES];
  95. [self assertIsKeptSynced:ref];
  96. // cleanup
  97. [ref keepSynced:NO];
  98. }
  99. - (void)testRemoveAllObserversDoesNotAffectKeepSynced {
  100. FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
  101. [ref keepSynced:YES];
  102. [self assertIsKeptSynced:ref];
  103. [ref removeAllObservers];
  104. [self assertIsKeptSynced:ref];
  105. // cleanup
  106. [ref keepSynced:NO];
  107. }
  108. - (void)testRemoveSingleObserverDoesNotAffectKeepSynced {
  109. FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
  110. [ref keepSynced:YES];
  111. [self assertIsKeptSynced:ref];
  112. __block BOOL done = NO;
  113. FIRDatabaseHandle handle = [ref observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  114. done = YES;
  115. }];
  116. WAIT_FOR(done);
  117. [ref removeObserverWithHandle:handle];
  118. [self assertIsKeptSynced:ref];
  119. // cleanup
  120. [ref keepSynced:NO];
  121. }
  122. - (void)testKeepSyncedNoDoesNotAffectExistingObserver {
  123. FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
  124. [ref keepSynced:YES];
  125. [self assertIsKeptSynced:ref];
  126. __block BOOL done = NO;
  127. FIRDatabaseHandle handle = [ref observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  128. done = [snapshot.value isEqual:@"done"];
  129. }];
  130. // cleanup
  131. [ref keepSynced:NO];
  132. [ref setValue:@"done"];
  133. WAIT_FOR(done);
  134. [ref removeObserverWithHandle:handle];
  135. }
  136. - (void)testDifferentQueriesAreIndependent {
  137. FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
  138. FIRDatabaseQuery *query1 = [ref queryLimitedToFirst:1];
  139. FIRDatabaseQuery *query2 = [ref queryLimitedToFirst:2];
  140. [query1 keepSynced:YES];
  141. [self assertIsKeptSynced:query1];
  142. [self assertNotKeptSynced:query2];
  143. [query2 keepSynced:YES];
  144. [self assertIsKeptSynced:query1];
  145. [self assertIsKeptSynced:query2];
  146. [query1 keepSynced:NO];
  147. [self assertIsKeptSynced:query2];
  148. [self assertNotKeptSynced:query1];
  149. [query2 keepSynced:NO];
  150. [self assertNotKeptSynced:query1];
  151. [self assertNotKeptSynced:query2];
  152. }
  153. - (void)testChildIsKeptSynced {
  154. FIRDatabaseReference *ref = [FTestHelpers getRandomNodeWithoutPersistence];
  155. FIRDatabaseReference *child = [ref child:@"random-child"];
  156. [ref keepSynced:YES];
  157. [self assertIsKeptSynced:child];
  158. // cleanup
  159. [ref keepSynced:NO];
  160. }
  161. - (void)testRootIsKeptSynced {
  162. FIRDatabaseReference *ref = [[FTestHelpers getRandomNodeWithoutPersistence] root];
  163. [ref keepSynced:YES];
  164. // Run on random child to make sure writes from this test doesn't interfere with any other tests.
  165. [self assertIsKeptSynced:[ref childByAutoId]];
  166. // cleanup
  167. [ref keepSynced:NO];
  168. }
  169. // TODO[offline]: Cancel listens for keep synced....
  170. @end