FIRBundlesTests.mm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. MakeString([FSTIntegrationTestCase databaseID]));
  62. }
  63. - (std::string)bundleForProject:(NSString*)projectID {
  64. return CreateBundle(MakeString(projectID), MakeString([FSTIntegrationTestCase databaseID]));
  65. }
  66. - (std::string)bundleForDatabase:(NSString*)databaseID {
  67. return CreateBundle(MakeString([FSTIntegrationTestCase projectID]), MakeString(databaseID));
  68. }
  69. - (void)verifyQueryResults {
  70. FIRCollectionReference* query = [self.db collectionWithPath:@"coll-1"];
  71. FIRQuerySnapshot* snapshot = [self readDocumentSetForRef:query source:FIRFirestoreSourceCache];
  72. NSArray* expected = @[ @{@"bar" : @1L, @"k" : @"a"}, @{@"bar" : @2L, @"k" : @"b"} ];
  73. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  74. [self verifyNamedQuery:@"limit" hasResult:@[ @{@"bar" : @2L, @"k" : @"b"} ]];
  75. [self verifyNamedQuery:@"limit-to-last" hasResult:@[ @{@"bar" : @1L, @"k" : @"a"} ]];
  76. }
  77. - (void)verifyNamedQuery:(NSString*)name hasResult:(NSArray*)expected {
  78. XCTestExpectation* expectation = [self expectationWithDescription:@"namedQuery"];
  79. __block FIRQuery* query;
  80. [self.db getQueryNamed:name
  81. completion:^(FIRQuery* q) {
  82. query = q;
  83. [expectation fulfill];
  84. }];
  85. [self awaitExpectation:expectation];
  86. FIRQuerySnapshot* snapshot = [self readDocumentSetForRef:query source:FIRFirestoreSourceCache];
  87. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
  88. }
  89. - (void)testLoadWithDocumentsThatAreAlreadyPulledFromBackend {
  90. [self writeDocumentRef:[self.db documentWithPath:@"coll-1/a"] data:@{@"bar" : @"newValueA"}];
  91. [self writeDocumentRef:[self.db documentWithPath:@"coll-1/b"] data:@{@"bar" : @"newValueB"}];
  92. // Finishing receiving backend event.
  93. FIRCollectionReference* collection = [self.db collectionWithPath:@"coll-1"];
  94. id<FIRListenerRegistration> registration =
  95. [collection addSnapshotListener:self.eventAccumulator.valueEventHandler];
  96. [self.eventAccumulator awaitRemoteEvent];
  97. // We should see no more snapshots from loading the bundle, because the data there is older.
  98. [self.eventAccumulator assertNoAdditionalEvents];
  99. auto bundle = [self defaultBundle];
  100. NSMutableArray* progresses = [[NSMutableArray alloc] init];
  101. __block FIRLoadBundleTaskProgress* result;
  102. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  103. FIRLoadBundleTask* task =
  104. [self.db loadBundle:[MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  105. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  106. result = progress;
  107. XCTAssertNil(error);
  108. [expectation fulfill];
  109. }];
  110. [task addObserver:^(FIRLoadBundleTaskProgress* progress) {
  111. [progresses addObject:progress];
  112. }];
  113. [self awaitExpectation:expectation];
  114. XCTAssertEqual(4ul, progresses.count);
  115. [self verifyProgress:progresses[0] hasLoadedDocument:0];
  116. [self verifyProgress:progresses[1] hasLoadedDocument:1];
  117. [self verifyProgress:progresses[2] hasLoadedDocument:2];
  118. [self verifySuccessProgress:progresses[3]];
  119. XCTAssertEqualObjects(progresses[3], result);
  120. [self verifyNamedQuery:@"limit" hasResult:@[ @{@"bar" : @"newValueB"} ]];
  121. [self verifyNamedQuery:@"limit-to-last" hasResult:@[ @{@"bar" : @"newValueA"} ]];
  122. [registration remove];
  123. }
  124. - (void)testLoadDocumentsWithProgressUpdates {
  125. NSMutableArray* progresses = [[NSMutableArray alloc] init];
  126. auto bundle = [self defaultBundle];
  127. __block FIRLoadBundleTaskProgress* result;
  128. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  129. FIRLoadBundleTask* task =
  130. [self.db loadBundle:[MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  131. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  132. result = progress;
  133. XCTAssertNil(error);
  134. [expectation fulfill];
  135. }];
  136. [task addObserver:^(FIRLoadBundleTaskProgress* progress) {
  137. [progresses addObject:progress];
  138. }];
  139. [self awaitExpectation:expectation];
  140. XCTAssertEqual(4ul, progresses.count);
  141. [self verifyProgress:progresses[0] hasLoadedDocument:0];
  142. [self verifyProgress:progresses[1] hasLoadedDocument:1];
  143. [self verifyProgress:progresses[2] hasLoadedDocument:2];
  144. [self verifySuccessProgress:progresses[3]];
  145. XCTAssertEqualObjects(progresses[3], result);
  146. [self verifyQueryResults];
  147. }
  148. - (void)testLoadForASecondTimeSkips {
  149. auto bundle = [self defaultBundle];
  150. [self.db loadBundle:[MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]];
  151. // Load for a second time
  152. NSMutableArray* progresses = [[NSMutableArray alloc] init];
  153. __block FIRLoadBundleTaskProgress* result;
  154. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  155. FIRLoadBundleTask* task =
  156. [self.db loadBundle:[MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  157. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  158. result = progress;
  159. XCTAssertNil(error);
  160. [expectation fulfill];
  161. }];
  162. [task addObserver:^(FIRLoadBundleTaskProgress* progress) {
  163. [progresses addObject:progress];
  164. }];
  165. [self awaitExpectation:expectation];
  166. XCTAssertEqual(1ul, progresses.count);
  167. [self verifySuccessProgress:progresses[0]];
  168. XCTAssertEqualObjects(progresses[0], result);
  169. [self verifyQueryResults];
  170. }
  171. - (void)testLoadedDocumentsShouldNotBeGarbageCollectedRightAway {
  172. auto settings = [self.db settings];
  173. [settings setCacheSettings:[[FIRMemoryCacheSettings alloc] init]];
  174. [self.db setSettings:settings];
  175. auto bundle = [self defaultBundle];
  176. __block FIRLoadBundleTaskProgress* result;
  177. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  178. [self.db loadBundle:[MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  179. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  180. result = progress;
  181. XCTAssertNil(error);
  182. [expectation fulfill];
  183. }];
  184. [self awaitExpectation:expectation];
  185. [self verifySuccessProgress:result];
  186. // Read a different collection. This will trigger GC.
  187. [self readDocumentSetForRef:[self.db collectionWithPath:@"coll-other"]];
  188. // Read the loaded documents, expecting them to exist in cache. With memory GC, the documents
  189. // would get GC-ed if we did not hold the document keys in an "umbrella" target. See
  190. // LocalStore for details.
  191. [self verifyQueryResults];
  192. }
  193. - (void)testLoadBundlesFromOtherProjectFails {
  194. NSMutableArray* progresses = [[NSMutableArray alloc] init];
  195. __block FIRLoadBundleTaskProgress* result;
  196. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  197. auto bundle = [self bundleForProject:@"OtherProject"];
  198. FIRLoadBundleTask* task =
  199. [self.db loadBundle:[MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  200. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  201. result = progress;
  202. XCTAssertNotNil(error);
  203. [expectation fulfill];
  204. }];
  205. [task addObserver:^(FIRLoadBundleTaskProgress* progress) {
  206. [progresses addObject:progress];
  207. }];
  208. [self awaitExpectation:expectation];
  209. XCTAssertEqual(2ul, progresses.count);
  210. [self verifyProgress:progresses[0] hasLoadedDocument:0];
  211. [self verifyErrorProgress:progresses[1]];
  212. XCTAssertEqualObjects(progresses[1], result);
  213. }
  214. - (void)testLoadBundlesFromOtherDatabaseFails {
  215. NSMutableArray* progresses = [[NSMutableArray alloc] init];
  216. __block FIRLoadBundleTaskProgress* result;
  217. XCTestExpectation* expectation = [self expectationWithDescription:@"loading complete"];
  218. auto bundle = [self bundleForDatabase:@"other-database"];
  219. FIRLoadBundleTask* task =
  220. [self.db loadBundle:[MakeNSString(bundle) dataUsingEncoding:NSUTF8StringEncoding]
  221. completion:^(FIRLoadBundleTaskProgress* progress, NSError* error) {
  222. result = progress;
  223. XCTAssertNotNil(error);
  224. [expectation fulfill];
  225. }];
  226. [task addObserver:^(FIRLoadBundleTaskProgress* progress) {
  227. [progresses addObject:progress];
  228. }];
  229. [self awaitExpectation:expectation];
  230. XCTAssertEqual(2ul, progresses.count);
  231. [self verifyProgress:progresses[0] hasLoadedDocument:0];
  232. [self verifyErrorProgress:progresses[1]];
  233. XCTAssertEqualObjects(progresses[1], result);
  234. }
  235. @end