FSTMutationQueueTests.mm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. #include <utility>
  20. #include <vector>
  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/src/firebase/firestore/model/mutation_batch.h"
  29. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  30. namespace core = firebase::firestore::core;
  31. namespace testutil = firebase::firestore::testutil;
  32. using firebase::Timestamp;
  33. using firebase::firestore::auth::User;
  34. using firebase::firestore::model::DocumentKey;
  35. using firebase::firestore::model::DocumentKeySet;
  36. using firebase::firestore::model::kBatchIdUnknown;
  37. using firebase::firestore::testutil::Key;
  38. using firebase::firestore::testutil::Query;
  39. NS_ASSUME_NONNULL_BEGIN
  40. @implementation FSTMutationQueueTests
  41. - (void)tearDown {
  42. [self.persistence shutdown];
  43. [super tearDown];
  44. }
  45. /**
  46. * Xcode will run tests from any class that extends XCTestCase, but this doesn't work for
  47. * FSTMutationQueueTests since it is incomplete without the implementations supplied by its
  48. * subclasses.
  49. */
  50. - (BOOL)isTestBaseClass {
  51. return [self class] == [FSTMutationQueueTests class];
  52. }
  53. - (void)testCountBatches {
  54. if ([self isTestBaseClass]) return;
  55. self.persistence.run("testCountBatches", [&]() {
  56. XCTAssertEqual(0, [self batchCount]);
  57. XCTAssertTrue(self.mutationQueue->IsEmpty());
  58. FSTMutationBatch *batch1 = [self addMutationBatch];
  59. XCTAssertEqual(1, [self batchCount]);
  60. XCTAssertFalse(self.mutationQueue->IsEmpty());
  61. FSTMutationBatch *batch2 = [self addMutationBatch];
  62. XCTAssertEqual(2, [self batchCount]);
  63. self.mutationQueue->RemoveMutationBatch(batch1);
  64. XCTAssertEqual(1, [self batchCount]);
  65. self.mutationQueue->RemoveMutationBatch(batch2);
  66. XCTAssertEqual(0, [self batchCount]);
  67. XCTAssertTrue(self.mutationQueue->IsEmpty());
  68. });
  69. }
  70. - (void)testAcknowledgeBatchID {
  71. if ([self isTestBaseClass]) return;
  72. self.persistence.run("testAcknowledgeBatchID", [&]() {
  73. XCTAssertEqual([self batchCount], 0);
  74. FSTMutationBatch *batch1 = [self addMutationBatch];
  75. FSTMutationBatch *batch2 = [self addMutationBatch];
  76. FSTMutationBatch *batch3 = [self addMutationBatch];
  77. XCTAssertGreaterThan(batch1.batchID, kBatchIdUnknown);
  78. XCTAssertGreaterThan(batch2.batchID, batch1.batchID);
  79. XCTAssertGreaterThan(batch3.batchID, batch2.batchID);
  80. XCTAssertEqual([self batchCount], 3);
  81. self.mutationQueue->AcknowledgeBatch(batch1, nil);
  82. self.mutationQueue->RemoveMutationBatch(batch1);
  83. XCTAssertEqual([self batchCount], 2);
  84. self.mutationQueue->AcknowledgeBatch(batch2, nil);
  85. XCTAssertEqual([self batchCount], 2);
  86. self.mutationQueue->RemoveMutationBatch(batch2);
  87. XCTAssertEqual([self batchCount], 1);
  88. self.mutationQueue->RemoveMutationBatch(batch3);
  89. XCTAssertEqual([self batchCount], 0);
  90. });
  91. }
  92. - (void)testAcknowledgeThenRemove {
  93. if ([self isTestBaseClass]) return;
  94. self.persistence.run("testAcknowledgeThenRemove", [&]() {
  95. FSTMutationBatch *batch1 = [self addMutationBatch];
  96. self.mutationQueue->AcknowledgeBatch(batch1, nil);
  97. self.mutationQueue->RemoveMutationBatch(batch1);
  98. XCTAssertEqual([self batchCount], 0);
  99. });
  100. }
  101. - (void)testLookupMutationBatch {
  102. if ([self isTestBaseClass]) return;
  103. // Searching on an empty queue should not find a non-existent batch
  104. self.persistence.run("testLookupMutationBatch", [&]() {
  105. FSTMutationBatch *notFound = self.mutationQueue->LookupMutationBatch(42);
  106. XCTAssertNil(notFound);
  107. std::vector<FSTMutationBatch *> batches = [self createBatches:10];
  108. std::vector<FSTMutationBatch *> removed = [self removeFirstBatches:3 inBatches:&batches];
  109. // After removing, a batch should not be found
  110. for (size_t i = 0; i < removed.size(); i++) {
  111. notFound = self.mutationQueue->LookupMutationBatch(removed[i].batchID);
  112. XCTAssertNil(notFound);
  113. }
  114. // Remaining entries should still be found
  115. for (FSTMutationBatch *batch : batches) {
  116. FSTMutationBatch *found = self.mutationQueue->LookupMutationBatch(batch.batchID);
  117. XCTAssertEqual(found.batchID, batch.batchID);
  118. }
  119. // Even on a nonempty queue searching should not find a non-existent batch
  120. notFound = self.mutationQueue->LookupMutationBatch(42);
  121. XCTAssertNil(notFound);
  122. });
  123. }
  124. - (void)testNextMutationBatchAfterBatchID {
  125. if ([self isTestBaseClass]) return;
  126. self.persistence.run("testNextMutationBatchAfterBatchID", [&]() {
  127. std::vector<FSTMutationBatch *> batches = [self createBatches:10];
  128. std::vector<FSTMutationBatch *> removed = [self removeFirstBatches:3 inBatches:&batches];
  129. for (size_t i = 0; i < batches.size() - 1; i++) {
  130. FSTMutationBatch *current = batches[i];
  131. FSTMutationBatch *next = batches[i + 1];
  132. FSTMutationBatch *found = self.mutationQueue->NextMutationBatchAfterBatchId(current.batchID);
  133. XCTAssertEqual(found.batchID, next.batchID);
  134. }
  135. for (size_t i = 0; i < removed.size(); i++) {
  136. FSTMutationBatch *current = removed[i];
  137. FSTMutationBatch *next = batches[0];
  138. FSTMutationBatch *found = self.mutationQueue->NextMutationBatchAfterBatchId(current.batchID);
  139. XCTAssertEqual(found.batchID, next.batchID);
  140. }
  141. FSTMutationBatch *first = batches[0];
  142. FSTMutationBatch *found = self.mutationQueue->NextMutationBatchAfterBatchId(first.batchID - 42);
  143. XCTAssertEqual(found.batchID, first.batchID);
  144. FSTMutationBatch *last = batches[batches.size() - 1];
  145. FSTMutationBatch *notFound = self.mutationQueue->NextMutationBatchAfterBatchId(last.batchID);
  146. XCTAssertNil(notFound);
  147. });
  148. }
  149. - (void)testAllMutationBatchesAffectingDocumentKey {
  150. if ([self isTestBaseClass]) return;
  151. self.persistence.run("testAllMutationBatchesAffectingDocumentKey", [&]() {
  152. NSArray<FSTMutation *> *mutations = @[
  153. FSTTestSetMutation(@"foi/bar", @{@"a" : @1}), FSTTestSetMutation(@"foo/bar", @{@"a" : @1}),
  154. FSTTestPatchMutation("foo/bar", @{@"b" : @1}, {}),
  155. FSTTestSetMutation(@"foo/bar/suffix/key", @{@"a" : @1}),
  156. FSTTestSetMutation(@"foo/baz", @{@"a" : @1}), FSTTestSetMutation(@"food/bar", @{@"a" : @1})
  157. ];
  158. // Store all the mutations.
  159. NSMutableArray<FSTMutationBatch *> *batches = [NSMutableArray array];
  160. for (FSTMutation *mutation in mutations) {
  161. FSTMutationBatch *batch =
  162. self.mutationQueue->AddMutationBatch(Timestamp::Now(), {}, {mutation});
  163. [batches addObject:batch];
  164. }
  165. std::vector<FSTMutationBatch *> expected{batches[1], batches[2]};
  166. std::vector<FSTMutationBatch *> matches =
  167. self.mutationQueue->AllMutationBatchesAffectingDocumentKey(testutil::Key("foo/bar"));
  168. FSTAssertEqualVectors(matches, expected);
  169. });
  170. }
  171. - (void)testAllMutationBatchesAffectingDocumentKeys {
  172. if ([self isTestBaseClass]) return;
  173. self.persistence.run("testAllMutationBatchesAffectingDocumentKey", [&]() {
  174. NSArray<FSTMutation *> *mutations = @[
  175. FSTTestSetMutation(@"fob/bar", @{@"a" : @1}), FSTTestSetMutation(@"foo/bar", @{@"a" : @1}),
  176. FSTTestPatchMutation("foo/bar", @{@"b" : @1}, {}),
  177. FSTTestSetMutation(@"foo/bar/suffix/key", @{@"a" : @1}),
  178. FSTTestSetMutation(@"foo/baz", @{@"a" : @1}), FSTTestSetMutation(@"food/bar", @{@"a" : @1})
  179. ];
  180. // Store all the mutations.
  181. NSMutableArray<FSTMutationBatch *> *batches = [NSMutableArray array];
  182. for (FSTMutation *mutation in mutations) {
  183. FSTMutationBatch *batch =
  184. self.mutationQueue->AddMutationBatch(Timestamp::Now(), {}, {mutation});
  185. [batches addObject:batch];
  186. }
  187. DocumentKeySet keys{
  188. Key("foo/bar"),
  189. Key("foo/baz"),
  190. };
  191. std::vector<FSTMutationBatch *> expected{batches[1], batches[2], batches[4]};
  192. std::vector<FSTMutationBatch *> matches =
  193. self.mutationQueue->AllMutationBatchesAffectingDocumentKeys(keys);
  194. FSTAssertEqualVectors(matches, expected);
  195. });
  196. }
  197. - (void)testAllMutationBatchesAffectingDocumentKeys_handlesOverlap {
  198. if ([self isTestBaseClass]) return;
  199. self.persistence.run("testAllMutationBatchesAffectingDocumentKeys_handlesOverlap", [&]() {
  200. std::vector<FSTMutation *> group1 = {
  201. FSTTestSetMutation(@"foo/bar", @{@"a" : @1}),
  202. FSTTestSetMutation(@"foo/baz", @{@"a" : @1}),
  203. };
  204. FSTMutationBatch *batch1 =
  205. self.mutationQueue->AddMutationBatch(Timestamp::Now(), {}, std::move(group1));
  206. std::vector<FSTMutation *> group2 = {FSTTestSetMutation(@"food/bar", @{@"a" : @1})};
  207. self.mutationQueue->AddMutationBatch(Timestamp::Now(), {}, std::move(group2));
  208. std::vector<FSTMutation *> group3 = {
  209. FSTTestSetMutation(@"foo/bar", @{@"b" : @1}),
  210. };
  211. FSTMutationBatch *batch3 =
  212. self.mutationQueue->AddMutationBatch(Timestamp::Now(), {}, std::move(group3));
  213. DocumentKeySet keys{
  214. Key("foo/bar"),
  215. Key("foo/baz"),
  216. };
  217. std::vector<FSTMutationBatch *> expected{batch1, batch3};
  218. std::vector<FSTMutationBatch *> matches =
  219. self.mutationQueue->AllMutationBatchesAffectingDocumentKeys(keys);
  220. FSTAssertEqualVectors(matches, expected);
  221. });
  222. }
  223. - (void)testAllMutationBatchesAffectingQuery {
  224. if ([self isTestBaseClass]) return;
  225. self.persistence.run("testAllMutationBatchesAffectingQuery", [&]() {
  226. NSArray<FSTMutation *> *mutations = @[
  227. FSTTestSetMutation(@"fob/bar", @{@"a" : @1}), FSTTestSetMutation(@"foo/bar", @{@"a" : @1}),
  228. FSTTestPatchMutation("foo/bar", @{@"b" : @1}, {}),
  229. FSTTestSetMutation(@"foo/bar/suffix/key", @{@"a" : @1}),
  230. FSTTestSetMutation(@"foo/baz", @{@"a" : @1}), FSTTestSetMutation(@"food/bar", @{@"a" : @1})
  231. ];
  232. // Store all the mutations.
  233. NSMutableArray<FSTMutationBatch *> *batches = [NSMutableArray array];
  234. for (FSTMutation *mutation in mutations) {
  235. FSTMutationBatch *batch =
  236. self.mutationQueue->AddMutationBatch(Timestamp::Now(), {}, {mutation});
  237. [batches addObject:batch];
  238. }
  239. std::vector<FSTMutationBatch *> expected = {batches[1], batches[2], batches[4]};
  240. core::Query query = Query("foo");
  241. std::vector<FSTMutationBatch *> matches =
  242. self.mutationQueue->AllMutationBatchesAffectingQuery(query);
  243. FSTAssertEqualVectors(matches, expected);
  244. });
  245. }
  246. - (void)testRemoveMutationBatches {
  247. if ([self isTestBaseClass]) return;
  248. self.persistence.run("testRemoveMutationBatches", [&]() {
  249. std::vector<FSTMutationBatch *> batches = [self createBatches:10];
  250. self.mutationQueue->RemoveMutationBatch(batches[0]);
  251. batches.erase(batches.begin());
  252. XCTAssertEqual([self batchCount], 9);
  253. std::vector<FSTMutationBatch *> found;
  254. found = self.mutationQueue->AllMutationBatches();
  255. FSTAssertEqualVectors(found, batches);
  256. XCTAssertEqual(found.size(), 9);
  257. self.mutationQueue->RemoveMutationBatch(batches[0]);
  258. self.mutationQueue->RemoveMutationBatch(batches[1]);
  259. self.mutationQueue->RemoveMutationBatch(batches[2]);
  260. batches.erase(batches.begin(), batches.begin() + 3);
  261. XCTAssertEqual([self batchCount], 6);
  262. found = self.mutationQueue->AllMutationBatches();
  263. FSTAssertEqualVectors(found, batches);
  264. XCTAssertEqual(found.size(), 6);
  265. self.mutationQueue->RemoveMutationBatch(batches[0]);
  266. batches.erase(batches.begin());
  267. XCTAssertEqual([self batchCount], 5);
  268. found = self.mutationQueue->AllMutationBatches();
  269. FSTAssertEqualVectors(found, batches);
  270. XCTAssertEqual(found.size(), 5);
  271. self.mutationQueue->RemoveMutationBatch(batches[0]);
  272. batches.erase(batches.begin());
  273. XCTAssertEqual([self batchCount], 4);
  274. self.mutationQueue->RemoveMutationBatch(batches[0]);
  275. batches.erase(batches.begin());
  276. XCTAssertEqual([self batchCount], 3);
  277. found = self.mutationQueue->AllMutationBatches();
  278. FSTAssertEqualVectors(found, batches);
  279. XCTAssertEqual(found.size(), 3);
  280. XCTAssertFalse(self.mutationQueue->IsEmpty());
  281. for (FSTMutationBatch *batch : batches) {
  282. self.mutationQueue->RemoveMutationBatch(batch);
  283. }
  284. found = self.mutationQueue->AllMutationBatches();
  285. XCTAssertEqual(found.size(), 0);
  286. XCTAssertTrue(self.mutationQueue->IsEmpty());
  287. });
  288. }
  289. - (void)testStreamToken {
  290. if ([self isTestBaseClass]) return;
  291. NSData *streamToken1 = [@"token1" dataUsingEncoding:NSUTF8StringEncoding];
  292. NSData *streamToken2 = [@"token2" dataUsingEncoding:NSUTF8StringEncoding];
  293. self.persistence.run("testStreamToken", [&]() {
  294. self.mutationQueue->SetLastStreamToken(streamToken1);
  295. FSTMutationBatch *batch1 = [self addMutationBatch];
  296. [self addMutationBatch];
  297. XCTAssertEqualObjects(self.mutationQueue->GetLastStreamToken(), streamToken1);
  298. self.mutationQueue->AcknowledgeBatch(batch1, streamToken2);
  299. XCTAssertEqualObjects(self.mutationQueue->GetLastStreamToken(), streamToken2);
  300. });
  301. }
  302. #pragma mark - Helpers
  303. /** Creates a new FSTMutationBatch with the next batch ID and a set of dummy mutations. */
  304. - (FSTMutationBatch *)addMutationBatch {
  305. return [self addMutationBatchWithKey:@"foo/bar"];
  306. }
  307. /**
  308. * Creates a new FSTMutationBatch with the given key, the next batch ID and a set of dummy
  309. * mutations.
  310. */
  311. - (FSTMutationBatch *)addMutationBatchWithKey:(NSString *)key {
  312. FSTSetMutation *mutation = FSTTestSetMutation(key, @{@"a" : @1});
  313. FSTMutationBatch *batch = self.mutationQueue->AddMutationBatch(Timestamp::Now(), {}, {mutation});
  314. return batch;
  315. }
  316. /**
  317. * Creates an array of batches containing @a number dummy FSTMutationBatches. Each has a different
  318. * batchID.
  319. */
  320. - (std::vector<FSTMutationBatch *>)createBatches:(int)number {
  321. std::vector<FSTMutationBatch *> batches;
  322. for (int i = 0; i < number; i++) {
  323. FSTMutationBatch *batch = [self addMutationBatch];
  324. batches.push_back(batch);
  325. }
  326. return batches;
  327. }
  328. /** Returns the number of mutation batches in the mutation queue. */
  329. - (size_t)batchCount {
  330. return self.mutationQueue->AllMutationBatches().size();
  331. }
  332. /**
  333. * Removes the first n entries from the the given batches and returns them.
  334. *
  335. * @param n The number of batches to remove.
  336. * @param batches The array to mutate, removing entries from it.
  337. * @return A new array containing all the entries that were removed from @a batches.
  338. */
  339. - (std::vector<FSTMutationBatch *>)removeFirstBatches:(size_t)n
  340. inBatches:(std::vector<FSTMutationBatch *> *)batches {
  341. std::vector<FSTMutationBatch *> removed(batches->begin(), batches->begin() + n);
  342. batches->erase(batches->begin(), batches->begin() + n);
  343. for (FSTMutationBatch *batch : removed) {
  344. self.mutationQueue->RemoveMutationBatch(batch);
  345. }
  346. return removed;
  347. }
  348. @end
  349. NS_ASSUME_NONNULL_END