FIRWriteBatchTests.mm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 <FirebaseFirestore/FirebaseFirestore.h>
  17. #import <XCTest/XCTest.h>
  18. #include <mach/mach.h>
  19. #include <cstdint>
  20. #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
  21. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  22. @interface FIRWriteBatchTests : FSTIntegrationTestCase
  23. @end
  24. @implementation FIRWriteBatchTests
  25. - (void)testSupportEmptyBatches {
  26. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  27. [[[self firestore] batch] commitWithCompletion:^(NSError *error) {
  28. XCTAssertNil(error);
  29. [expectation fulfill];
  30. }];
  31. [self awaitExpectations];
  32. }
  33. - (void)testCommitWithoutCompletionHandler {
  34. FIRDocumentReference *doc = [self documentRef];
  35. FIRWriteBatch *batch1 = [doc.firestore batch];
  36. [batch1 setData:@{@"aa" : @"bb"} forDocument:doc];
  37. [batch1 commitWithCompletion:nil];
  38. FIRDocumentSnapshot *snapshot1 = [self readDocumentForRef:doc];
  39. XCTAssertTrue(snapshot1.exists);
  40. XCTAssertEqualObjects(snapshot1.data, @{@"aa" : @"bb"});
  41. FIRWriteBatch *batch2 = [doc.firestore batch];
  42. [batch2 setData:@{@"cc" : @"dd"} forDocument:doc];
  43. [batch2 commit];
  44. // TODO(b/70631617): There's currently a backend bug that prevents us from using a resume token
  45. // right away (against hexa at least). So we sleep. :-( :-( Anything over ~10ms seems to be
  46. // sufficient.
  47. [NSThread sleepForTimeInterval:0.2f];
  48. FIRDocumentSnapshot *snapshot2 = [self readDocumentForRef:doc];
  49. XCTAssertTrue(snapshot2.exists);
  50. XCTAssertEqualObjects(snapshot2.data, @{@"cc" : @"dd"});
  51. }
  52. - (void)testSetDocuments {
  53. FIRDocumentReference *doc = [self documentRef];
  54. XCTestExpectation *batchExpectation = [self expectationWithDescription:@"batch written"];
  55. FIRWriteBatch *batch = [doc.firestore batch];
  56. [batch setData:@{@"a" : @"b"} forDocument:doc];
  57. [batch setData:@{@"c" : @"d"} forDocument:doc];
  58. [batch commitWithCompletion:^(NSError *error) {
  59. XCTAssertNil(error);
  60. [batchExpectation fulfill];
  61. }];
  62. [self awaitExpectations];
  63. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  64. XCTAssertTrue(snapshot.exists);
  65. XCTAssertEqualObjects(snapshot.data, @{@"c" : @"d"});
  66. }
  67. - (void)testSetDocumentWithMerge {
  68. FIRDocumentReference *doc = [self documentRef];
  69. XCTestExpectation *batchExpectation = [self expectationWithDescription:@"batch written"];
  70. FIRWriteBatch *batch = [doc.firestore batch];
  71. [batch setData:@{@"a" : @"b", @"nested" : @{@"a" : @"b"}} forDocument:doc];
  72. [batch setData:@{@"c" : @"d", @"nested" : @{@"c" : @"d"}} forDocument:doc merge:YES];
  73. [batch commitWithCompletion:^(NSError *error) {
  74. XCTAssertNil(error);
  75. [batchExpectation fulfill];
  76. }];
  77. [self awaitExpectations];
  78. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  79. XCTAssertTrue(snapshot.exists);
  80. XCTAssertEqualObjects(snapshot.data,
  81. (@{@"a" : @"b", @"c" : @"d", @"nested" : @{@"a" : @"b", @"c" : @"d"}}));
  82. }
  83. - (void)testUpdateDocuments {
  84. FIRDocumentReference *doc = [self documentRef];
  85. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  86. XCTestExpectation *batchExpectation = [self expectationWithDescription:@"batch written"];
  87. FIRWriteBatch *batch = [doc.firestore batch];
  88. [batch updateData:@{@"baz" : @42} forDocument:doc];
  89. [batch commitWithCompletion:^(NSError *error) {
  90. XCTAssertNil(error);
  91. [batchExpectation fulfill];
  92. }];
  93. [self awaitExpectations];
  94. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  95. XCTAssertTrue(snapshot.exists);
  96. XCTAssertEqualObjects(snapshot.data, (@{@"foo" : @"bar", @"baz" : @42}));
  97. }
  98. - (void)testCannotUpdateNonexistentDocuments {
  99. FIRDocumentReference *doc = [self documentRef];
  100. XCTestExpectation *batchExpectation = [self expectationWithDescription:@"batch written"];
  101. FIRWriteBatch *batch = [doc.firestore batch];
  102. [batch updateData:@{@"baz" : @42} forDocument:doc];
  103. [batch commitWithCompletion:^(NSError *error) {
  104. XCTAssertNotNil(error);
  105. [batchExpectation fulfill];
  106. }];
  107. [self awaitExpectations];
  108. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  109. XCTAssertFalse(result.exists);
  110. }
  111. - (void)testDeleteDocuments {
  112. FIRDocumentReference *doc = [self documentRef];
  113. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  114. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  115. XCTAssertTrue(snapshot.exists);
  116. XCTestExpectation *batchExpectation = [self expectationWithDescription:@"batch written"];
  117. FIRWriteBatch *batch = [doc.firestore batch];
  118. [batch deleteDocument:doc];
  119. [batch commitWithCompletion:^(NSError *error) {
  120. XCTAssertNil(error);
  121. [batchExpectation fulfill];
  122. }];
  123. [self awaitExpectations];
  124. snapshot = [self readDocumentForRef:doc];
  125. XCTAssertFalse(snapshot.exists);
  126. }
  127. - (void)testBatchesCommitAtomicallyRaisingCorrectEvents {
  128. FIRCollectionReference *collection = [self collectionRef];
  129. FIRDocumentReference *docA = [collection documentWithPath:@"a"];
  130. FIRDocumentReference *docB = [collection documentWithPath:@"b"];
  131. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  132. [collection addSnapshotListenerWithIncludeMetadataChanges:YES
  133. listener:accumulator.valueEventHandler];
  134. FIRQuerySnapshot *initialSnap = [accumulator awaitEventWithName:@"initial event"];
  135. XCTAssertEqual(initialSnap.count, 0);
  136. // Atomically write two documents.
  137. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  138. FIRWriteBatch *batch = [collection.firestore batch];
  139. [batch setData:@{@"a" : @1} forDocument:docA];
  140. [batch setData:@{@"b" : @2} forDocument:docB];
  141. [batch commitWithCompletion:^(NSError *_Nullable error) {
  142. XCTAssertNil(error);
  143. [expectation fulfill];
  144. }];
  145. FIRQuerySnapshot *localSnap = [accumulator awaitEventWithName:@"local event"];
  146. XCTAssertTrue(localSnap.metadata.hasPendingWrites);
  147. XCTAssertEqualObjects(FIRQuerySnapshotGetData(localSnap), (@[ @{@"a" : @1}, @{@"b" : @2} ]));
  148. FIRQuerySnapshot *serverSnap = [accumulator awaitEventWithName:@"server event"];
  149. XCTAssertFalse(serverSnap.metadata.hasPendingWrites);
  150. XCTAssertEqualObjects(FIRQuerySnapshotGetData(serverSnap), (@[ @{@"a" : @1}, @{@"b" : @2} ]));
  151. }
  152. - (void)testBatchesFailAtomicallyRaisingCorrectEvents {
  153. FIRCollectionReference *collection = [self collectionRef];
  154. FIRDocumentReference *docA = [collection documentWithPath:@"a"];
  155. FIRDocumentReference *docB = [collection documentWithPath:@"b"];
  156. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  157. [collection addSnapshotListenerWithIncludeMetadataChanges:YES
  158. listener:accumulator.valueEventHandler];
  159. FIRQuerySnapshot *initialSnap = [accumulator awaitEventWithName:@"initial event"];
  160. XCTAssertEqual(initialSnap.count, 0);
  161. // Atomically write 1 document and update a nonexistent document.
  162. XCTestExpectation *expectation = [self expectationWithDescription:@"batch failed"];
  163. FIRWriteBatch *batch = [collection.firestore batch];
  164. [batch setData:@{@"a" : @1} forDocument:docA];
  165. [batch updateData:@{@"b" : @2} forDocument:docB];
  166. [batch commitWithCompletion:^(NSError *_Nullable error) {
  167. XCTAssertNotNil(error);
  168. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  169. XCTAssertEqual(error.code, FIRFirestoreErrorCodeNotFound);
  170. [expectation fulfill];
  171. }];
  172. // Local event with the set document.
  173. FIRQuerySnapshot *localSnap = [accumulator awaitEventWithName:@"local event"];
  174. XCTAssertTrue(localSnap.metadata.hasPendingWrites);
  175. XCTAssertEqualObjects(FIRQuerySnapshotGetData(localSnap), (@[ @{@"a" : @1} ]));
  176. // Server event with the set reverted.
  177. FIRQuerySnapshot *serverSnap = [accumulator awaitEventWithName:@"server event"];
  178. XCTAssertFalse(serverSnap.metadata.hasPendingWrites);
  179. XCTAssertEqual(serverSnap.count, 0);
  180. }
  181. - (void)testWriteTheSameServerTimestampAcrossWrites {
  182. FIRCollectionReference *collection = [self collectionRef];
  183. FIRDocumentReference *docA = [collection documentWithPath:@"a"];
  184. FIRDocumentReference *docB = [collection documentWithPath:@"b"];
  185. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  186. [collection addSnapshotListenerWithIncludeMetadataChanges:YES
  187. listener:accumulator.valueEventHandler];
  188. FIRQuerySnapshot *initialSnap = [accumulator awaitEventWithName:@"initial event"];
  189. XCTAssertEqual(initialSnap.count, 0);
  190. // Atomically write 2 documents with server timestamps.
  191. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  192. FIRWriteBatch *batch = [collection.firestore batch];
  193. [batch setData:@{@"when" : [FIRFieldValue fieldValueForServerTimestamp]} forDocument:docA];
  194. [batch setData:@{@"when" : [FIRFieldValue fieldValueForServerTimestamp]} forDocument:docB];
  195. [batch commitWithCompletion:^(NSError *_Nullable error) {
  196. XCTAssertNil(error);
  197. [expectation fulfill];
  198. }];
  199. FIRQuerySnapshot *localSnap = [accumulator awaitEventWithName:@"local event"];
  200. XCTAssertTrue(localSnap.metadata.hasPendingWrites);
  201. XCTAssertEqualObjects(FIRQuerySnapshotGetData(localSnap),
  202. (@[ @{@"when" : [NSNull null]}, @{@"when" : [NSNull null]} ]));
  203. FIRQuerySnapshot *serverSnap = [accumulator awaitEventWithName:@"server event"];
  204. XCTAssertFalse(serverSnap.metadata.hasPendingWrites);
  205. XCTAssertEqual(serverSnap.count, 2);
  206. NSDate *when = serverSnap.documents[0][@"when"];
  207. XCTAssertEqualObjects(FIRQuerySnapshotGetData(serverSnap),
  208. (@[ @{@"when" : when}, @{@"when" : when} ]));
  209. }
  210. - (void)testCanWriteTheSameDocumentMultipleTimes {
  211. FIRDocumentReference *doc = [self documentRef];
  212. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  213. [doc addSnapshotListenerWithIncludeMetadataChanges:YES listener:accumulator.valueEventHandler];
  214. FIRDocumentSnapshot *initialSnap = [accumulator awaitEventWithName:@"initial event"];
  215. XCTAssertFalse(initialSnap.exists);
  216. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  217. FIRWriteBatch *batch = [doc.firestore batch];
  218. [batch deleteDocument:doc];
  219. [batch setData:@{@"a" : @1, @"b" : @1, @"when" : @"when"} forDocument:doc];
  220. [batch updateData:@{@"b" : @2, @"when" : [FIRFieldValue fieldValueForServerTimestamp]}
  221. forDocument:doc];
  222. [batch commitWithCompletion:^(NSError *_Nullable error) {
  223. XCTAssertNil(error);
  224. [expectation fulfill];
  225. }];
  226. FIRDocumentSnapshot *localSnap = [accumulator awaitEventWithName:@"local event"];
  227. XCTAssertTrue(localSnap.metadata.hasPendingWrites);
  228. XCTAssertEqualObjects(localSnap.data, (@{@"a" : @1, @"b" : @2, @"when" : [NSNull null]}));
  229. FIRDocumentSnapshot *serverSnap = [accumulator awaitEventWithName:@"server event"];
  230. XCTAssertFalse(serverSnap.metadata.hasPendingWrites);
  231. NSDate *when = serverSnap[@"when"];
  232. XCTAssertEqualObjects(serverSnap.data, (@{@"a" : @1, @"b" : @2, @"when" : when}));
  233. }
  234. - (void)testUpdateFieldsWithDots {
  235. FIRDocumentReference *doc = [self documentRef];
  236. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateFieldsWithDots"];
  237. FIRWriteBatch *batch = [doc.firestore batch];
  238. [batch setData:@{@"a.b" : @"old", @"c.d" : @"old"} forDocument:doc];
  239. [batch updateData:@{[[FIRFieldPath alloc] initWithFields:@[ @"a.b" ]] : @"new"} forDocument:doc];
  240. [batch commitWithCompletion:^(NSError *_Nullable error) {
  241. XCTAssertNil(error);
  242. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  243. XCTAssertNil(error);
  244. XCTAssertEqualObjects(snapshot.data, (@{@"a.b" : @"new", @"c.d" : @"old"}));
  245. }];
  246. [expectation fulfill];
  247. }];
  248. [self awaitExpectations];
  249. }
  250. - (void)testUpdateNestedFields {
  251. FIRDocumentReference *doc = [self documentRef];
  252. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateNestedFields"];
  253. FIRWriteBatch *batch = [doc.firestore batch];
  254. [batch setData:@{@"a" : @{@"b" : @"old"}, @"c" : @{@"d" : @"old"}, @"e" : @{@"f" : @"old"}}
  255. forDocument:doc];
  256. [batch
  257. updateData:@{@"a.b" : @"new", [[FIRFieldPath alloc] initWithFields:@[ @"c", @"d" ]] : @"new"}
  258. forDocument:doc];
  259. [batch commitWithCompletion:^(NSError *_Nullable error) {
  260. XCTAssertNil(error);
  261. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  262. XCTAssertNil(error);
  263. XCTAssertEqualObjects(snapshot.data, (@{
  264. @"a" : @{@"b" : @"new"},
  265. @"c" : @{@"d" : @"new"},
  266. @"e" : @{@"f" : @"old"}
  267. }));
  268. }];
  269. [expectation fulfill];
  270. }];
  271. [self awaitExpectations];
  272. }
  273. // Returns how much memory the test application is currently using, in megabytes (fractional part is
  274. // truncated), or -1 if the OS call fails.
  275. // TODO(varconst): move the helper function and the test into a new test target for performance
  276. // testing.
  277. int64_t GetCurrentMemoryUsedInMb() {
  278. mach_task_basic_info taskInfo;
  279. mach_msg_type_number_t taskInfoSize = MACH_TASK_BASIC_INFO_COUNT;
  280. const auto errorCode =
  281. task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&taskInfo, &taskInfoSize);
  282. if (errorCode == KERN_SUCCESS) {
  283. const int bytesInMegabyte = 1024 * 1024;
  284. return taskInfo.resident_size / bytesInMegabyte;
  285. }
  286. return -1;
  287. }
  288. - (void)testReasonableMemoryUsageForLotsOfMutations {
  289. XCTestExpectation *expectation =
  290. [self expectationWithDescription:@"testReasonableMemoryUsageForLotsOfMutations"];
  291. FIRDocumentReference *mainDoc = [self documentRef];
  292. FIRWriteBatch *batch = [mainDoc.firestore batch];
  293. // > 500 mutations will be rejected.
  294. const int maxMutations = 500;
  295. for (int i = 0; i != maxMutations; ++i) {
  296. FIRDocumentReference *nestedDoc = [[mainDoc collectionWithPath:@"nested"] documentWithAutoID];
  297. // The exact data doesn't matter; what is important is the large number of mutations.
  298. [batch setData:@{
  299. @"a" : @"foo",
  300. @"b" : @"bar",
  301. }
  302. forDocument:nestedDoc];
  303. }
  304. const int64_t memoryUsedBeforeCommitMb = GetCurrentMemoryUsedInMb();
  305. XCTAssertNotEqual(memoryUsedBeforeCommitMb, -1);
  306. [batch commitWithCompletion:^(NSError *_Nullable error) {
  307. XCTAssertNil(error);
  308. const int64_t memoryUsedAfterCommitMb = GetCurrentMemoryUsedInMb();
  309. XCTAssertNotEqual(memoryUsedAfterCommitMb, -1);
  310. const int64_t memoryDeltaMb = memoryUsedAfterCommitMb - memoryUsedBeforeCommitMb;
  311. // This by its nature cannot be a precise value. Runs on simulator seem to give an increase of
  312. // 10MB in debug mode pretty consistently. A regression would be on the scale of 500Mb.
  313. XCTAssertLessThan(memoryDeltaMb, 20);
  314. [expectation fulfill];
  315. }];
  316. [self awaitExpectations];
  317. }
  318. @end