FSTMutationQueueTests.mm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 removeMutationBatch:batch1];
  59. XCTAssertEqual(1, [self batchCount]);
  60. [self.mutationQueue removeMutationBatch:batch2];
  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 removeMutationBatch:batch1];
  80. [self.mutationQueue acknowledgeBatch:batch2 streamToken:nil];
  81. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch2.batchID);
  82. [self.mutationQueue removeMutationBatch:batch2];
  83. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch2.batchID);
  84. // Batch 3 never acknowledged.
  85. [self.mutationQueue removeMutationBatch:batch3];
  86. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch2.batchID);
  87. });
  88. }
  89. - (void)testAcknowledgeThenRemove {
  90. if ([self isTestBaseClass]) return;
  91. self.persistence.run("testAcknowledgeThenRemove", [&]() {
  92. FSTMutationBatch *batch1 = [self addMutationBatch];
  93. [self.mutationQueue acknowledgeBatch:batch1 streamToken:nil];
  94. [self.mutationQueue removeMutationBatch:batch1];
  95. XCTAssertEqual([self batchCount], 0);
  96. XCTAssertEqual([self.mutationQueue highestAcknowledgedBatchID], batch1.batchID);
  97. });
  98. }
  99. - (void)testLookupMutationBatch {
  100. if ([self isTestBaseClass]) return;
  101. // Searching on an empty queue should not find a non-existent batch
  102. self.persistence.run("testLookupMutationBatch", [&]() {
  103. FSTMutationBatch *notFound = [self.mutationQueue lookupMutationBatch:42];
  104. XCTAssertNil(notFound);
  105. NSMutableArray<FSTMutationBatch *> *batches = [self createBatches:10];
  106. NSArray<FSTMutationBatch *> *removed = [self removeFirstBatches:3 inBatches:batches];
  107. // After removing, a batch should not be found
  108. for (NSUInteger i = 0; i < removed.count; i++) {
  109. notFound = [self.mutationQueue lookupMutationBatch:removed[i].batchID];
  110. XCTAssertNil(notFound);
  111. }
  112. // Remaining entries should still be found
  113. for (FSTMutationBatch *batch in batches) {
  114. FSTMutationBatch *found = [self.mutationQueue lookupMutationBatch:batch.batchID];
  115. XCTAssertEqual(found.batchID, batch.batchID);
  116. }
  117. // Even on a nonempty queue searching should not find a non-existent batch
  118. notFound = [self.mutationQueue lookupMutationBatch:42];
  119. XCTAssertNil(notFound);
  120. });
  121. }
  122. - (void)testNextMutationBatchAfterBatchID {
  123. if ([self isTestBaseClass]) return;
  124. self.persistence.run("testNextMutationBatchAfterBatchID", [&]() {
  125. NSMutableArray<FSTMutationBatch *> *batches = [self createBatches:10];
  126. NSArray<FSTMutationBatch *> *removed = [self removeFirstBatches:3 inBatches:batches];
  127. for (NSUInteger i = 0; i < batches.count - 1; i++) {
  128. FSTMutationBatch *current = batches[i];
  129. FSTMutationBatch *next = batches[i + 1];
  130. FSTMutationBatch *found = [self.mutationQueue nextMutationBatchAfterBatchID:current.batchID];
  131. XCTAssertEqual(found.batchID, next.batchID);
  132. }
  133. for (NSUInteger i = 0; i < removed.count; i++) {
  134. FSTMutationBatch *current = removed[i];
  135. FSTMutationBatch *next = batches[0];
  136. FSTMutationBatch *found = [self.mutationQueue nextMutationBatchAfterBatchID:current.batchID];
  137. XCTAssertEqual(found.batchID, next.batchID);
  138. }
  139. FSTMutationBatch *first = batches[0];
  140. FSTMutationBatch *found = [self.mutationQueue nextMutationBatchAfterBatchID:first.batchID - 42];
  141. XCTAssertEqual(found.batchID, first.batchID);
  142. FSTMutationBatch *last = batches[batches.count - 1];
  143. FSTMutationBatch *notFound = [self.mutationQueue nextMutationBatchAfterBatchID:last.batchID];
  144. XCTAssertNil(notFound);
  145. });
  146. }
  147. - (void)testNextMutationBatchAfterBatchIDSkipsAcknowledgedBatches {
  148. if ([self isTestBaseClass]) return;
  149. NSMutableArray<FSTMutationBatch *> *batches = self.persistence.run(
  150. "testNextMutationBatchAfterBatchIDSkipsAcknowledgedBatches newBatches",
  151. [&]() -> NSMutableArray<FSTMutationBatch *> * {
  152. NSMutableArray<FSTMutationBatch *> *newBatches = [self createBatches:3];
  153. XCTAssertEqualObjects([self.mutationQueue nextMutationBatchAfterBatchID:kFSTBatchIDUnknown],
  154. newBatches[0]);
  155. return newBatches;
  156. });
  157. self.persistence.run("testNextMutationBatchAfterBatchIDSkipsAcknowledgedBatches", [&]() {
  158. [self.mutationQueue acknowledgeBatch:batches[0] streamToken:nil];
  159. XCTAssertEqualObjects([self.mutationQueue nextMutationBatchAfterBatchID:kFSTBatchIDUnknown],
  160. batches[1]);
  161. XCTAssertEqualObjects([self.mutationQueue nextMutationBatchAfterBatchID:batches[0].batchID],
  162. batches[1]);
  163. XCTAssertEqualObjects([self.mutationQueue nextMutationBatchAfterBatchID:batches[1].batchID],
  164. batches[2]);
  165. });
  166. }
  167. - (void)testAllMutationBatchesAffectingDocumentKey {
  168. if ([self isTestBaseClass]) return;
  169. self.persistence.run("testAllMutationBatchesAffectingDocumentKey", [&]() {
  170. NSArray<FSTMutation *> *mutations = @[
  171. FSTTestSetMutation(@"foi/bar",
  172. @{@"a" : @1}),
  173. FSTTestSetMutation(@"foo/bar",
  174. @{@"a" : @1}),
  175. FSTTestPatchMutation("foo/bar",
  176. @{@"b" : @1}, {}),
  177. FSTTestSetMutation(@"foo/bar/suffix/key",
  178. @{@"a" : @1}),
  179. FSTTestSetMutation(@"foo/baz",
  180. @{@"a" : @1}),
  181. FSTTestSetMutation(@"food/bar",
  182. @{@"a" : @1})
  183. ];
  184. // Store all the mutations.
  185. NSMutableArray<FSTMutationBatch *> *batches = [NSMutableArray array];
  186. for (FSTMutation *mutation in mutations) {
  187. FSTMutationBatch *batch =
  188. [self.mutationQueue addMutationBatchWithWriteTime:[FIRTimestamp timestamp]
  189. mutations:@[ mutation ]];
  190. [batches addObject:batch];
  191. }
  192. NSArray<FSTMutationBatch *> *expected = @[ batches[1], batches[2] ];
  193. NSArray<FSTMutationBatch *> *matches =
  194. [self.mutationQueue allMutationBatchesAffectingDocumentKey:testutil::Key("foo/bar")];
  195. XCTAssertEqualObjects(matches, expected);
  196. });
  197. }
  198. - (void)testAllMutationBatchesAffectingDocumentKeys {
  199. if ([self isTestBaseClass]) return;
  200. self.persistence.run("testAllMutationBatchesAffectingDocumentKey", [&]() {
  201. NSArray<FSTMutation *> *mutations = @[
  202. FSTTestSetMutation(@"fob/bar",
  203. @{@"a" : @1}),
  204. FSTTestSetMutation(@"foo/bar",
  205. @{@"a" : @1}),
  206. FSTTestPatchMutation("foo/bar",
  207. @{@"b" : @1}, {}),
  208. FSTTestSetMutation(@"foo/bar/suffix/key",
  209. @{@"a" : @1}),
  210. FSTTestSetMutation(@"foo/baz",
  211. @{@"a" : @1}),
  212. FSTTestSetMutation(@"food/bar",
  213. @{@"a" : @1})
  214. ];
  215. // Store all the mutations.
  216. NSMutableArray<FSTMutationBatch *> *batches = [NSMutableArray array];
  217. for (FSTMutation *mutation in mutations) {
  218. FSTMutationBatch *batch =
  219. [self.mutationQueue addMutationBatchWithWriteTime:[FIRTimestamp timestamp]
  220. mutations:@[ mutation ]];
  221. [batches addObject:batch];
  222. }
  223. DocumentKeySet keys{
  224. Key("foo/bar"),
  225. Key("foo/baz"),
  226. };
  227. NSArray<FSTMutationBatch *> *expected = @[ batches[1], batches[2], batches[4] ];
  228. NSArray<FSTMutationBatch *> *matches =
  229. [self.mutationQueue allMutationBatchesAffectingDocumentKeys:keys];
  230. XCTAssertEqualObjects(matches, expected);
  231. });
  232. }
  233. - (void)testAllMutationBatchesAffectingDocumentKeys_handlesOverlap {
  234. if ([self isTestBaseClass]) return;
  235. self.persistence.run("testAllMutationBatchesAffectingDocumentKeys_handlesOverlap", [&]() {
  236. NSArray<FSTMutation *> *group1 = @[
  237. FSTTestSetMutation(@"foo/bar",
  238. @{@"a" : @1}),
  239. FSTTestSetMutation(@"foo/baz",
  240. @{@"a" : @1}),
  241. ];
  242. FSTMutationBatch *batch1 =
  243. [self.mutationQueue addMutationBatchWithWriteTime:[FIRTimestamp timestamp]
  244. mutations:group1];
  245. NSArray<FSTMutation *> *group2 = @[ FSTTestSetMutation(@"food/bar", @{@"a" : @1}) ];
  246. [self.mutationQueue addMutationBatchWithWriteTime:[FIRTimestamp timestamp] mutations:group2];
  247. NSArray<FSTMutation *> *group3 = @[
  248. FSTTestSetMutation(@"foo/bar",
  249. @{@"b" : @1}),
  250. ];
  251. FSTMutationBatch *batch3 =
  252. [self.mutationQueue addMutationBatchWithWriteTime:[FIRTimestamp timestamp]
  253. mutations:group3];
  254. DocumentKeySet keys{
  255. Key("foo/bar"),
  256. Key("foo/baz"),
  257. };
  258. NSArray<FSTMutationBatch *> *expected = @[ batch1, batch3 ];
  259. NSArray<FSTMutationBatch *> *matches =
  260. [self.mutationQueue allMutationBatchesAffectingDocumentKeys:keys];
  261. XCTAssertEqualObjects(matches, expected);
  262. });
  263. }
  264. - (void)testAllMutationBatchesAffectingQuery {
  265. if ([self isTestBaseClass]) return;
  266. self.persistence.run("testAllMutationBatchesAffectingQuery", [&]() {
  267. NSArray<FSTMutation *> *mutations = @[
  268. FSTTestSetMutation(@"fob/bar",
  269. @{@"a" : @1}),
  270. FSTTestSetMutation(@"foo/bar",
  271. @{@"a" : @1}),
  272. FSTTestPatchMutation("foo/bar",
  273. @{@"b" : @1}, {}),
  274. FSTTestSetMutation(@"foo/bar/suffix/key",
  275. @{@"a" : @1}),
  276. FSTTestSetMutation(@"foo/baz",
  277. @{@"a" : @1}),
  278. FSTTestSetMutation(@"food/bar",
  279. @{@"a" : @1})
  280. ];
  281. // Store all the mutations.
  282. NSMutableArray<FSTMutationBatch *> *batches = [NSMutableArray array];
  283. for (FSTMutation *mutation in mutations) {
  284. FSTMutationBatch *batch =
  285. [self.mutationQueue addMutationBatchWithWriteTime:[FIRTimestamp timestamp]
  286. mutations:@[ mutation ]];
  287. [batches addObject:batch];
  288. }
  289. NSArray<FSTMutationBatch *> *expected = @[ batches[1], batches[2], batches[4] ];
  290. FSTQuery *query = FSTTestQuery("foo");
  291. NSArray<FSTMutationBatch *> *matches =
  292. [self.mutationQueue allMutationBatchesAffectingQuery:query];
  293. XCTAssertEqualObjects(matches, expected);
  294. });
  295. }
  296. - (void)testRemoveMutationBatches {
  297. if ([self isTestBaseClass]) return;
  298. self.persistence.run("testRemoveMutationBatches", [&]() {
  299. NSMutableArray<FSTMutationBatch *> *batches = [self createBatches:10];
  300. [self.mutationQueue removeMutationBatch:batches[0]];
  301. [batches removeObjectAtIndex:0];
  302. XCTAssertEqual([self batchCount], 9);
  303. NSArray<FSTMutationBatch *> *found;
  304. found = [self.mutationQueue allMutationBatches];
  305. XCTAssertEqualObjects(found, batches);
  306. XCTAssertEqual(found.count, 9);
  307. [self.mutationQueue removeMutationBatch:batches[0]];
  308. [self.mutationQueue removeMutationBatch:batches[1]];
  309. [self.mutationQueue removeMutationBatch:batches[2]];
  310. [batches removeObjectsInRange:NSMakeRange(0, 3)];
  311. XCTAssertEqual([self batchCount], 6);
  312. found = [self.mutationQueue allMutationBatches];
  313. XCTAssertEqualObjects(found, batches);
  314. XCTAssertEqual(found.count, 6);
  315. [self.mutationQueue removeMutationBatch:batches[0]];
  316. [batches removeObjectAtIndex:0];
  317. XCTAssertEqual([self batchCount], 5);
  318. found = [self.mutationQueue allMutationBatches];
  319. XCTAssertEqualObjects(found, batches);
  320. XCTAssertEqual(found.count, 5);
  321. [self.mutationQueue removeMutationBatch:batches[0]];
  322. [batches removeObjectAtIndex:0];
  323. XCTAssertEqual([self batchCount], 4);
  324. [self.mutationQueue removeMutationBatch:batches[0]];
  325. [batches removeObjectAtIndex:0];
  326. XCTAssertEqual([self batchCount], 3);
  327. found = [self.mutationQueue allMutationBatches];
  328. XCTAssertEqualObjects(found, batches);
  329. XCTAssertEqual(found.count, 3);
  330. XCTAssertFalse([self.mutationQueue isEmpty]);
  331. for (FSTMutationBatch *batch in batches) {
  332. [self.mutationQueue removeMutationBatch:batch];
  333. }
  334. found = [self.mutationQueue allMutationBatches];
  335. XCTAssertEqualObjects(found, @[]);
  336. XCTAssertEqual(found.count, 0);
  337. XCTAssertTrue([self.mutationQueue isEmpty]);
  338. });
  339. }
  340. - (void)testStreamToken {
  341. if ([self isTestBaseClass]) return;
  342. NSData *streamToken1 = [@"token1" dataUsingEncoding:NSUTF8StringEncoding];
  343. NSData *streamToken2 = [@"token2" dataUsingEncoding:NSUTF8StringEncoding];
  344. self.persistence.run("testStreamToken", [&]() {
  345. [self.mutationQueue setLastStreamToken:streamToken1];
  346. FSTMutationBatch *batch1 = [self addMutationBatch];
  347. [self addMutationBatch];
  348. XCTAssertEqualObjects([self.mutationQueue lastStreamToken], streamToken1);
  349. [self.mutationQueue acknowledgeBatch:batch1 streamToken:streamToken2];
  350. XCTAssertEqual(self.mutationQueue.highestAcknowledgedBatchID, batch1.batchID);
  351. XCTAssertEqualObjects([self.mutationQueue lastStreamToken], streamToken2);
  352. });
  353. }
  354. #pragma mark - Helpers
  355. /** Creates a new FSTMutationBatch with the next batch ID and a set of dummy mutations. */
  356. - (FSTMutationBatch *)addMutationBatch {
  357. return [self addMutationBatchWithKey:@"foo/bar"];
  358. }
  359. /**
  360. * Creates a new FSTMutationBatch with the given key, the next batch ID and a set of dummy
  361. * mutations.
  362. */
  363. - (FSTMutationBatch *)addMutationBatchWithKey:(NSString *)key {
  364. FSTSetMutation *mutation = FSTTestSetMutation(key, @{@"a" : @1});
  365. FSTMutationBatch *batch =
  366. [self.mutationQueue addMutationBatchWithWriteTime:[FIRTimestamp timestamp]
  367. mutations:@[ mutation ]];
  368. return batch;
  369. }
  370. /**
  371. * Creates an array of batches containing @a number dummy FSTMutationBatches. Each has a different
  372. * batchID.
  373. */
  374. - (NSMutableArray<FSTMutationBatch *> *)createBatches:(int)number {
  375. NSMutableArray<FSTMutationBatch *> *batches = [NSMutableArray array];
  376. for (int i = 0; i < number; i++) {
  377. FSTMutationBatch *batch = [self addMutationBatch];
  378. [batches addObject:batch];
  379. }
  380. return batches;
  381. }
  382. /** Returns the number of mutation batches in the mutation queue. */
  383. - (NSUInteger)batchCount {
  384. return [self.mutationQueue allMutationBatches].count;
  385. }
  386. /**
  387. * Removes the first n entries from the the given batches and returns them.
  388. *
  389. * @param n The number of batches to remove.
  390. * @param batches The array to mutate, removing entries from it.
  391. * @return A new array containing all the entries that were removed from @a batches.
  392. */
  393. - (NSArray<FSTMutationBatch *> *)removeFirstBatches:(NSUInteger)n
  394. inBatches:(NSMutableArray<FSTMutationBatch *> *)batches {
  395. NSArray<FSTMutationBatch *> *removed = [batches subarrayWithRange:NSMakeRange(0, n)];
  396. [batches removeObjectsInRange:NSMakeRange(0, n)];
  397. [removed enumerateObjectsUsingBlock:^(FSTMutationBatch *batch, NSUInteger idx, BOOL *stop) {
  398. [self.mutationQueue removeMutationBatch:batch];
  399. }];
  400. return removed;
  401. }
  402. @end
  403. NS_ASSUME_NONNULL_END