FIRBundlesTests.mm 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright 2021 Google LLC
  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 <FirebaseFirestore/FirebaseFirestore.h>
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Source/API/FIRLoadBundleTask+Internal.h"
  19. #import "Firestore/Source/API/FIRLocalCacheSettings+Internal.h"
  20. #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
  21. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  22. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  23. #include "Firestore/core/src/util/string_apple.h"
  24. #include "Firestore/core/test/unit/testutil/bundle_builder.h"
  25. using firebase::firestore::testutil::CreateBundle;
  26. using firebase::firestore::util::MakeNSString;
  27. using firebase::firestore::util::MakeString;
  28. @interface FIRBundlesTests : FSTIntegrationTestCase
  29. @end
  30. @implementation FIRBundlesTests
  31. // Clears persistence for each test method to have a clean start.
  32. - (void)setUp {
  33. [super setUp];
  34. self.db = [self firestore];
  35. XCTestExpectation* exp = [self expectationWithDescription:@"clear persistence"];
  36. [self.db clearPersistenceWithCompletion:^(NSError*) {
  37. [exp fulfill];
  38. }];
  39. [self awaitExpectation:exp];
  40. }
  41. - (void)verifyProgress:(FIRLoadBundleTaskProgress*)progress hasLoadedDocument:(int32_t)loaded {
  42. XCTAssertEqual(progress.state, FIRLoadBundleTaskStateInProgress);
  43. XCTAssertLessThanOrEqual(progress.bytesLoaded, progress.totalBytes);
  44. XCTAssertLessThanOrEqual(progress.documentsLoaded, progress.totalDocuments);
  45. XCTAssertEqual(progress.documentsLoaded, loaded);
  46. }
  47. - (void)verifySuccessProgress:(FIRLoadBundleTaskProgress*)progress {
  48. XCTAssertEqual(progress.state, FIRLoadBundleTaskStateSuccess);
  49. XCTAssertGreaterThan(progress.bytesLoaded, 0);
  50. XCTAssertEqual(progress.bytesLoaded, progress.totalBytes);
  51. XCTAssertGreaterThan(progress.documentsLoaded, 0);
  52. XCTAssertEqual(progress.documentsLoaded, progress.totalDocuments);
  53. }
  54. - (void)verifyErrorProgress:(FIRLoadBundleTaskProgress*)progress {
  55. XCTAssertEqual(progress.state, FIRLoadBundleTaskStateError);
  56. XCTAssertEqual(progress.bytesLoaded, 0);
  57. XCTAssertEqual(progress.documentsLoaded, 0);
  58. }
  59. - (std::string)defaultBundle {
  60. return CreateBundle(MakeString([FSTIntegrationTestCase projectID]));
  61. }
  62. - (std::string)bundleForProject:(NSString*)projectID {
  63. return CreateBundle(MakeString(projectID));
  64. }
  65. - (void)verifyQueryResults {
  66. FIRCollectionReference* query = [self.db collectionWithPath:@"coll-1"];
  67. FIRQuerySnapshot* snapshot = [self readDocumentSetForRef:query source:FIRFirestoreSourceCache];
  68. NSArray* expected = @[ @{@"bar" : @1L, @"k" : @"a"}, @{@"bar" : @2L, @"k" : @"b"} ];
  69. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  70. [self verifyNamedQuery:@"limit" hasResult:@[ @{@"bar" : @2L, @"k" : @"b"} ]];
  71. [self verifyNamedQuery:@"limit-to-last" hasResult:@[ @{@"bar" : @1L, @"k" : @"a"} ]];
  72. }
  73. - (void)verifyNamedQuery:(NSString*)name hasResult:(NSArray*)expected {
  74. XCTestExpectation* expectation = [self expectationWithDescription:@"namedQuery"];
  75. __block FIRQuery* query;
  76. [self.db getQueryNamed:name
  77. completion:^(FIRQuery* q) {
  78. query = q;
  79. [expectation fulfill];
  80. }];
  81. [self awaitExpectation:expectation];
  82. FIRQuerySnapshot* snapshot = [self readDocumentSetForRef:query source:FIRFirestoreSourceCache];
  83. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  84. }
  85. - (void)testLoadWithDocumentsThatAreAlreadyPulledFromBackend {
  86. [self writeDocumentRef:[self.db documentWithPath:@"coll-1/a"] data:@{@"bar" : @"newValueA"}];
  87. [self writeDocumentRef:[self.db documentWithPath:@"coll-1/b"] data:@{@"bar" : @"newValueB"}];
  88. // Finishing receiving backend event.
  89. FIRCollectionReference* collection = [self.db collectionWithPath:@"coll-1"];
  90. id<FIRListenerRegistration> registration =
  91. [collection addSnapshotListener:self.eventAccumulator.valueEventHandler];
  92. [self.eventAccumulator awaitRemoteEvent];
  93. // We should see no more snapshots from loading the bundle, because the data there is older.
  94. [self.eventAccumulator assertNoAdditionalEvents];
  95. auto bundle = [self defaultBundle];
  96. NSMutableArray* progresses = [[NSMutableArray alloc] init];
  97. __block FIRLoadBundleTaskProgress* result;
  98. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  99. FIRLoadBundleTask* task =
  100. [self.db loadBundle:[MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  101. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  102. result = progress;
  103. XCTAssertNil(error);
  104. [expectation fulfill];
  105. }];
  106. [task addObserver:^(FIRLoadBundleTaskProgress* progress) {
  107. [progresses addObject:progress];
  108. }];
  109. [self awaitExpectation:expectation];
  110. XCTAssertEqual(4ul, progresses.count);
  111. [self verifyProgress:progresses[0] hasLoadedDocument:0];
  112. [self verifyProgress:progresses[1] hasLoadedDocument:1];
  113. [self verifyProgress:progresses[2] hasLoadedDocument:2];
  114. [self verifySuccessProgress:progresses[3]];
  115. XCTAssertEqualObjects(progresses[3], result);
  116. [self verifyNamedQuery:@"limit" hasResult:@[ @{@"bar" : @"newValueB"} ]];
  117. [self verifyNamedQuery:@"limit-to-last" hasResult:@[ @{@"bar" : @"newValueA"} ]];
  118. [registration remove];
  119. }
  120. - (void)testLoadDocumentsWithProgressUpdates {
  121. NSMutableArray* progresses = [[NSMutableArray alloc] init];
  122. auto bundle = [self defaultBundle];
  123. __block FIRLoadBundleTaskProgress* result;
  124. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  125. FIRLoadBundleTask* task =
  126. [self.db loadBundle:[MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  127. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  128. result = progress;
  129. XCTAssertNil(error);
  130. [expectation fulfill];
  131. }];
  132. [task addObserver:^(FIRLoadBundleTaskProgress* progress) {
  133. [progresses addObject:progress];
  134. }];
  135. [self awaitExpectation:expectation];
  136. XCTAssertEqual(4ul, progresses.count);
  137. [self verifyProgress:progresses[0] hasLoadedDocument:0];
  138. [self verifyProgress:progresses[1] hasLoadedDocument:1];
  139. [self verifyProgress:progresses[2] hasLoadedDocument:2];
  140. [self verifySuccessProgress:progresses[3]];
  141. XCTAssertEqualObjects(progresses[3], result);
  142. [self verifyQueryResults];
  143. }
  144. - (void)testLoadForASecondTimeSkips {
  145. auto bundle = [self defaultBundle];
  146. [self.db loadBundle:[MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]];
  147. // Load for a second time
  148. NSMutableArray* progresses = [[NSMutableArray alloc] init];
  149. __block FIRLoadBundleTaskProgress* result;
  150. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  151. FIRLoadBundleTask* task =
  152. [self.db loadBundle:[MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  153. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  154. result = progress;
  155. XCTAssertNil(error);
  156. [expectation fulfill];
  157. }];
  158. [task addObserver:^(FIRLoadBundleTaskProgress* progress) {
  159. [progresses addObject:progress];
  160. }];
  161. [self awaitExpectation:expectation];
  162. XCTAssertEqual(1ul, progresses.count);
  163. [self verifySuccessProgress:progresses[0]];
  164. XCTAssertEqualObjects(progresses[0], result);
  165. [self verifyQueryResults];
  166. }
  167. - (void)testLoadedDocumentsShouldNotBeGarbageCollectedRightAway {
  168. auto settings = [self.db settings];
  169. [settings setCacheSettings:[[FIRMemoryCacheSettings alloc] init]];
  170. [self.db setSettings:settings];
  171. auto bundle = [self defaultBundle];
  172. __block FIRLoadBundleTaskProgress* result;
  173. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  174. [self.db loadBundle:[MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  175. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  176. result = progress;
  177. XCTAssertNil(error);
  178. [expectation fulfill];
  179. }];
  180. [self awaitExpectation:expectation];
  181. [self verifySuccessProgress:result];
  182. // Read a different collection. This will trigger GC.
  183. [self readDocumentSetForRef:[self.db collectionWithPath:@"coll-other"]];
  184. // Read the loaded documents, expecting them to exist in cache. With memory GC, the documents
  185. // would get GC-ed if we did not hold the document keys in an "umbrella" target. See
  186. // LocalStore for details.
  187. [self verifyQueryResults];
  188. }
  189. - (void)testLoadBundlesFromOtherProjectFails {
  190. NSMutableArray* progresses = [[NSMutableArray alloc] init];
  191. __block FIRLoadBundleTaskProgress* result;
  192. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  193. auto bundle = [self bundleForProject:@"OtherProject"];
  194. FIRLoadBundleTask* task =
  195. [self.db loadBundle:[MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  196. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  197. result = progress;
  198. XCTAssertNotNil(error);
  199. [expectation fulfill];
  200. }];
  201. [task addObserver:^(FIRLoadBundleTaskProgress* progress) {
  202. [progresses addObject:progress];
  203. }];
  204. [self awaitExpectation:expectation];
  205. XCTAssertEqual(2ul, progresses.count);
  206. [self verifyProgress:progresses[0] hasLoadedDocument:0];
  207. [self verifyErrorProgress:progresses[1]];
  208. XCTAssertEqualObjects(progresses[1], result);
  209. }
  210. @end