FSTLocalStoreTests.mm 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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/Source/Local/FSTLocalStore.h"
  17. #import <FirebaseFirestore/FIRTimestamp.h>
  18. #import <XCTest/XCTest.h>
  19. #include <utility>
  20. #include <vector>
  21. #import "Firestore/Source/Core/FSTQuery.h"
  22. #import "Firestore/Source/Local/FSTLocalWriteResult.h"
  23. #import "Firestore/Source/Local/FSTPersistence.h"
  24. #import "Firestore/Source/Local/FSTQueryData.h"
  25. #import "Firestore/Source/Model/FSTDocument.h"
  26. #import "Firestore/Source/Model/FSTDocumentSet.h"
  27. #import "Firestore/Source/Model/FSTMutation.h"
  28. #import "Firestore/Source/Model/FSTMutationBatch.h"
  29. #import "Firestore/Source/Util/FSTClasses.h"
  30. #import "Firestore/Example/Tests/Local/FSTLocalStoreTests.h"
  31. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  32. #import "Firestore/third_party/Immutable/Tests/FSTImmutableSortedDictionary+Testing.h"
  33. #import "Firestore/third_party/Immutable/Tests/FSTImmutableSortedSet+Testing.h"
  34. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  35. #include "Firestore/core/src/firebase/firestore/model/document_map.h"
  36. #include "Firestore/core/src/firebase/firestore/remote/remote_event.h"
  37. #include "Firestore/core/src/firebase/firestore/remote/watch_change.h"
  38. #include "Firestore/core/src/firebase/firestore/util/status.h"
  39. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  40. namespace testutil = firebase::firestore::testutil;
  41. using firebase::firestore::auth::User;
  42. using firebase::firestore::model::DocumentKey;
  43. using firebase::firestore::model::DocumentKeySet;
  44. using firebase::firestore::model::ListenSequenceNumber;
  45. using firebase::firestore::model::DocumentMap;
  46. using firebase::firestore::model::MaybeDocumentMap;
  47. using firebase::firestore::model::SnapshotVersion;
  48. using firebase::firestore::model::TargetId;
  49. using firebase::firestore::remote::RemoteEvent;
  50. using firebase::firestore::remote::TestTargetMetadataProvider;
  51. using firebase::firestore::remote::WatchChangeAggregator;
  52. using firebase::firestore::remote::WatchTargetChange;
  53. using firebase::firestore::remote::WatchTargetChangeState;
  54. using firebase::firestore::util::Status;
  55. static NSArray<FSTDocument *> *docMapToArray(const DocumentMap &docs) {
  56. NSMutableArray<FSTDocument *> *result = [NSMutableArray array];
  57. for (const auto &kv : docs.underlying_map()) {
  58. [result addObject:static_cast<FSTDocument *>(kv.second)];
  59. }
  60. return result;
  61. }
  62. NS_ASSUME_NONNULL_BEGIN
  63. @interface FSTLocalStoreTests ()
  64. @property(nonatomic, strong, readwrite) id<FSTPersistence> localStorePersistence;
  65. @property(nonatomic, strong, readwrite) FSTLocalStore *localStore;
  66. @property(nonatomic, strong, readonly) NSMutableArray<FSTMutationBatch *> *batches;
  67. @property(nonatomic, assign, readwrite) TargetId lastTargetID;
  68. @end
  69. @implementation FSTLocalStoreTests {
  70. MaybeDocumentMap _lastChanges;
  71. }
  72. - (void)setUp {
  73. [super setUp];
  74. if ([self isTestBaseClass]) {
  75. return;
  76. }
  77. id<FSTPersistence> persistence = [self persistence];
  78. self.localStorePersistence = persistence;
  79. self.localStore = [[FSTLocalStore alloc] initWithPersistence:persistence
  80. initialUser:User::Unauthenticated()];
  81. [self.localStore start];
  82. _batches = [NSMutableArray array];
  83. _lastTargetID = 0;
  84. }
  85. - (void)tearDown {
  86. [self.localStorePersistence shutdown];
  87. [super tearDown];
  88. }
  89. - (id<FSTPersistence>)persistence {
  90. @throw FSTAbstractMethodException(); // NOLINT
  91. }
  92. - (BOOL)gcIsEager {
  93. @throw FSTAbstractMethodException(); // NOLINT
  94. }
  95. /**
  96. * Xcode will run tests from any class that extends XCTestCase, but this doesn't work for
  97. * FSTLocalStoreTests since it is incomplete without the implementations supplied by its
  98. * subclasses.
  99. */
  100. - (BOOL)isTestBaseClass {
  101. return [self class] == [FSTLocalStoreTests class];
  102. }
  103. - (void)writeMutation:(FSTMutation *)mutation {
  104. [self writeMutations:{mutation}];
  105. }
  106. - (void)writeMutations:(std::vector<FSTMutation *> &&)mutations {
  107. auto mutationsCopy = mutations;
  108. FSTLocalWriteResult *result = [self.localStore locallyWriteMutations:std::move(mutationsCopy)];
  109. XCTAssertNotNil(result);
  110. [self.batches addObject:[[FSTMutationBatch alloc] initWithBatchID:result.batchID
  111. localWriteTime:[FIRTimestamp timestamp]
  112. mutations:std::move(mutations)]];
  113. _lastChanges = result.changes;
  114. }
  115. - (void)applyRemoteEvent:(const RemoteEvent &)event {
  116. _lastChanges = [self.localStore applyRemoteEvent:event];
  117. }
  118. - (void)notifyLocalViewChanges:(FSTLocalViewChanges *)changes {
  119. [self.localStore notifyLocalViewChanges:@[ changes ]];
  120. }
  121. - (void)acknowledgeMutationWithVersion:(FSTTestSnapshotVersion)documentVersion {
  122. FSTMutationBatch *batch = [self.batches firstObject];
  123. [self.batches removeObjectAtIndex:0];
  124. XCTAssertEqual(batch.mutations.size(), 1, @"Acknowledging more than one mutation not supported.");
  125. SnapshotVersion version = testutil::Version(documentVersion);
  126. FSTMutationResult *mutationResult = [[FSTMutationResult alloc] initWithVersion:version
  127. transformResults:nil];
  128. FSTMutationBatchResult *result = [FSTMutationBatchResult resultWithBatch:batch
  129. commitVersion:version
  130. mutationResults:{mutationResult}
  131. streamToken:nil];
  132. _lastChanges = [self.localStore acknowledgeBatchWithResult:result];
  133. }
  134. - (void)rejectMutation {
  135. FSTMutationBatch *batch = [self.batches firstObject];
  136. [self.batches removeObjectAtIndex:0];
  137. _lastChanges = [self.localStore rejectBatchID:batch.batchID];
  138. }
  139. - (TargetId)allocateQuery:(FSTQuery *)query {
  140. FSTQueryData *queryData = [self.localStore allocateQuery:query];
  141. self.lastTargetID = queryData.targetID;
  142. return queryData.targetID;
  143. }
  144. /** Asserts that the last target ID is the given number. */
  145. #define FSTAssertTargetID(targetID) \
  146. do { \
  147. XCTAssertEqual(self.lastTargetID, targetID); \
  148. } while (0)
  149. /** Asserts that a the lastChanges contain the docs in the given array. */
  150. #define FSTAssertChanged(documents) \
  151. do { \
  152. NSArray<FSTMaybeDocument *> *expected = (documents); \
  153. XCTAssertEqual(_lastChanges.size(), expected.count); \
  154. NSEnumerator<FSTMaybeDocument *> *enumerator = expected.objectEnumerator; \
  155. for (const auto &kv : _lastChanges) { \
  156. FSTMaybeDocument *value = kv.second; \
  157. XCTAssertEqualObjects(value, [enumerator nextObject]); \
  158. } \
  159. _lastChanges = MaybeDocumentMap{}; \
  160. } while (0)
  161. /** Asserts that the given keys were removed. */
  162. #define FSTAssertRemoved(keyPaths) \
  163. do { \
  164. XCTAssertEqual(_lastChanges.size(), keyPaths.count); \
  165. NSEnumerator<NSString *> *keyPathEnumerator = keyPaths.objectEnumerator; \
  166. for (const auto &kv : _lastChanges) { \
  167. const DocumentKey &actualKey = kv.first; \
  168. FSTMaybeDocument *value = kv.second; \
  169. DocumentKey expectedKey = FSTTestDocKey([keyPathEnumerator nextObject]); \
  170. XCTAssertEqual(actualKey, expectedKey); \
  171. XCTAssertTrue([value isKindOfClass:[FSTDeletedDocument class]]); \
  172. } \
  173. _lastChanges = MaybeDocumentMap{}; \
  174. } while (0)
  175. /** Asserts that the given local store contains the given document. */
  176. #define FSTAssertContains(document) \
  177. do { \
  178. FSTMaybeDocument *expected = (document); \
  179. FSTMaybeDocument *actual = [self.localStore readDocument:expected.key]; \
  180. XCTAssertEqualObjects(actual, expected); \
  181. } while (0)
  182. /** Asserts that the given local store does not contain the given document. */
  183. #define FSTAssertNotContains(keyPathString) \
  184. do { \
  185. DocumentKey key = FSTTestDocKey(keyPathString); \
  186. FSTMaybeDocument *actual = [self.localStore readDocument:key]; \
  187. XCTAssertNil(actual); \
  188. } while (0)
  189. - (void)testMutationBatchKeys {
  190. if ([self isTestBaseClass]) return;
  191. FSTMutation *set1 = FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"});
  192. FSTMutation *set2 = FSTTestSetMutation(@"bar/baz", @{@"bar" : @"baz"});
  193. FSTMutationBatch *batch = [[FSTMutationBatch alloc] initWithBatchID:1
  194. localWriteTime:[FIRTimestamp timestamp]
  195. mutations:{set1, set2}];
  196. DocumentKeySet keys = [batch keys];
  197. XCTAssertEqual(keys.size(), 2u);
  198. }
  199. - (void)testHandlesSetMutation {
  200. if ([self isTestBaseClass]) return;
  201. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
  202. FSTAssertChanged(
  203. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
  204. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
  205. [self acknowledgeMutationWithVersion:0];
  206. FSTAssertChanged(
  207. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateCommittedMutations) ]);
  208. if ([self gcIsEager]) {
  209. // Nothing is pinning this anymore, as it has been acknowledged and there are no targets active.
  210. FSTAssertNotContains(@"foo/bar");
  211. } else {
  212. FSTAssertContains(
  213. FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateCommittedMutations));
  214. }
  215. }
  216. - (void)testHandlesSetMutationThenDocument {
  217. if ([self isTestBaseClass]) return;
  218. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
  219. FSTAssertChanged(
  220. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
  221. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
  222. FSTQuery *query = FSTTestQuery("foo");
  223. TargetId targetID = [self allocateQuery:query];
  224. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDoc("foo/bar", 2, @{@"it" : @"changed"},
  225. FSTDocumentStateSynced),
  226. {targetID}, {})];
  227. FSTAssertChanged(
  228. @[ FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
  229. FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
  230. }
  231. - (void)testHandlesAckThenRejectThenRemoteEvent {
  232. if ([self isTestBaseClass]) return;
  233. // Start a query that requires acks to be held.
  234. FSTQuery *query = FSTTestQuery("foo");
  235. TargetId targetID = [self allocateQuery:query];
  236. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
  237. FSTAssertChanged(
  238. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
  239. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
  240. // The last seen version is zero, so this ack must be held.
  241. [self acknowledgeMutationWithVersion:1];
  242. FSTAssertChanged(
  243. @[ FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, FSTDocumentStateCommittedMutations) ]);
  244. // Under eager GC, there is no longer a reference for the document, and it should be
  245. // deleted.
  246. if ([self gcIsEager]) {
  247. FSTAssertNotContains(@"foo/bar");
  248. } else {
  249. FSTAssertContains(
  250. FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, FSTDocumentStateCommittedMutations));
  251. }
  252. [self writeMutation:FSTTestSetMutation(@"bar/baz", @{@"bar" : @"baz"})];
  253. FSTAssertChanged(
  254. @[ FSTTestDoc("bar/baz", 0, @{@"bar" : @"baz"}, FSTDocumentStateLocalMutations) ]);
  255. FSTAssertContains(FSTTestDoc("bar/baz", 0, @{@"bar" : @"baz"}, FSTDocumentStateLocalMutations));
  256. [self rejectMutation];
  257. FSTAssertRemoved(@[ @"bar/baz" ]);
  258. FSTAssertNotContains(@"bar/baz");
  259. [self applyRemoteEvent:FSTTestAddedRemoteEvent(FSTTestDoc("foo/bar", 2, @{@"it" : @"changed"},
  260. FSTDocumentStateSynced),
  261. {targetID})];
  262. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 2, @{@"it" : @"changed"}, FSTDocumentStateSynced) ]);
  263. FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"it" : @"changed"}, FSTDocumentStateSynced));
  264. FSTAssertNotContains(@"bar/baz");
  265. }
  266. - (void)testHandlesDeletedDocumentThenSetMutationThenAck {
  267. if ([self isTestBaseClass]) return;
  268. FSTQuery *query = FSTTestQuery("foo");
  269. TargetId targetID = [self allocateQuery:query];
  270. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDeletedDoc("foo/bar", 2, NO), {targetID},
  271. {})];
  272. FSTAssertRemoved(@[ @"foo/bar" ]);
  273. // Under eager GC, there is no longer a reference for the document, and it should be
  274. // deleted.
  275. if (![self gcIsEager]) {
  276. FSTAssertContains(FSTTestDeletedDoc("foo/bar", 2, NO));
  277. } else {
  278. FSTAssertNotContains(@"foo/bar");
  279. }
  280. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
  281. FSTAssertChanged(
  282. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
  283. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
  284. // Can now remove the target, since we have a mutation pinning the document
  285. [self.localStore releaseQuery:query];
  286. // Verify we didn't lose anything
  287. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
  288. [self acknowledgeMutationWithVersion:3];
  289. FSTAssertChanged(
  290. @[ FSTTestDoc("foo/bar", 3, @{@"foo" : @"bar"}, FSTDocumentStateCommittedMutations) ]);
  291. // It has been acknowledged, and should no longer be retained as there is no target and mutation
  292. if ([self gcIsEager]) {
  293. FSTAssertNotContains(@"foo/bar");
  294. }
  295. }
  296. - (void)testHandlesSetMutationThenDeletedDocument {
  297. if ([self isTestBaseClass]) return;
  298. FSTQuery *query = FSTTestQuery("foo");
  299. TargetId targetID = [self allocateQuery:query];
  300. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
  301. FSTAssertChanged(
  302. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
  303. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDeletedDoc("foo/bar", 2, NO), {targetID},
  304. {})];
  305. FSTAssertChanged(
  306. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
  307. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
  308. }
  309. - (void)testHandlesDocumentThenSetMutationThenAckThenDocument {
  310. if ([self isTestBaseClass]) return;
  311. // Start a query that requires acks to be held.
  312. FSTQuery *query = FSTTestQuery("foo");
  313. TargetId targetID = [self allocateQuery:query];
  314. [self applyRemoteEvent:FSTTestAddedRemoteEvent(
  315. FSTTestDoc("foo/bar", 2, @{@"it" : @"base"}, FSTDocumentStateSynced),
  316. {targetID})];
  317. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 2, @{@"it" : @"base"}, FSTDocumentStateSynced) ]);
  318. FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"it" : @"base"}, FSTDocumentStateSynced));
  319. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
  320. FSTAssertChanged(
  321. @[ FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
  322. FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
  323. [self acknowledgeMutationWithVersion:3];
  324. // we haven't seen the remote event yet, so the write is still held.
  325. FSTAssertChanged(
  326. @[ FSTTestDoc("foo/bar", 3, @{@"foo" : @"bar"}, FSTDocumentStateCommittedMutations) ]);
  327. FSTAssertContains(
  328. FSTTestDoc("foo/bar", 3, @{@"foo" : @"bar"}, FSTDocumentStateCommittedMutations));
  329. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDoc("foo/bar", 3, @{@"it" : @"changed"},
  330. FSTDocumentStateSynced),
  331. {targetID}, {})];
  332. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 3, @{@"it" : @"changed"}, FSTDocumentStateSynced) ]);
  333. FSTAssertContains(FSTTestDoc("foo/bar", 3, @{@"it" : @"changed"}, FSTDocumentStateSynced));
  334. }
  335. - (void)testHandlesPatchWithoutPriorDocument {
  336. if ([self isTestBaseClass]) return;
  337. [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
  338. FSTAssertRemoved(@[ @"foo/bar" ]);
  339. FSTAssertNotContains(@"foo/bar");
  340. [self acknowledgeMutationWithVersion:1];
  341. FSTAssertChanged(@[ FSTTestUnknownDoc("foo/bar", 1) ]);
  342. if ([self gcIsEager]) {
  343. FSTAssertNotContains(@"foo/bar");
  344. } else {
  345. FSTAssertContains(FSTTestUnknownDoc("foo/bar", 1));
  346. }
  347. }
  348. - (void)testHandlesPatchMutationThenDocumentThenAck {
  349. if ([self isTestBaseClass]) return;
  350. [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
  351. FSTAssertRemoved(@[ @"foo/bar" ]);
  352. FSTAssertNotContains(@"foo/bar");
  353. FSTQuery *query = FSTTestQuery("foo");
  354. TargetId targetID = [self allocateQuery:query];
  355. [self applyRemoteEvent:FSTTestAddedRemoteEvent(
  356. FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced),
  357. {targetID})];
  358. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar", @"it" : @"base"},
  359. FSTDocumentStateLocalMutations) ]);
  360. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar", @"it" : @"base"},
  361. FSTDocumentStateLocalMutations));
  362. [self acknowledgeMutationWithVersion:2];
  363. // We still haven't seen the remote events for the patch, so the local changes remain, and there
  364. // are no changes
  365. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar", @"it" : @"base"},
  366. FSTDocumentStateCommittedMutations) ]);
  367. FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar", @"it" : @"base"},
  368. FSTDocumentStateCommittedMutations));
  369. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  370. FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar", @"it" : @"base"},
  371. FSTDocumentStateSynced),
  372. {targetID}, {})];
  373. FSTAssertChanged(
  374. @[ FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar", @"it" : @"base"}, FSTDocumentStateSynced) ]);
  375. FSTAssertContains(
  376. FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar", @"it" : @"base"}, FSTDocumentStateSynced));
  377. }
  378. - (void)testHandlesPatchMutationThenAckThenDocument {
  379. if ([self isTestBaseClass]) return;
  380. [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
  381. FSTAssertRemoved(@[ @"foo/bar" ]);
  382. FSTAssertNotContains(@"foo/bar");
  383. [self acknowledgeMutationWithVersion:1];
  384. FSTAssertChanged(@[ FSTTestUnknownDoc("foo/bar", 1) ]);
  385. // There's no target pinning the doc, and we've ack'd the mutation.
  386. if ([self gcIsEager]) {
  387. FSTAssertNotContains(@"foo/bar");
  388. } else {
  389. FSTAssertContains(FSTTestUnknownDoc("foo/bar", 1));
  390. }
  391. FSTQuery *query = FSTTestQuery("foo");
  392. TargetId targetID = [self allocateQuery:query];
  393. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  394. FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced),
  395. {targetID}, {})];
  396. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced) ]);
  397. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced));
  398. }
  399. - (void)testHandlesDeleteMutationThenAck {
  400. if ([self isTestBaseClass]) return;
  401. [self writeMutation:FSTTestDeleteMutation(@"foo/bar")];
  402. FSTAssertRemoved(@[ @"foo/bar" ]);
  403. FSTAssertContains(FSTTestDeletedDoc("foo/bar", 0, NO));
  404. [self acknowledgeMutationWithVersion:1];
  405. FSTAssertRemoved(@[ @"foo/bar" ]);
  406. // There's no target pinning the doc, and we've ack'd the mutation.
  407. if ([self gcIsEager]) {
  408. FSTAssertNotContains(@"foo/bar");
  409. }
  410. }
  411. - (void)testHandlesDocumentThenDeleteMutationThenAck {
  412. if ([self isTestBaseClass]) return;
  413. FSTQuery *query = FSTTestQuery("foo");
  414. TargetId targetID = [self allocateQuery:query];
  415. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  416. FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced),
  417. {targetID}, {})];
  418. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced) ]);
  419. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced));
  420. [self writeMutation:FSTTestDeleteMutation(@"foo/bar")];
  421. FSTAssertRemoved(@[ @"foo/bar" ]);
  422. FSTAssertContains(FSTTestDeletedDoc("foo/bar", 0, NO));
  423. // Remove the target so only the mutation is pinning the document
  424. [self.localStore releaseQuery:query];
  425. [self acknowledgeMutationWithVersion:2];
  426. FSTAssertRemoved(@[ @"foo/bar" ]);
  427. if ([self gcIsEager]) {
  428. // Neither the target nor the mutation pin the document, it should be gone.
  429. FSTAssertNotContains(@"foo/bar");
  430. }
  431. }
  432. - (void)testHandlesDeleteMutationThenDocumentThenAck {
  433. if ([self isTestBaseClass]) return;
  434. FSTQuery *query = FSTTestQuery("foo");
  435. TargetId targetID = [self allocateQuery:query];
  436. [self writeMutation:FSTTestDeleteMutation(@"foo/bar")];
  437. FSTAssertRemoved(@[ @"foo/bar" ]);
  438. FSTAssertContains(FSTTestDeletedDoc("foo/bar", 0, NO));
  439. // Add the document to a target so it will remain in persistence even when ack'd
  440. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  441. FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced),
  442. {targetID}, {})];
  443. FSTAssertRemoved(@[ @"foo/bar" ]);
  444. FSTAssertContains(FSTTestDeletedDoc("foo/bar", 0, NO));
  445. // Don't need to keep it pinned anymore
  446. [self.localStore releaseQuery:query];
  447. [self acknowledgeMutationWithVersion:2];
  448. FSTAssertRemoved(@[ @"foo/bar" ]);
  449. if ([self gcIsEager]) {
  450. // The doc is not pinned in a target and we've acknowledged the mutation. It shouldn't exist
  451. // anymore.
  452. FSTAssertNotContains(@"foo/bar");
  453. }
  454. }
  455. - (void)testHandlesDocumentThenDeletedDocumentThenDocument {
  456. if ([self isTestBaseClass]) return;
  457. FSTQuery *query = FSTTestQuery("foo");
  458. TargetId targetID = [self allocateQuery:query];
  459. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  460. FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced),
  461. {targetID}, {})];
  462. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced) ]);
  463. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced));
  464. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDeletedDoc("foo/bar", 2, NO), {targetID},
  465. {})];
  466. FSTAssertRemoved(@[ @"foo/bar" ]);
  467. if (![self gcIsEager]) {
  468. FSTAssertContains(FSTTestDeletedDoc("foo/bar", 2, NO));
  469. }
  470. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDoc("foo/bar", 3, @{@"it" : @"changed"},
  471. FSTDocumentStateSynced),
  472. {targetID}, {})];
  473. FSTAssertChanged(@[ FSTTestDoc("foo/bar", 3, @{@"it" : @"changed"}, FSTDocumentStateSynced) ]);
  474. FSTAssertContains(FSTTestDoc("foo/bar", 3, @{@"it" : @"changed"}, FSTDocumentStateSynced));
  475. }
  476. - (void)testHandlesSetMutationThenPatchMutationThenDocumentThenAckThenAck {
  477. if ([self isTestBaseClass]) return;
  478. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"old"})];
  479. FSTAssertChanged(
  480. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"old"}, FSTDocumentStateLocalMutations) ]);
  481. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"old"}, FSTDocumentStateLocalMutations));
  482. [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
  483. FSTAssertChanged(
  484. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
  485. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
  486. FSTQuery *query = FSTTestQuery("foo");
  487. TargetId targetID = [self allocateQuery:query];
  488. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  489. FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced),
  490. {targetID}, {})];
  491. FSTAssertChanged(
  492. @[ FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
  493. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
  494. [self.localStore releaseQuery:query];
  495. [self acknowledgeMutationWithVersion:2]; // delete mutation
  496. FSTAssertChanged(
  497. @[ FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
  498. FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
  499. [self acknowledgeMutationWithVersion:3]; // patch mutation
  500. FSTAssertChanged(
  501. @[ FSTTestDoc("foo/bar", 3, @{@"foo" : @"bar"}, FSTDocumentStateCommittedMutations) ]);
  502. if ([self gcIsEager]) {
  503. // we've ack'd all of the mutations, nothing is keeping this pinned anymore
  504. FSTAssertNotContains(@"foo/bar");
  505. } else {
  506. FSTAssertContains(
  507. FSTTestDoc("foo/bar", 3, @{@"foo" : @"bar"}, FSTDocumentStateCommittedMutations));
  508. }
  509. }
  510. - (void)testHandlesSetMutationAndPatchMutationTogether {
  511. if ([self isTestBaseClass]) return;
  512. [self writeMutations:{
  513. FSTTestSetMutation(@"foo/bar", @{@"foo" : @"old"}),
  514. FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})
  515. }];
  516. FSTAssertChanged(
  517. @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
  518. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
  519. }
  520. - (void)testHandlesSetMutationThenPatchMutationThenReject {
  521. if ([self isTestBaseClass]) return;
  522. if (![self gcIsEager]) return;
  523. [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"old"})];
  524. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"old"}, FSTDocumentStateLocalMutations));
  525. [self acknowledgeMutationWithVersion:1];
  526. FSTAssertNotContains(@"foo/bar");
  527. [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
  528. // A blind patch is not visible in the cache
  529. FSTAssertNotContains(@"foo/bar");
  530. [self rejectMutation];
  531. FSTAssertNotContains(@"foo/bar");
  532. }
  533. - (void)testHandlesSetMutationsAndPatchMutationOfJustOneTogether {
  534. if ([self isTestBaseClass]) return;
  535. [self writeMutations:{
  536. FSTTestSetMutation(@"foo/bar", @{@"foo" : @"old"}),
  537. FSTTestSetMutation(@"bar/baz", @{@"bar" : @"baz"}),
  538. FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})
  539. }];
  540. FSTAssertChanged((@[
  541. FSTTestDoc("bar/baz", 0, @{@"bar" : @"baz"}, FSTDocumentStateLocalMutations),
  542. FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations)
  543. ]));
  544. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
  545. FSTAssertContains(FSTTestDoc("bar/baz", 0, @{@"bar" : @"baz"}, FSTDocumentStateLocalMutations));
  546. }
  547. - (void)testHandlesDeleteMutationThenPatchMutationThenAckThenAck {
  548. if ([self isTestBaseClass]) return;
  549. [self writeMutation:FSTTestDeleteMutation(@"foo/bar")];
  550. FSTAssertRemoved(@[ @"foo/bar" ]);
  551. FSTAssertContains(FSTTestDeletedDoc("foo/bar", 0, NO));
  552. [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
  553. FSTAssertRemoved(@[ @"foo/bar" ]);
  554. FSTAssertContains(FSTTestDeletedDoc("foo/bar", 0, NO));
  555. [self acknowledgeMutationWithVersion:2]; // delete mutation
  556. FSTAssertRemoved(@[ @"foo/bar" ]);
  557. FSTAssertContains(FSTTestDeletedDoc("foo/bar", 2, YES));
  558. [self acknowledgeMutationWithVersion:3]; // patch mutation
  559. FSTAssertChanged(@[ FSTTestUnknownDoc("foo/bar", 3) ]);
  560. if ([self gcIsEager]) {
  561. // There are no more pending mutations, the doc has been dropped
  562. FSTAssertNotContains(@"foo/bar");
  563. } else {
  564. FSTAssertContains(FSTTestUnknownDoc("foo/bar", 3));
  565. }
  566. }
  567. - (void)testCollectsGarbageAfterChangeBatchWithNoTargetIDs {
  568. if ([self isTestBaseClass]) return;
  569. if (![self gcIsEager]) return;
  570. [self applyRemoteEvent:FSTTestUpdateRemoteEventWithLimboTargets(
  571. FSTTestDeletedDoc("foo/bar", 2, NO), {}, {}, {1})];
  572. FSTAssertNotContains(@"foo/bar");
  573. [self applyRemoteEvent:FSTTestUpdateRemoteEventWithLimboTargets(
  574. FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, FSTDocumentStateSynced),
  575. {}, {}, {1})];
  576. FSTAssertNotContains(@"foo/bar");
  577. }
  578. - (void)testCollectsGarbageAfterChangeBatch {
  579. if ([self isTestBaseClass]) return;
  580. if (![self gcIsEager]) return;
  581. FSTQuery *query = FSTTestQuery("foo");
  582. TargetId targetID = [self allocateQuery:query];
  583. [self applyRemoteEvent:FSTTestAddedRemoteEvent(
  584. FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, FSTDocumentStateSynced),
  585. {targetID})];
  586. FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, FSTDocumentStateSynced));
  587. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  588. FSTTestDoc("foo/bar", 2, @{@"foo" : @"baz"}, FSTDocumentStateSynced),
  589. {}, {targetID})];
  590. FSTAssertNotContains(@"foo/bar");
  591. }
  592. - (void)testCollectsGarbageAfterAcknowledgedMutation {
  593. if ([self isTestBaseClass]) return;
  594. if (![self gcIsEager]) return;
  595. FSTQuery *query = FSTTestQuery("foo");
  596. TargetId targetID = [self allocateQuery:query];
  597. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  598. FSTTestDoc("foo/bar", 0, @{@"foo" : @"old"}, FSTDocumentStateSynced),
  599. {targetID}, {})];
  600. [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
  601. // Release the query so that our target count goes back to 0 and we are considered up-to-date.
  602. [self.localStore releaseQuery:query];
  603. [self writeMutation:FSTTestSetMutation(@"foo/bah", @{@"foo" : @"bah"})];
  604. [self writeMutation:FSTTestDeleteMutation(@"foo/baz")];
  605. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
  606. FSTAssertContains(FSTTestDoc("foo/bah", 0, @{@"foo" : @"bah"}, FSTDocumentStateLocalMutations));
  607. FSTAssertContains(FSTTestDeletedDoc("foo/baz", 0, NO));
  608. [self acknowledgeMutationWithVersion:3];
  609. FSTAssertNotContains(@"foo/bar");
  610. FSTAssertContains(FSTTestDoc("foo/bah", 0, @{@"foo" : @"bah"}, FSTDocumentStateLocalMutations));
  611. FSTAssertContains(FSTTestDeletedDoc("foo/baz", 0, NO));
  612. [self acknowledgeMutationWithVersion:4];
  613. FSTAssertNotContains(@"foo/bar");
  614. FSTAssertNotContains(@"foo/bah");
  615. FSTAssertContains(FSTTestDeletedDoc("foo/baz", 0, NO));
  616. [self acknowledgeMutationWithVersion:5];
  617. FSTAssertNotContains(@"foo/bar");
  618. FSTAssertNotContains(@"foo/bah");
  619. FSTAssertNotContains(@"foo/baz");
  620. }
  621. - (void)testCollectsGarbageAfterRejectedMutation {
  622. if ([self isTestBaseClass]) return;
  623. if (![self gcIsEager]) return;
  624. FSTQuery *query = FSTTestQuery("foo");
  625. TargetId targetID = [self allocateQuery:query];
  626. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  627. FSTTestDoc("foo/bar", 0, @{@"foo" : @"old"}, FSTDocumentStateSynced),
  628. {targetID}, {})];
  629. [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
  630. // Release the query so that our target count goes back to 0 and we are considered up-to-date.
  631. [self.localStore releaseQuery:query];
  632. [self writeMutation:FSTTestSetMutation(@"foo/bah", @{@"foo" : @"bah"})];
  633. [self writeMutation:FSTTestDeleteMutation(@"foo/baz")];
  634. FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
  635. FSTAssertContains(FSTTestDoc("foo/bah", 0, @{@"foo" : @"bah"}, FSTDocumentStateLocalMutations));
  636. FSTAssertContains(FSTTestDeletedDoc("foo/baz", 0, NO));
  637. [self rejectMutation]; // patch mutation
  638. FSTAssertNotContains(@"foo/bar");
  639. FSTAssertContains(FSTTestDoc("foo/bah", 0, @{@"foo" : @"bah"}, FSTDocumentStateLocalMutations));
  640. FSTAssertContains(FSTTestDeletedDoc("foo/baz", 0, NO));
  641. [self rejectMutation]; // set mutation
  642. FSTAssertNotContains(@"foo/bar");
  643. FSTAssertNotContains(@"foo/bah");
  644. FSTAssertContains(FSTTestDeletedDoc("foo/baz", 0, NO));
  645. [self rejectMutation]; // delete mutation
  646. FSTAssertNotContains(@"foo/bar");
  647. FSTAssertNotContains(@"foo/bah");
  648. FSTAssertNotContains(@"foo/baz");
  649. }
  650. - (void)testPinsDocumentsInTheLocalView {
  651. if ([self isTestBaseClass]) return;
  652. if (![self gcIsEager]) return;
  653. FSTQuery *query = FSTTestQuery("foo");
  654. TargetId targetID = [self allocateQuery:query];
  655. [self applyRemoteEvent:FSTTestAddedRemoteEvent(
  656. FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, FSTDocumentStateSynced),
  657. {targetID})];
  658. [self writeMutation:FSTTestSetMutation(@"foo/baz", @{@"foo" : @"baz"})];
  659. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, FSTDocumentStateSynced));
  660. FSTAssertContains(FSTTestDoc("foo/baz", 0, @{@"foo" : @"baz"}, FSTDocumentStateLocalMutations));
  661. [self notifyLocalViewChanges:FSTTestViewChanges(targetID, @[ @"foo/bar", @"foo/baz" ], @[])];
  662. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, FSTDocumentStateSynced));
  663. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  664. FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, FSTDocumentStateSynced),
  665. {}, {targetID})];
  666. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  667. FSTTestDoc("foo/baz", 2, @{@"foo" : @"baz"}, FSTDocumentStateSynced),
  668. {targetID}, {})];
  669. FSTAssertContains(FSTTestDoc("foo/baz", 2, @{@"foo" : @"baz"}, FSTDocumentStateLocalMutations));
  670. [self acknowledgeMutationWithVersion:2];
  671. FSTAssertContains(FSTTestDoc("foo/baz", 2, @{@"foo" : @"baz"}, FSTDocumentStateSynced));
  672. FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, FSTDocumentStateSynced));
  673. FSTAssertContains(FSTTestDoc("foo/baz", 2, @{@"foo" : @"baz"}, FSTDocumentStateSynced));
  674. [self notifyLocalViewChanges:FSTTestViewChanges(targetID, @[], @[ @"foo/bar", @"foo/baz" ])];
  675. [self.localStore releaseQuery:query];
  676. FSTAssertNotContains(@"foo/bar");
  677. FSTAssertNotContains(@"foo/baz");
  678. }
  679. - (void)testThrowsAwayDocumentsWithUnknownTargetIDsImmediately {
  680. if ([self isTestBaseClass]) return;
  681. if (![self gcIsEager]) return;
  682. TargetId targetID = 321;
  683. [self applyRemoteEvent:FSTTestUpdateRemoteEventWithLimboTargets(
  684. FSTTestDoc("foo/bar", 1, @{}, FSTDocumentStateSynced), {}, {},
  685. {targetID})];
  686. FSTAssertNotContains(@"foo/bar");
  687. }
  688. - (void)testCanExecuteDocumentQueries {
  689. if ([self isTestBaseClass]) return;
  690. [self.localStore locallyWriteMutations:{
  691. FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"}),
  692. FSTTestSetMutation(@"foo/baz", @{@"foo" : @"baz"}),
  693. FSTTestSetMutation(@"foo/bar/Foo/Bar", @{@"Foo" : @"Bar"})
  694. }];
  695. FSTQuery *query = FSTTestQuery("foo/bar");
  696. DocumentMap docs = [self.localStore executeQuery:query];
  697. XCTAssertEqualObjects(docMapToArray(docs), @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"},
  698. FSTDocumentStateLocalMutations) ]);
  699. }
  700. - (void)testCanExecuteCollectionQueries {
  701. if ([self isTestBaseClass]) return;
  702. [self.localStore locallyWriteMutations:{
  703. FSTTestSetMutation(@"fo/bar", @{@"fo" : @"bar"}),
  704. FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"}),
  705. FSTTestSetMutation(@"foo/baz", @{@"foo" : @"baz"}),
  706. FSTTestSetMutation(@"foo/bar/Foo/Bar", @{@"Foo" : @"Bar"}),
  707. FSTTestSetMutation(@"fooo/blah", @{@"fooo" : @"blah"})
  708. }];
  709. FSTQuery *query = FSTTestQuery("foo");
  710. DocumentMap docs = [self.localStore executeQuery:query];
  711. XCTAssertEqualObjects(
  712. docMapToArray(docs), (@[
  713. FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations),
  714. FSTTestDoc("foo/baz", 0, @{@"foo" : @"baz"}, FSTDocumentStateLocalMutations)
  715. ]));
  716. }
  717. - (void)testCanExecuteMixedCollectionQueries {
  718. if ([self isTestBaseClass]) return;
  719. FSTQuery *query = FSTTestQuery("foo");
  720. [self allocateQuery:query];
  721. FSTAssertTargetID(2);
  722. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  723. FSTTestDoc("foo/baz", 10, @{@"a" : @"b"}, FSTDocumentStateSynced), {2},
  724. {})];
  725. [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
  726. FSTTestDoc("foo/bar", 20, @{@"a" : @"b"}, FSTDocumentStateSynced), {2},
  727. {})];
  728. [self.localStore locallyWriteMutations:{ FSTTestSetMutation(@"foo/bonk", @{@"a" : @"b"}) }];
  729. DocumentMap docs = [self.localStore executeQuery:query];
  730. XCTAssertEqualObjects(docMapToArray(docs), (@[
  731. FSTTestDoc("foo/bar", 20, @{@"a" : @"b"}, FSTDocumentStateSynced),
  732. FSTTestDoc("foo/baz", 10, @{@"a" : @"b"}, FSTDocumentStateSynced),
  733. FSTTestDoc("foo/bonk", 0, @{@"a" : @"b"}, FSTDocumentStateLocalMutations)
  734. ]));
  735. }
  736. - (void)testPersistsResumeTokens {
  737. if ([self isTestBaseClass]) return;
  738. // This test only works in the absence of the FSTEagerGarbageCollector.
  739. if ([self gcIsEager]) return;
  740. FSTQuery *query = FSTTestQuery("foo/bar");
  741. FSTQueryData *queryData = [self.localStore allocateQuery:query];
  742. ListenSequenceNumber initialSequenceNumber = queryData.sequenceNumber;
  743. TargetId targetID = queryData.targetID;
  744. NSData *resumeToken = FSTTestResumeTokenFromSnapshotVersion(1000);
  745. WatchTargetChange watchChange{WatchTargetChangeState::Current, {targetID}, resumeToken};
  746. auto metadataProvider = TestTargetMetadataProvider::CreateSingleResultProvider(
  747. testutil::Key("foo/bar"), std::vector<TargetId>{targetID});
  748. WatchChangeAggregator aggregator{&metadataProvider};
  749. aggregator.HandleTargetChange(watchChange);
  750. RemoteEvent remoteEvent = aggregator.CreateRemoteEvent(testutil::Version(1000));
  751. [self applyRemoteEvent:remoteEvent];
  752. // Stop listening so that the query should become inactive (but persistent)
  753. [self.localStore releaseQuery:query];
  754. // Should come back with the same resume token
  755. FSTQueryData *queryData2 = [self.localStore allocateQuery:query];
  756. XCTAssertEqualObjects(queryData2.resumeToken, resumeToken);
  757. // The sequence number should have been bumped when we saved the new resume token.
  758. ListenSequenceNumber newSequenceNumber = queryData2.sequenceNumber;
  759. XCTAssertGreaterThan(newSequenceNumber, initialSequenceNumber);
  760. }
  761. - (void)testRemoteDocumentKeysForTarget {
  762. if ([self isTestBaseClass]) return;
  763. FSTQuery *query = FSTTestQuery("foo");
  764. [self allocateQuery:query];
  765. FSTAssertTargetID(2);
  766. [self
  767. applyRemoteEvent:FSTTestAddedRemoteEvent(
  768. FSTTestDoc("foo/baz", 10, @{@"a" : @"b"}, FSTDocumentStateSynced), {2})];
  769. [self
  770. applyRemoteEvent:FSTTestAddedRemoteEvent(
  771. FSTTestDoc("foo/bar", 20, @{@"a" : @"b"}, FSTDocumentStateSynced), {2})];
  772. [self.localStore locallyWriteMutations:{ FSTTestSetMutation(@"foo/bonk", @{@"a" : @"b"}) }];
  773. DocumentKeySet keys = [self.localStore remoteDocumentKeysForTarget:2];
  774. DocumentKeySet expected{testutil::Key("foo/bar"), testutil::Key("foo/baz")};
  775. XCTAssertEqual(keys, expected);
  776. keys = [self.localStore remoteDocumentKeysForTarget:2];
  777. XCTAssertEqual(keys, (DocumentKeySet{testutil::Key("foo/bar"), testutil::Key("foo/baz")}));
  778. }
  779. @end
  780. NS_ASSUME_NONNULL_END