FSTLocalStoreTests.mm 51 KB

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