FIRWriteBatchTests.mm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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(
  81. snapshot.data, (
  82. @{ @"a" : @"b",
  83. @"c" : @"d",
  84. @"nested" : @{@"a" : @"b", @"c" : @"d"} }));
  85. }
  86. - (void)testUpdateDocuments {
  87. FIRDocumentReference *doc = [self documentRef];
  88. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  89. XCTestExpectation *batchExpectation = [self expectationWithDescription:@"batch written"];
  90. FIRWriteBatch *batch = [doc.firestore batch];
  91. [batch updateData:@{ @"baz" : @42 } forDocument:doc];
  92. [batch commitWithCompletion:^(NSError *error) {
  93. XCTAssertNil(error);
  94. [batchExpectation fulfill];
  95. }];
  96. [self awaitExpectations];
  97. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  98. XCTAssertTrue(snapshot.exists);
  99. XCTAssertEqualObjects(snapshot.data, (@{ @"foo" : @"bar", @"baz" : @42 }));
  100. }
  101. - (void)testCannotUpdateNonexistentDocuments {
  102. FIRDocumentReference *doc = [self documentRef];
  103. XCTestExpectation *batchExpectation = [self expectationWithDescription:@"batch written"];
  104. FIRWriteBatch *batch = [doc.firestore batch];
  105. [batch updateData:@{ @"baz" : @42 } forDocument:doc];
  106. [batch commitWithCompletion:^(NSError *error) {
  107. XCTAssertNotNil(error);
  108. [batchExpectation fulfill];
  109. }];
  110. [self awaitExpectations];
  111. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  112. XCTAssertFalse(result.exists);
  113. }
  114. - (void)testDeleteDocuments {
  115. FIRDocumentReference *doc = [self documentRef];
  116. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  117. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  118. XCTAssertTrue(snapshot.exists);
  119. XCTestExpectation *batchExpectation = [self expectationWithDescription:@"batch written"];
  120. FIRWriteBatch *batch = [doc.firestore batch];
  121. [batch deleteDocument:doc];
  122. [batch commitWithCompletion:^(NSError *error) {
  123. XCTAssertNil(error);
  124. [batchExpectation fulfill];
  125. }];
  126. [self awaitExpectations];
  127. snapshot = [self readDocumentForRef:doc];
  128. XCTAssertFalse(snapshot.exists);
  129. }
  130. - (void)testBatchesCommitAtomicallyRaisingCorrectEvents {
  131. FIRCollectionReference *collection = [self collectionRef];
  132. FIRDocumentReference *docA = [collection documentWithPath:@"a"];
  133. FIRDocumentReference *docB = [collection documentWithPath:@"b"];
  134. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  135. [collection addSnapshotListenerWithIncludeMetadataChanges:YES
  136. listener:accumulator.valueEventHandler];
  137. FIRQuerySnapshot *initialSnap = [accumulator awaitEventWithName:@"initial event"];
  138. XCTAssertEqual(initialSnap.count, 0);
  139. // Atomically write two documents.
  140. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  141. FIRWriteBatch *batch = [collection.firestore batch];
  142. [batch setData:@{ @"a" : @1 } forDocument:docA];
  143. [batch setData:@{ @"b" : @2 } forDocument:docB];
  144. [batch commitWithCompletion:^(NSError *_Nullable error) {
  145. XCTAssertNil(error);
  146. [expectation fulfill];
  147. }];
  148. FIRQuerySnapshot *localSnap = [accumulator awaitEventWithName:@"local event"];
  149. XCTAssertTrue(localSnap.metadata.hasPendingWrites);
  150. XCTAssertEqualObjects(FIRQuerySnapshotGetData(localSnap), (@[ @{ @"a" : @1 }, @{ @"b" : @2 } ]));
  151. FIRQuerySnapshot *serverSnap = [accumulator awaitEventWithName:@"server event"];
  152. XCTAssertFalse(serverSnap.metadata.hasPendingWrites);
  153. XCTAssertEqualObjects(FIRQuerySnapshotGetData(serverSnap), (@[ @{ @"a" : @1 }, @{ @"b" : @2 } ]));
  154. }
  155. - (void)testBatchesFailAtomicallyRaisingCorrectEvents {
  156. FIRCollectionReference *collection = [self collectionRef];
  157. FIRDocumentReference *docA = [collection documentWithPath:@"a"];
  158. FIRDocumentReference *docB = [collection documentWithPath:@"b"];
  159. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  160. [collection addSnapshotListenerWithIncludeMetadataChanges:YES
  161. listener:accumulator.valueEventHandler];
  162. FIRQuerySnapshot *initialSnap = [accumulator awaitEventWithName:@"initial event"];
  163. XCTAssertEqual(initialSnap.count, 0);
  164. // Atomically write 1 document and update a nonexistent document.
  165. XCTestExpectation *expectation = [self expectationWithDescription:@"batch failed"];
  166. FIRWriteBatch *batch = [collection.firestore batch];
  167. [batch setData:@{ @"a" : @1 } forDocument:docA];
  168. [batch updateData:@{ @"b" : @2 } forDocument:docB];
  169. [batch commitWithCompletion:^(NSError *_Nullable error) {
  170. XCTAssertNotNil(error);
  171. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  172. XCTAssertEqual(error.code, FIRFirestoreErrorCodeNotFound);
  173. [expectation fulfill];
  174. }];
  175. // Local event with the set document.
  176. FIRQuerySnapshot *localSnap = [accumulator awaitEventWithName:@"local event"];
  177. XCTAssertTrue(localSnap.metadata.hasPendingWrites);
  178. XCTAssertEqualObjects(FIRQuerySnapshotGetData(localSnap), (@[ @{ @"a" : @1 } ]));
  179. // Server event with the set reverted.
  180. FIRQuerySnapshot *serverSnap = [accumulator awaitEventWithName:@"server event"];
  181. XCTAssertFalse(serverSnap.metadata.hasPendingWrites);
  182. XCTAssertEqual(serverSnap.count, 0);
  183. }
  184. - (void)testWriteTheSameServerTimestampAcrossWrites {
  185. FIRCollectionReference *collection = [self collectionRef];
  186. FIRDocumentReference *docA = [collection documentWithPath:@"a"];
  187. FIRDocumentReference *docB = [collection documentWithPath:@"b"];
  188. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  189. [collection addSnapshotListenerWithIncludeMetadataChanges:YES
  190. listener:accumulator.valueEventHandler];
  191. FIRQuerySnapshot *initialSnap = [accumulator awaitEventWithName:@"initial event"];
  192. XCTAssertEqual(initialSnap.count, 0);
  193. // Atomically write 2 documents with server timestamps.
  194. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  195. FIRWriteBatch *batch = [collection.firestore batch];
  196. [batch setData:@{@"when" : [FIRFieldValue fieldValueForServerTimestamp]} forDocument:docA];
  197. [batch setData:@{@"when" : [FIRFieldValue fieldValueForServerTimestamp]} forDocument:docB];
  198. [batch commitWithCompletion:^(NSError *_Nullable error) {
  199. XCTAssertNil(error);
  200. [expectation fulfill];
  201. }];
  202. FIRQuerySnapshot *localSnap = [accumulator awaitEventWithName:@"local event"];
  203. XCTAssertTrue(localSnap.metadata.hasPendingWrites);
  204. XCTAssertEqualObjects(FIRQuerySnapshotGetData(localSnap),
  205. (@[ @{@"when" : [NSNull null]}, @{@"when" : [NSNull null]} ]));
  206. FIRQuerySnapshot *serverSnap = [accumulator awaitEventWithName:@"server event"];
  207. XCTAssertFalse(serverSnap.metadata.hasPendingWrites);
  208. XCTAssertEqual(serverSnap.count, 2);
  209. NSDate *when = serverSnap.documents[0][@"when"];
  210. XCTAssertEqualObjects(FIRQuerySnapshotGetData(serverSnap),
  211. (@[ @{@"when" : when}, @{@"when" : when} ]));
  212. }
  213. - (void)testCanWriteTheSameDocumentMultipleTimes {
  214. FIRDocumentReference *doc = [self documentRef];
  215. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  216. [doc addSnapshotListenerWithIncludeMetadataChanges:YES listener:accumulator.valueEventHandler];
  217. FIRDocumentSnapshot *initialSnap = [accumulator awaitEventWithName:@"initial event"];
  218. XCTAssertFalse(initialSnap.exists);
  219. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  220. FIRWriteBatch *batch = [doc.firestore batch];
  221. [batch deleteDocument:doc];
  222. [batch setData:@{ @"a" : @1, @"b" : @1, @"when" : @"when" } forDocument:doc];
  223. [batch updateData:@{
  224. @"b" : @2,
  225. @"when" : [FIRFieldValue fieldValueForServerTimestamp]
  226. }
  227. forDocument:doc];
  228. [batch commitWithCompletion:^(NSError *_Nullable error) {
  229. XCTAssertNil(error);
  230. [expectation fulfill];
  231. }];
  232. FIRDocumentSnapshot *localSnap = [accumulator awaitEventWithName:@"local event"];
  233. XCTAssertTrue(localSnap.metadata.hasPendingWrites);
  234. XCTAssertEqualObjects(localSnap.data, (@{ @"a" : @1, @"b" : @2, @"when" : [NSNull null] }));
  235. FIRDocumentSnapshot *serverSnap = [accumulator awaitEventWithName:@"server event"];
  236. XCTAssertFalse(serverSnap.metadata.hasPendingWrites);
  237. NSDate *when = serverSnap[@"when"];
  238. XCTAssertEqualObjects(serverSnap.data, (@{ @"a" : @1, @"b" : @2, @"when" : when }));
  239. }
  240. - (void)testUpdateFieldsWithDots {
  241. FIRDocumentReference *doc = [self documentRef];
  242. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateFieldsWithDots"];
  243. FIRWriteBatch *batch = [doc.firestore batch];
  244. [batch setData:@{@"a.b" : @"old", @"c.d" : @"old"} forDocument:doc];
  245. [batch updateData:@{
  246. [[FIRFieldPath alloc] initWithFields:@[ @"a.b" ]] : @"new"
  247. }
  248. forDocument:doc];
  249. [batch commitWithCompletion:^(NSError *_Nullable error) {
  250. XCTAssertNil(error);
  251. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  252. XCTAssertNil(error);
  253. XCTAssertEqualObjects(snapshot.data, (@{@"a.b" : @"new", @"c.d" : @"old"}));
  254. }];
  255. [expectation fulfill];
  256. }];
  257. [self awaitExpectations];
  258. }
  259. - (void)testUpdateNestedFields {
  260. FIRDocumentReference *doc = [self documentRef];
  261. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateNestedFields"];
  262. FIRWriteBatch *batch = [doc.firestore batch];
  263. [batch setData:@{
  264. @"a" : @{@"b" : @"old"},
  265. @"c" : @{@"d" : @"old"},
  266. @"e" : @{@"f" : @"old"}
  267. }
  268. forDocument:doc];
  269. [batch updateData:@{
  270. @"a.b" : @"new",
  271. [[FIRFieldPath alloc] initWithFields:@[ @"c", @"d" ]] : @"new"
  272. }
  273. forDocument:doc];
  274. [batch commitWithCompletion:^(NSError *_Nullable error) {
  275. XCTAssertNil(error);
  276. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  277. XCTAssertNil(error);
  278. XCTAssertEqualObjects(snapshot.data, (@{
  279. @"a" : @{@"b" : @"new"},
  280. @"c" : @{@"d" : @"new"},
  281. @"e" : @{@"f" : @"old"}
  282. }));
  283. }];
  284. [expectation fulfill];
  285. }];
  286. [self awaitExpectations];
  287. }
  288. // Returns how much memory the test application is currently using, in megabytes (fractional part is
  289. // truncated), or -1 if the OS call fails.
  290. // TODO(varconst): move the helper function and the test into a new test target for performance
  291. // testing.
  292. int64_t GetCurrentMemoryUsedInMb() {
  293. mach_task_basic_info taskInfo;
  294. mach_msg_type_number_t taskInfoSize = MACH_TASK_BASIC_INFO_COUNT;
  295. const auto errorCode =
  296. task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&taskInfo, &taskInfoSize);
  297. if (errorCode == KERN_SUCCESS) {
  298. const int bytesInMegabyte = 1024 * 1024;
  299. return taskInfo.resident_size / bytesInMegabyte;
  300. }
  301. return -1;
  302. }
  303. - (void)testReasonableMemoryUsageForLotsOfMutations {
  304. XCTestExpectation *expectation =
  305. [self expectationWithDescription:@"testReasonableMemoryUsageForLotsOfMutations"];
  306. FIRDocumentReference *mainDoc = [self documentRef];
  307. FIRWriteBatch *batch = [mainDoc.firestore batch];
  308. // > 500 mutations will be rejected.
  309. const int maxMutations = 500;
  310. for (int i = 0; i != maxMutations; ++i) {
  311. FIRDocumentReference *nestedDoc = [[mainDoc collectionWithPath:@"nested"] documentWithAutoID];
  312. // The exact data doesn't matter; what is important is the large number of mutations.
  313. [batch setData:@{
  314. @"a" : @"foo",
  315. @"b" : @"bar",
  316. }
  317. forDocument:nestedDoc];
  318. }
  319. const int64_t memoryUsedBeforeCommitMb = GetCurrentMemoryUsedInMb();
  320. XCTAssertNotEqual(memoryUsedBeforeCommitMb, -1);
  321. [batch commitWithCompletion:^(NSError *_Nullable error) {
  322. XCTAssertNil(error);
  323. const int64_t memoryUsedAfterCommitMb = GetCurrentMemoryUsedInMb();
  324. XCTAssertNotEqual(memoryUsedAfterCommitMb, -1);
  325. const int64_t memoryDeltaMb = memoryUsedAfterCommitMb - memoryUsedBeforeCommitMb;
  326. // This by its nature cannot be a precise value. Runs on simulator seem to give an increase of
  327. // 10MB in debug mode pretty consistently. A regression would be on the scale of 500Mb.
  328. XCTAssertLessThan(memoryDeltaMb, 20);
  329. [expectation fulfill];
  330. }];
  331. [self awaitExpectations];
  332. }
  333. @end