FIRWriteBatchTests.mm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. (
  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:@{[[FIRFieldPath alloc] initWithFields:@[ @"a.b" ]] : @"new"} forDocument:doc];
  246. [batch commitWithCompletion:^(NSError *_Nullable error) {
  247. XCTAssertNil(error);
  248. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  249. XCTAssertNil(error);
  250. XCTAssertEqualObjects(snapshot.data, (@{@"a.b" : @"new", @"c.d" : @"old"}));
  251. }];
  252. [expectation fulfill];
  253. }];
  254. [self awaitExpectations];
  255. }
  256. - (void)testUpdateNestedFields {
  257. FIRDocumentReference *doc = [self documentRef];
  258. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateNestedFields"];
  259. FIRWriteBatch *batch = [doc.firestore batch];
  260. [batch setData:@{
  261. @"a" : @{@"b" : @"old"},
  262. @"c" : @{@"d" : @"old"},
  263. @"e" : @{@"f" : @"old"}
  264. }
  265. forDocument:doc];
  266. [batch updateData:@{
  267. @"a.b" : @"new",
  268. [[FIRFieldPath alloc] initWithFields:@[ @"c", @"d" ]] : @"new"
  269. }
  270. forDocument:doc];
  271. [batch commitWithCompletion:^(NSError *_Nullable error) {
  272. XCTAssertNil(error);
  273. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  274. XCTAssertNil(error);
  275. XCTAssertEqualObjects(snapshot.data, (@{
  276. @"a" : @{@"b" : @"new"},
  277. @"c" : @{@"d" : @"new"},
  278. @"e" : @{@"f" : @"old"}
  279. }));
  280. }];
  281. [expectation fulfill];
  282. }];
  283. [self awaitExpectations];
  284. }
  285. // Returns how much memory the test application is currently using, in megabytes (fractional part is
  286. // truncated), or -1 if the OS call fails.
  287. // TODO(varconst): move the helper function and the test into a new test target for performance
  288. // testing.
  289. int64_t GetCurrentMemoryUsedInMb() {
  290. mach_task_basic_info taskInfo;
  291. mach_msg_type_number_t taskInfoSize = MACH_TASK_BASIC_INFO_COUNT;
  292. const auto errorCode =
  293. task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&taskInfo, &taskInfoSize);
  294. if (errorCode == KERN_SUCCESS) {
  295. const int bytesInMegabyte = 1024 * 1024;
  296. return taskInfo.resident_size / bytesInMegabyte;
  297. }
  298. return -1;
  299. }
  300. - (void)testReasonableMemoryUsageForLotsOfMutations {
  301. XCTestExpectation *expectation =
  302. [self expectationWithDescription:@"testReasonableMemoryUsageForLotsOfMutations"];
  303. FIRDocumentReference *mainDoc = [self documentRef];
  304. FIRWriteBatch *batch = [mainDoc.firestore batch];
  305. // > 500 mutations will be rejected.
  306. const int maxMutations = 500;
  307. for (int i = 0; i != maxMutations; ++i) {
  308. FIRDocumentReference *nestedDoc = [[mainDoc collectionWithPath:@"nested"] documentWithAutoID];
  309. // The exact data doesn't matter; what is important is the large number of mutations.
  310. [batch setData:@{
  311. @"a" : @"foo",
  312. @"b" : @"bar",
  313. }
  314. forDocument:nestedDoc];
  315. }
  316. const int64_t memoryUsedBeforeCommitMb = GetCurrentMemoryUsedInMb();
  317. XCTAssertNotEqual(memoryUsedBeforeCommitMb, -1);
  318. [batch commitWithCompletion:^(NSError *_Nullable error) {
  319. XCTAssertNil(error);
  320. const int64_t memoryUsedAfterCommitMb = GetCurrentMemoryUsedInMb();
  321. XCTAssertNotEqual(memoryUsedAfterCommitMb, -1);
  322. const int64_t memoryDeltaMb = memoryUsedAfterCommitMb - memoryUsedBeforeCommitMb;
  323. // This by its nature cannot be a precise value. Runs on simulator seem to give an increase of
  324. // 10MB in debug mode pretty consistently. A regression would be on the scale of 500Mb.
  325. XCTAssertLessThan(memoryDeltaMb, 20);
  326. [expectation fulfill];
  327. }];
  328. [self awaitExpectations];
  329. }
  330. @end