FSTMutationQueueTests.mm 16 KB

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