FSTMutationQueueTests.mm 24 KB

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