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