FSTMutationQueueTests.mm 15 KB

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