FSTMutationQueueTests.mm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 "Firestore/Example/Tests/Local/FSTMutationQueueTests.h"
  17. #import <FirebaseFirestore/FIRTimestamp.h>
  18. #include <set>
  19. #import "Firestore/Source/Core/FSTQuery.h"
  20. #import "Firestore/Source/Local/FSTEagerGarbageCollector.h"
  21. #import "Firestore/Source/Local/FSTMutationQueue.h"
  22. #import "Firestore/Source/Local/FSTPersistence.h"
  23. #import "Firestore/Source/Model/FSTMutation.h"
  24. #import "Firestore/Source/Model/FSTMutationBatch.h"
  25. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  26. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  27. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  28. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  29. namespace testutil = firebase::firestore::testutil;
  30. using firebase::firestore::auth::User;
  31. using firebase::firestore::model::DocumentKey;
  32. NS_ASSUME_NONNULL_BEGIN
  33. @implementation FSTMutationQueueTests
  34. - (void)tearDown {
  35. [self.persistence shutdown];
  36. [super tearDown];
  37. }
  38. /**
  39. * Xcode will run tests from any class that extends XCTestCase, but this doesn't work for
  40. * FSTMutationQueueTests since it is incomplete without the implementations supplied by its
  41. * subclasses.
  42. */
  43. - (BOOL)isTestBaseClass {
  44. return [self class] == [FSTMutationQueueTests class];
  45. }
  46. - (void)testCountBatches {
  47. if ([self isTestBaseClass]) return;
  48. self.persistence.run("testCountBatches", [&]() {
  49. XCTAssertEqual(0, [self batchCount]);
  50. XCTAssertTrue([self.mutationQueue isEmpty]);
  51. FSTMutationBatch *batch1 = [self addMutationBatch];
  52. XCTAssertEqual(1, [self batchCount]);
  53. XCTAssertFalse([self.mutationQueue isEmpty]);
  54. FSTMutationBatch *batch2 = [self addMutationBatch];
  55. XCTAssertEqual(2, [self batchCount]);
  56. [self.mutationQueue removeMutationBatches:@[ batch2 ]];
  57. XCTAssertEqual(1, [self batchCount]);
  58. [self.mutationQueue removeMutationBatches:@[ batch1 ]];
  59. XCTAssertEqual(0, [self batchCount]);
  60. XCTAssertTrue([self.mutationQueue isEmpty]);
  61. });
  62. }
  63. - (void)testAcknowledgeBatchID {
  64. if ([self isTestBaseClass]) return;
  65. // Initial state of an empty queue
  66. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], kFSTBatchIDUnknown);
  67. // Adding mutation batches should not change the highest acked batchID.
  68. self.persistence.run("testAcknowledgeBatchID", [&]() {
  69. FSTMutationBatch *batch1 = [self addMutationBatch];
  70. FSTMutationBatch *batch2 = [self addMutationBatch];
  71. FSTMutationBatch *batch3 = [self addMutationBatch];
  72. XCTAssertGreaterThan(batch1.batchID, kFSTBatchIDUnknown);
  73. XCTAssertGreaterThan(batch2.batchID, batch1.batchID);
  74. XCTAssertGreaterThan(batch3.batchID, batch2.batchID);
  75. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], kFSTBatchIDUnknown);
  76. [self.mutationQueue acknowledgeBatch:batch1 streamToken:nil];
  77. [self.mutationQueue acknowledgeBatch:batch2 streamToken:nil];
  78. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch2.batchID);
  79. [self.mutationQueue removeMutationBatches:@[ batch1 ]];
  80. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch2.batchID);
  81. [self.mutationQueue removeMutationBatches:@[ batch2 ]];
  82. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch2.batchID);
  83. // Batch 3 never acknowledged.
  84. [self.mutationQueue removeMutationBatches:@[ batch3 ]];
  85. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch2.batchID);
  86. });
  87. }
  88. - (void)testAcknowledgeThenRemove {
  89. if ([self isTestBaseClass]) return;
  90. self.persistence.run("testAcknowledgeThenRemove", [&]() {
  91. FSTMutationBatch *batch1 = [self addMutationBatch];
  92. [self.mutationQueue acknowledgeBatch:batch1 streamToken:nil];
  93. [self.mutationQueue removeMutationBatches:@[ batch1 ]];
  94. XCTAssertEqual([self batchCount], 0);
  95. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch1.batchID);
  96. });
  97. }
  98. - (void)testHighestAcknowledgedBatchIDNeverExceedsNextBatchID {
  99. if ([self isTestBaseClass]) return;
  100. FSTMutationBatch *batch1 =
  101. self.persistence.run("testHighestAcknowledgedBatchIDNeverExceedsNextBatchID batch1",
  102. [&]() -> FSTMutationBatch * { return [self addMutationBatch]; });
  103. FSTMutationBatch *batch2 =
  104. self.persistence.run("testHighestAcknowledgedBatchIDNeverExceedsNextBatchID batch2",
  105. [&]() -> FSTMutationBatch * { return [self addMutationBatch]; });
  106. self.persistence.run("testHighestAcknowledgedBatchIDNeverExceedsNextBatchID", [&]() {
  107. [self.mutationQueue acknowledgeBatch:batch1 streamToken:nil];
  108. [self.mutationQueue acknowledgeBatch:batch2 streamToken:nil];
  109. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch2.batchID);
  110. [self.mutationQueue removeMutationBatches:@[ batch1, batch2 ]];
  111. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch2.batchID);
  112. });
  113. // Restart the queue so that nextBatchID will be reset.
  114. FSTMutationBatch *batch = self.persistence.run(
  115. "testHighestAcknowledgedBatchIDNeverExceedsNextBatchID restart", [&]() -> FSTMutationBatch * {
  116. self.mutationQueue = [self.persistence mutationQueueForUser:User("user")];
  117. [self.mutationQueue start];
  118. // Verify that on restart with an empty queue, nextBatchID falls to a lower value.
  119. XCTAssertLessThan(self.mutationQueue.nextBatchID, batch2.batchID);
  120. // As a result highestAcknowledgedBatchID must also reset lower.
  121. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], kFSTBatchIDUnknown);
  122. // The mutation queue will reset the next batchID after all mutations are removed so adding
  123. // another mutation will cause a collision.
  124. FSTMutationBatch *newBatch = [self addMutationBatch];
  125. XCTAssertEqual(newBatch.batchID, batch1.batchID);
  126. return newBatch;
  127. });
  128. self.persistence.run("testHighestAcknowledgedBatchIDNeverExceedsNextBatchID restart2", [&]() {
  129. // Restart the queue with one unacknowledged batch in it.
  130. [self.mutationQueue start];
  131. XCTAssertEqual([self.mutationQueue nextBatchID], batch.batchID + 1);
  132. // highestAcknowledgedBatchID must still be kFSTBatchIDUnknown.
  133. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], kFSTBatchIDUnknown);
  134. });
  135. }
  136. - (void)testLookupMutationBatch {
  137. if ([self isTestBaseClass]) return;
  138. // Searching on an empty queue should not find a non-existent batch
  139. self.persistence.run("testLookupMutationBatch", [&]() {
  140. FSTMutationBatch *notFound = [self.mutationQueue lookupMutationBatch:42];
  141. XCTAssertNil(notFound);
  142. NSMutableArray<FSTMutationBatch *> *batches = [self createBatches:10];
  143. NSArray<FSTMutationBatch *> *removed = [self makeHoles:@[ @2, @6, @7 ] inBatches:batches];
  144. // After removing, a batch should not be found
  145. for (NSUInteger i = 0; i < removed.count; i++) {
  146. notFound = [self.mutationQueue lookupMutationBatch:removed[i].batchID];
  147. XCTAssertNil(notFound);
  148. }
  149. // Remaining entries should still be found
  150. for (FSTMutationBatch *batch in batches) {
  151. FSTMutationBatch *found = [self.mutationQueue lookupMutationBatch:batch.batchID];
  152. XCTAssertEqual(found.batchID, batch.batchID);
  153. }
  154. // Even on a nonempty queue searching should not find a non-existent batch
  155. notFound = [self.mutationQueue lookupMutationBatch:42];
  156. XCTAssertNil(notFound);
  157. });
  158. }
  159. - (void)testNextMutationBatchAfterBatchID {
  160. if ([self isTestBaseClass]) return;
  161. self.persistence.run("testNextMutationBatchAfterBatchID", [&]() {
  162. NSMutableArray<FSTMutationBatch *> *batches = [self createBatches:10];
  163. // This is an array of successors assuming the removals below will happen:
  164. NSArray<FSTMutationBatch *> *afters = @[ batches[3], batches[8], batches[8] ];
  165. NSArray<FSTMutationBatch *> *removed = [self makeHoles:@[ @2, @6, @7 ] inBatches:batches];
  166. for (NSUInteger i = 0; i < batches.count - 1; i++) {
  167. FSTMutationBatch *current = batches[i];
  168. FSTMutationBatch *next = batches[i + 1];
  169. FSTMutationBatch *found = [self.mutationQueue nextMutationBatchAfterBatchID:current.batchID];
  170. XCTAssertEqual(found.batchID, next.batchID);
  171. }
  172. for (NSUInteger i = 0; i < removed.count; i++) {
  173. FSTMutationBatch *current = removed[i];
  174. FSTMutationBatch *next = afters[i];
  175. FSTMutationBatch *found = [self.mutationQueue nextMutationBatchAfterBatchID:current.batchID];
  176. XCTAssertEqual(found.batchID, next.batchID);
  177. }
  178. FSTMutationBatch *first = batches[0];
  179. FSTMutationBatch *found = [self.mutationQueue nextMutationBatchAfterBatchID:first.batchID - 42];
  180. XCTAssertEqual(found.batchID, first.batchID);
  181. FSTMutationBatch *last = batches[batches.count - 1];
  182. FSTMutationBatch *notFound = [self.mutationQueue nextMutationBatchAfterBatchID:last.batchID];
  183. XCTAssertNil(notFound);
  184. });
  185. }
  186. - (void)testNextMutationBatchAfterBatchIDSkipsAcknowledgedBatches {
  187. if ([self isTestBaseClass]) return;
  188. NSMutableArray<FSTMutationBatch *> *batches = self.persistence.run(
  189. "testNextMutationBatchAfterBatchIDSkipsAcknowledgedBatches newBatches",
  190. [&]() -> NSMutableArray<FSTMutationBatch *> * {
  191. NSMutableArray<FSTMutationBatch *> *newBatches = [self createBatches:3];
  192. XCTAssertEqualObjects([self.mutationQueue nextMutationBatchAfterBatchID:kFSTBatchIDUnknown],
  193. newBatches[0]);
  194. return newBatches;
  195. });
  196. self.persistence.run("testNextMutationBatchAfterBatchIDSkipsAcknowledgedBatches", [&]() {
  197. [self.mutationQueue acknowledgeBatch:batches[0] streamToken:nil];
  198. XCTAssertEqualObjects([self.mutationQueue nextMutationBatchAfterBatchID:kFSTBatchIDUnknown],
  199. batches[1]);
  200. XCTAssertEqualObjects([self.mutationQueue nextMutationBatchAfterBatchID:batches[0].batchID],
  201. batches[1]);
  202. XCTAssertEqualObjects([self.mutationQueue nextMutationBatchAfterBatchID:batches[1].batchID],
  203. batches[2]);
  204. });
  205. }
  206. - (void)testAllMutationBatchesThroughBatchID {
  207. if ([self isTestBaseClass]) return;
  208. self.persistence.run("testAllMutationBatchesThroughBatchID", [&]() {
  209. NSMutableArray<FSTMutationBatch *> *batches = [self createBatches:10];
  210. [self makeHoles:@[ @2, @6, @7 ] inBatches:batches];
  211. NSArray<FSTMutationBatch *> *found, *expected;
  212. found = [self.mutationQueue allMutationBatchesThroughBatchID:batches[0].batchID - 1];
  213. XCTAssertEqualObjects(found, (@[]));
  214. for (NSUInteger i = 0; i < batches.count; i++) {
  215. found = [self.mutationQueue allMutationBatchesThroughBatchID:batches[i].batchID];
  216. expected = [batches subarrayWithRange:NSMakeRange(0, i + 1)];
  217. XCTAssertEqualObjects(found, expected, @"for index %lu", (unsigned long)i);
  218. }
  219. });
  220. }
  221. - (void)testAllMutationBatchesAffectingDocumentKey {
  222. if ([self isTestBaseClass]) return;
  223. self.persistence.run("testAllMutationBatchesAffectingDocumentKey", [&]() {
  224. NSArray<FSTMutation *> *mutations = @[
  225. FSTTestSetMutation(@"fob/bar",
  226. @{ @"a" : @1 }),
  227. FSTTestSetMutation(@"foo/bar",
  228. @{ @"a" : @1 }),
  229. FSTTestPatchMutation("foo/bar",
  230. @{ @"b" : @1 }, {}),
  231. FSTTestSetMutation(@"foo/bar/suffix/key",
  232. @{ @"a" : @1 }),
  233. FSTTestSetMutation(@"foo/baz",
  234. @{ @"a" : @1 }),
  235. FSTTestSetMutation(@"food/bar",
  236. @{ @"a" : @1 })
  237. ];
  238. // Store all the mutations.
  239. NSMutableArray<FSTMutationBatch *> *batches = [NSMutableArray array];
  240. for (FSTMutation *mutation in mutations) {
  241. FSTMutationBatch *batch =
  242. [self.mutationQueue addMutationBatchWithWriteTime:[FIRTimestamp timestamp]
  243. mutations:@[ mutation ]];
  244. [batches addObject:batch];
  245. }
  246. NSArray<FSTMutationBatch *> *expected = @[ batches[1], batches[2] ];
  247. NSArray<FSTMutationBatch *> *matches =
  248. [self.mutationQueue allMutationBatchesAffectingDocumentKey:testutil::Key("foo/bar")];
  249. XCTAssertEqualObjects(matches, expected);
  250. });
  251. }
  252. - (void)testAllMutationBatchesAffectingQuery {
  253. if ([self isTestBaseClass]) return;
  254. self.persistence.run("testAllMutationBatchesAffectingQuery", [&]() {
  255. NSArray<FSTMutation *> *mutations = @[
  256. FSTTestSetMutation(@"fob/bar",
  257. @{ @"a" : @1 }),
  258. FSTTestSetMutation(@"foo/bar",
  259. @{ @"a" : @1 }),
  260. FSTTestPatchMutation("foo/bar",
  261. @{ @"b" : @1 }, {}),
  262. FSTTestSetMutation(@"foo/bar/suffix/key",
  263. @{ @"a" : @1 }),
  264. FSTTestSetMutation(@"foo/baz",
  265. @{ @"a" : @1 }),
  266. FSTTestSetMutation(@"food/bar",
  267. @{ @"a" : @1 })
  268. ];
  269. // Store all the mutations.
  270. NSMutableArray<FSTMutationBatch *> *batches = [NSMutableArray array];
  271. for (FSTMutation *mutation in mutations) {
  272. FSTMutationBatch *batch =
  273. [self.mutationQueue addMutationBatchWithWriteTime:[FIRTimestamp timestamp]
  274. mutations:@[ mutation ]];
  275. [batches addObject:batch];
  276. }
  277. NSArray<FSTMutationBatch *> *expected = @[ batches[1], batches[2], batches[4] ];
  278. FSTQuery *query = FSTTestQuery("foo");
  279. NSArray<FSTMutationBatch *> *matches =
  280. [self.mutationQueue allMutationBatchesAffectingQuery:query];
  281. XCTAssertEqualObjects(matches, expected);
  282. });
  283. }
  284. - (void)testRemoveMutationBatches {
  285. if ([self isTestBaseClass]) return;
  286. self.persistence.run("testRemoveMutationBatches", [&]() {
  287. NSMutableArray<FSTMutationBatch *> *batches = [self createBatches:10];
  288. [self.mutationQueue removeMutationBatches:@[ batches[0] ]];
  289. [batches removeObjectAtIndex:0];
  290. FSTMutationBatch *last = batches[batches.count - 1];
  291. XCTAssertEqual([self batchCount], 9);
  292. NSArray<FSTMutationBatch *> *found;
  293. found = [self.mutationQueue allMutationBatchesThroughBatchID:last.batchID];
  294. XCTAssertEqualObjects(found, batches);
  295. XCTAssertEqual(found.count, 9);
  296. [self.mutationQueue removeMutationBatches:@[ batches[0], batches[1], batches[2] ]];
  297. [batches removeObjectsInRange:NSMakeRange(0, 3)];
  298. XCTAssertEqual([self batchCount], 6);
  299. found = [self.mutationQueue allMutationBatchesThroughBatchID:last.batchID];
  300. XCTAssertEqualObjects(found, batches);
  301. XCTAssertEqual(found.count, 6);
  302. [self.mutationQueue removeMutationBatches:@[ batches[batches.count - 1] ]];
  303. [batches removeObjectAtIndex:batches.count - 1];
  304. XCTAssertEqual([self batchCount], 5);
  305. found = [self.mutationQueue allMutationBatchesThroughBatchID:last.batchID];
  306. XCTAssertEqualObjects(found, batches);
  307. XCTAssertEqual(found.count, 5);
  308. [self.mutationQueue removeMutationBatches:@[ batches[3] ]];
  309. [batches removeObjectAtIndex:3];
  310. XCTAssertEqual([self batchCount], 4);
  311. [self.mutationQueue removeMutationBatches:@[ batches[1] ]];
  312. [batches removeObjectAtIndex:1];
  313. XCTAssertEqual([self batchCount], 3);
  314. found = [self.mutationQueue allMutationBatchesThroughBatchID:last.batchID];
  315. XCTAssertEqualObjects(found, batches);
  316. XCTAssertEqual(found.count, 3);
  317. XCTAssertFalse([self.mutationQueue isEmpty]);
  318. [self.mutationQueue removeMutationBatches:batches];
  319. found = [self.mutationQueue allMutationBatchesThroughBatchID:last.batchID];
  320. XCTAssertEqualObjects(found, @[]);
  321. XCTAssertEqual(found.count, 0);
  322. XCTAssertTrue([self.mutationQueue isEmpty]);
  323. });
  324. }
  325. - (void)testRemoveMutationBatchesEmitsGarbageEvents {
  326. if ([self isTestBaseClass]) return;
  327. FSTEagerGarbageCollector *garbageCollector = [[FSTEagerGarbageCollector alloc] init];
  328. [garbageCollector addGarbageSource:self.mutationQueue];
  329. NSMutableArray<FSTMutationBatch *> *batches = [NSMutableArray array];
  330. self.persistence.run("testRemoveMutationBatchesEmitsGarbageEvents", [&]() {
  331. [batches addObjectsFromArray:@[
  332. [self addMutationBatchWithKey:@"foo/bar"],
  333. [self addMutationBatchWithKey:@"foo/ba"],
  334. [self addMutationBatchWithKey:@"foo/bar2"],
  335. [self addMutationBatchWithKey:@"foo/bar"],
  336. [self addMutationBatchWithKey:@"foo/bar/suffix/baz"],
  337. [self addMutationBatchWithKey:@"bar/baz"],
  338. ]];
  339. [self.mutationQueue removeMutationBatches:@[ batches[0] ]];
  340. std::set<DocumentKey> garbage = [garbageCollector collectGarbage];
  341. XCTAssertEqual(garbage, std::set<DocumentKey>({}));
  342. [self.mutationQueue removeMutationBatches:@[ batches[1] ]];
  343. garbage = [garbageCollector collectGarbage];
  344. XCTAssertEqual(garbage, std::set<DocumentKey>({testutil::Key("foo/ba")}));
  345. [self.mutationQueue removeMutationBatches:@[ batches[5] ]];
  346. garbage = [garbageCollector collectGarbage];
  347. XCTAssertEqual(garbage, std::set<DocumentKey>({testutil::Key("bar/baz")}));
  348. [self.mutationQueue removeMutationBatches:@[ batches[2], batches[3] ]];
  349. garbage = [garbageCollector collectGarbage];
  350. XCTAssertEqual(garbage,
  351. std::set<DocumentKey>({testutil::Key("foo/bar"), testutil::Key("foo/bar2")}));
  352. [batches addObject:[self addMutationBatchWithKey:@"foo/bar/suffix/baz"]];
  353. garbage = [garbageCollector collectGarbage];
  354. XCTAssertEqual(garbage, std::set<DocumentKey>({}));
  355. [self.mutationQueue removeMutationBatches:@[ batches[4], batches[6] ]];
  356. garbage = [garbageCollector collectGarbage];
  357. XCTAssertEqual(garbage, std::set<DocumentKey>({testutil::Key("foo/bar/suffix/baz")}));
  358. });
  359. }
  360. - (void)testStreamToken {
  361. if ([self isTestBaseClass]) return;
  362. NSData *streamToken1 = [@"token1" dataUsingEncoding:NSUTF8StringEncoding];
  363. NSData *streamToken2 = [@"token2" dataUsingEncoding:NSUTF8StringEncoding];
  364. self.persistence.run("testStreamToken", [&]() {
  365. [self.mutationQueue setLastStreamToken:streamToken1];
  366. FSTMutationBatch *batch1 = [self addMutationBatch];
  367. [self addMutationBatch];
  368. XCTAssertEqualObjects([self.mutationQueue lastStreamToken], streamToken1);
  369. [self.mutationQueue acknowledgeBatch:batch1 streamToken:streamToken2];
  370. XCTAssertEqual(self.mutationQueue.highestAcknowledgedBatchID, batch1.batchID);
  371. XCTAssertEqualObjects([self.mutationQueue lastStreamToken], streamToken2);
  372. });
  373. }
  374. #pragma mark - Helpers
  375. /** Creates a new FSTMutationBatch with the next batch ID and a set of dummy mutations. */
  376. - (FSTMutationBatch *)addMutationBatch {
  377. return [self addMutationBatchWithKey:@"foo/bar"];
  378. }
  379. /**
  380. * Creates a new FSTMutationBatch with the given key, the next batch ID and a set of dummy
  381. * mutations.
  382. */
  383. - (FSTMutationBatch *)addMutationBatchWithKey:(NSString *)key {
  384. FSTSetMutation *mutation = FSTTestSetMutation(key, @{ @"a" : @1 });
  385. FSTMutationBatch *batch =
  386. [self.mutationQueue addMutationBatchWithWriteTime:[FIRTimestamp timestamp]
  387. mutations:@[ mutation ]];
  388. return batch;
  389. }
  390. /**
  391. * Creates an array of batches containing @a number dummy FSTMutationBatches. Each has a different
  392. * batchID.
  393. */
  394. - (NSMutableArray<FSTMutationBatch *> *)createBatches:(int)number {
  395. NSMutableArray<FSTMutationBatch *> *batches = [NSMutableArray array];
  396. for (int i = 0; i < number; i++) {
  397. FSTMutationBatch *batch = [self addMutationBatch];
  398. [batches addObject:batch];
  399. }
  400. return batches;
  401. }
  402. /** Returns the number of mutation batches in the mutation queue. */
  403. - (NSUInteger)batchCount {
  404. return [self.mutationQueue allMutationBatches].count;
  405. }
  406. /**
  407. * Removes entries from from the given @a batches and returns them.
  408. *
  409. * @param holes An array of indexes in the batches array; in increasing order. Indexes are relative
  410. * to the original state of the batches array, not any intermediate state that might occur.
  411. * @param batches The array to mutate, removing entries from it.
  412. * @return A new array containing all the entries that were removed from @a batches.
  413. */
  414. - (NSArray<FSTMutationBatch *> *)makeHoles:(NSArray<NSNumber *> *)holes
  415. inBatches:(NSMutableArray<FSTMutationBatch *> *)batches {
  416. NSMutableArray<FSTMutationBatch *> *removed = [NSMutableArray array];
  417. for (NSUInteger i = 0; i < holes.count; i++) {
  418. NSUInteger index = holes[i].unsignedIntegerValue - i;
  419. FSTMutationBatch *batch = batches[index];
  420. [self.mutationQueue removeMutationBatches:@[ batch ]];
  421. [batches removeObjectAtIndex:index];
  422. [removed addObject:batch];
  423. }
  424. return removed;
  425. }
  426. @end
  427. NS_ASSUME_NONNULL_END