FSTLocalStoreTests.mm 51 KB

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