FSTMutationQueueTests.mm 22 KB

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