FSTMutationQueueTests.mm 15 KB

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