| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165 |
- /*
- * Copyright 2017 Google
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #import "Firestore/Source/Local/FSTLocalStore.h"
- #import <FirebaseFirestore/FIRTimestamp.h>
- #import <XCTest/XCTest.h>
- #include <utility>
- #include <vector>
- #import "Firestore/Source/API/FIRFieldValue+Internal.h"
- #import "Firestore/Source/Core/FSTQuery.h"
- #import "Firestore/Source/Local/FSTLocalWriteResult.h"
- #import "Firestore/Source/Local/FSTPersistence.h"
- #import "Firestore/Source/Local/FSTQueryData.h"
- #import "Firestore/Source/Model/FSTDocument.h"
- #import "Firestore/Source/Model/FSTMutation.h"
- #import "Firestore/Source/Model/FSTMutationBatch.h"
- #import "Firestore/Source/Util/FSTClasses.h"
- #import "Firestore/Example/Tests/Local/FSTLocalStoreTests.h"
- #import "Firestore/Example/Tests/Util/FSTHelpers.h"
- #import "Firestore/third_party/Immutable/Tests/FSTImmutableSortedDictionary+Testing.h"
- #import "Firestore/third_party/Immutable/Tests/FSTImmutableSortedSet+Testing.h"
- #include "Firestore/core/src/firebase/firestore/auth/user.h"
- #include "Firestore/core/src/firebase/firestore/model/document_map.h"
- #include "Firestore/core/src/firebase/firestore/model/document_set.h"
- #include "Firestore/core/src/firebase/firestore/remote/remote_event.h"
- #include "Firestore/core/src/firebase/firestore/remote/watch_change.h"
- #include "Firestore/core/src/firebase/firestore/util/status.h"
- #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
- namespace testutil = firebase::firestore::testutil;
- using firebase::firestore::auth::User;
- using firebase::firestore::model::DocumentKey;
- using firebase::firestore::model::DocumentKeySet;
- using firebase::firestore::model::ListenSequenceNumber;
- using firebase::firestore::model::DocumentMap;
- using firebase::firestore::model::MaybeDocumentMap;
- using firebase::firestore::model::SnapshotVersion;
- using firebase::firestore::model::TargetId;
- using firebase::firestore::remote::RemoteEvent;
- using firebase::firestore::remote::TestTargetMetadataProvider;
- using firebase::firestore::remote::WatchChangeAggregator;
- using firebase::firestore::remote::WatchTargetChange;
- using firebase::firestore::remote::WatchTargetChangeState;
- using firebase::firestore::util::Status;
- static NSArray<FSTDocument *> *docMapToArray(const DocumentMap &docs) {
- NSMutableArray<FSTDocument *> *result = [NSMutableArray array];
- for (const auto &kv : docs.underlying_map()) {
- [result addObject:static_cast<FSTDocument *>(kv.second)];
- }
- return result;
- }
- NS_ASSUME_NONNULL_BEGIN
- @interface FSTLocalStoreTests ()
- @property(nonatomic, strong, readwrite) id<FSTPersistence> localStorePersistence;
- @property(nonatomic, strong, readwrite) FSTLocalStore *localStore;
- @property(nonatomic, strong, readonly) NSMutableArray<FSTMutationBatch *> *batches;
- @property(nonatomic, assign, readwrite) TargetId lastTargetID;
- @end
- @implementation FSTLocalStoreTests {
- MaybeDocumentMap _lastChanges;
- }
- - (void)setUp {
- [super setUp];
- if ([self isTestBaseClass]) {
- return;
- }
- id<FSTPersistence> persistence = [self persistence];
- self.localStorePersistence = persistence;
- self.localStore = [[FSTLocalStore alloc] initWithPersistence:persistence
- initialUser:User::Unauthenticated()];
- [self.localStore start];
- _batches = [NSMutableArray array];
- _lastTargetID = 0;
- }
- - (void)tearDown {
- [self.localStorePersistence shutdown];
- [super tearDown];
- }
- - (id<FSTPersistence>)persistence {
- @throw FSTAbstractMethodException(); // NOLINT
- }
- - (BOOL)gcIsEager {
- @throw FSTAbstractMethodException(); // NOLINT
- }
- /**
- * Xcode will run tests from any class that extends XCTestCase, but this doesn't work for
- * FSTLocalStoreTests since it is incomplete without the implementations supplied by its
- * subclasses.
- */
- - (BOOL)isTestBaseClass {
- return [self class] == [FSTLocalStoreTests class];
- }
- - (void)writeMutation:(FSTMutation *)mutation {
- [self writeMutations:{mutation}];
- }
- - (void)writeMutations:(std::vector<FSTMutation *> &&)mutations {
- auto mutationsCopy = mutations;
- FSTLocalWriteResult *result = [self.localStore locallyWriteMutations:std::move(mutationsCopy)];
- XCTAssertNotNil(result);
- [self.batches addObject:[[FSTMutationBatch alloc] initWithBatchID:result.batchID
- localWriteTime:[FIRTimestamp timestamp]
- baseMutations:{}
- mutations:std::move(mutations)]];
- _lastChanges = result.changes;
- }
- - (void)applyRemoteEvent:(const RemoteEvent &)event {
- _lastChanges = [self.localStore applyRemoteEvent:event];
- }
- - (void)notifyLocalViewChanges:(FSTLocalViewChanges *)changes {
- [self.localStore notifyLocalViewChanges:@[ changes ]];
- }
- - (void)acknowledgeMutationWithVersion:(FSTTestSnapshotVersion)documentVersion
- transformResult:(id _Nullable)transformResult {
- FSTMutationBatch *batch = [self.batches firstObject];
- [self.batches removeObjectAtIndex:0];
- XCTAssertEqual(batch.mutations.size(), 1, @"Acknowledging more than one mutation not supported.");
- SnapshotVersion version = testutil::Version(documentVersion);
- FSTMutationResult *mutationResult = [[FSTMutationResult alloc]
- initWithVersion:version
- transformResults:transformResult != nil ? @[ FSTTestFieldValue(transformResult) ] : nil];
- FSTMutationBatchResult *result = [FSTMutationBatchResult resultWithBatch:batch
- commitVersion:version
- mutationResults:{mutationResult}
- streamToken:nil];
- _lastChanges = [self.localStore acknowledgeBatchWithResult:result];
- }
- - (void)acknowledgeMutationWithVersion:(FSTTestSnapshotVersion)documentVersion {
- [self acknowledgeMutationWithVersion:documentVersion transformResult:nil];
- }
- - (void)rejectMutation {
- FSTMutationBatch *batch = [self.batches firstObject];
- [self.batches removeObjectAtIndex:0];
- _lastChanges = [self.localStore rejectBatchID:batch.batchID];
- }
- - (TargetId)allocateQuery:(FSTQuery *)query {
- FSTQueryData *queryData = [self.localStore allocateQuery:query];
- self.lastTargetID = queryData.targetID;
- return queryData.targetID;
- }
- /** Asserts that the last target ID is the given number. */
- #define FSTAssertTargetID(targetID) \
- do { \
- XCTAssertEqual(self.lastTargetID, targetID); \
- } while (0)
- /** Asserts that a the lastChanges contain the docs in the given array. */
- #define FSTAssertChanged(documents) \
- do { \
- NSArray<FSTMaybeDocument *> *expected = (documents); \
- XCTAssertEqual(_lastChanges.size(), expected.count); \
- NSEnumerator<FSTMaybeDocument *> *enumerator = expected.objectEnumerator; \
- for (const auto &kv : _lastChanges) { \
- FSTMaybeDocument *value = kv.second; \
- XCTAssertEqualObjects(value, [enumerator nextObject]); \
- } \
- _lastChanges = MaybeDocumentMap{}; \
- } while (0)
- /** Asserts that the given keys were removed. */
- #define FSTAssertRemoved(keyPaths) \
- do { \
- XCTAssertEqual(_lastChanges.size(), keyPaths.count); \
- NSEnumerator<NSString *> *keyPathEnumerator = keyPaths.objectEnumerator; \
- for (const auto &kv : _lastChanges) { \
- const DocumentKey &actualKey = kv.first; \
- FSTMaybeDocument *value = kv.second; \
- DocumentKey expectedKey = FSTTestDocKey([keyPathEnumerator nextObject]); \
- XCTAssertEqual(actualKey, expectedKey); \
- XCTAssertTrue([value isKindOfClass:[FSTDeletedDocument class]]); \
- } \
- _lastChanges = MaybeDocumentMap{}; \
- } while (0)
- /** Asserts that the given local store contains the given document. */
- #define FSTAssertContains(document) \
- do { \
- FSTMaybeDocument *expected = (document); \
- FSTMaybeDocument *actual = [self.localStore readDocument:expected.key]; \
- XCTAssertEqualObjects(actual, expected); \
- } while (0)
- /** Asserts that the given local store does not contain the given document. */
- #define FSTAssertNotContains(keyPathString) \
- do { \
- DocumentKey key = FSTTestDocKey(keyPathString); \
- FSTMaybeDocument *actual = [self.localStore readDocument:key]; \
- XCTAssertNil(actual); \
- } while (0)
- - (void)testMutationBatchKeys {
- if ([self isTestBaseClass]) return;
- FSTMutation *base = FSTTestSetMutation(@"foo/ignore", @{@"foo" : @"bar"});
- FSTMutation *set1 = FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"});
- FSTMutation *set2 = FSTTestSetMutation(@"bar/baz", @{@"bar" : @"baz"});
- FSTMutationBatch *batch = [[FSTMutationBatch alloc] initWithBatchID:1
- localWriteTime:[FIRTimestamp timestamp]
- baseMutations:{base}
- mutations:{set1, set2}];
- DocumentKeySet keys = [batch keys];
- XCTAssertEqual(keys.size(), 2u);
- }
- - (void)testHandlesSetMutation {
- if ([self isTestBaseClass]) return;
- [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
- [self acknowledgeMutationWithVersion:0];
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateCommittedMutations) ]);
- if ([self gcIsEager]) {
- // Nothing is pinning this anymore, as it has been acknowledged and there are no targets active.
- FSTAssertNotContains(@"foo/bar");
- } else {
- FSTAssertContains(
- FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateCommittedMutations));
- }
- }
- - (void)testHandlesSetMutationThenDocument {
- if ([self isTestBaseClass]) return;
- [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
- FSTQuery *query = FSTTestQuery("foo");
- TargetId targetID = [self allocateQuery:query];
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDoc("foo/bar", 2, @{@"it" : @"changed"},
- FSTDocumentStateSynced),
- {targetID}, {})];
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
- }
- - (void)testHandlesAckThenRejectThenRemoteEvent {
- if ([self isTestBaseClass]) return;
- // Start a query that requires acks to be held.
- FSTQuery *query = FSTTestQuery("foo");
- TargetId targetID = [self allocateQuery:query];
- [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
- // The last seen version is zero, so this ack must be held.
- [self acknowledgeMutationWithVersion:1];
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, FSTDocumentStateCommittedMutations) ]);
- // Under eager GC, there is no longer a reference for the document, and it should be
- // deleted.
- if ([self gcIsEager]) {
- FSTAssertNotContains(@"foo/bar");
- } else {
- FSTAssertContains(
- FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, FSTDocumentStateCommittedMutations));
- }
- [self writeMutation:FSTTestSetMutation(@"bar/baz", @{@"bar" : @"baz"})];
- FSTAssertChanged(
- @[ FSTTestDoc("bar/baz", 0, @{@"bar" : @"baz"}, FSTDocumentStateLocalMutations) ]);
- FSTAssertContains(FSTTestDoc("bar/baz", 0, @{@"bar" : @"baz"}, FSTDocumentStateLocalMutations));
- [self rejectMutation];
- FSTAssertRemoved(@[ @"bar/baz" ]);
- FSTAssertNotContains(@"bar/baz");
- [self applyRemoteEvent:FSTTestAddedRemoteEvent(FSTTestDoc("foo/bar", 2, @{@"it" : @"changed"},
- FSTDocumentStateSynced),
- {targetID})];
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 2, @{@"it" : @"changed"}, FSTDocumentStateSynced) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"it" : @"changed"}, FSTDocumentStateSynced));
- FSTAssertNotContains(@"bar/baz");
- }
- - (void)testHandlesDeletedDocumentThenSetMutationThenAck {
- if ([self isTestBaseClass]) return;
- FSTQuery *query = FSTTestQuery("foo");
- TargetId targetID = [self allocateQuery:query];
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDeletedDoc("foo/bar", 2, NO), {targetID},
- {})];
- FSTAssertRemoved(@[ @"foo/bar" ]);
- // Under eager GC, there is no longer a reference for the document, and it should be
- // deleted.
- if (![self gcIsEager]) {
- FSTAssertContains(FSTTestDeletedDoc("foo/bar", 2, NO));
- } else {
- FSTAssertNotContains(@"foo/bar");
- }
- [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
- // Can now remove the target, since we have a mutation pinning the document
- [self.localStore releaseQuery:query];
- // Verify we didn't lose anything
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
- [self acknowledgeMutationWithVersion:3];
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 3, @{@"foo" : @"bar"}, FSTDocumentStateCommittedMutations) ]);
- // It has been acknowledged, and should no longer be retained as there is no target and mutation
- if ([self gcIsEager]) {
- FSTAssertNotContains(@"foo/bar");
- }
- }
- - (void)testHandlesSetMutationThenDeletedDocument {
- if ([self isTestBaseClass]) return;
- FSTQuery *query = FSTTestQuery("foo");
- TargetId targetID = [self allocateQuery:query];
- [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDeletedDoc("foo/bar", 2, NO), {targetID},
- {})];
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
- }
- - (void)testHandlesDocumentThenSetMutationThenAckThenDocument {
- if ([self isTestBaseClass]) return;
- // Start a query that requires acks to be held.
- FSTQuery *query = FSTTestQuery("foo");
- TargetId targetID = [self allocateQuery:query];
- [self applyRemoteEvent:FSTTestAddedRemoteEvent(
- FSTTestDoc("foo/bar", 2, @{@"it" : @"base"}, FSTDocumentStateSynced),
- {targetID})];
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 2, @{@"it" : @"base"}, FSTDocumentStateSynced) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"it" : @"base"}, FSTDocumentStateSynced));
- [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
- [self acknowledgeMutationWithVersion:3];
- // we haven't seen the remote event yet, so the write is still held.
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 3, @{@"foo" : @"bar"}, FSTDocumentStateCommittedMutations) ]);
- FSTAssertContains(
- FSTTestDoc("foo/bar", 3, @{@"foo" : @"bar"}, FSTDocumentStateCommittedMutations));
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDoc("foo/bar", 3, @{@"it" : @"changed"},
- FSTDocumentStateSynced),
- {targetID}, {})];
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 3, @{@"it" : @"changed"}, FSTDocumentStateSynced) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 3, @{@"it" : @"changed"}, FSTDocumentStateSynced));
- }
- - (void)testHandlesPatchWithoutPriorDocument {
- if ([self isTestBaseClass]) return;
- [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
- FSTAssertRemoved(@[ @"foo/bar" ]);
- FSTAssertNotContains(@"foo/bar");
- [self acknowledgeMutationWithVersion:1];
- FSTAssertChanged(@[ FSTTestUnknownDoc("foo/bar", 1) ]);
- if ([self gcIsEager]) {
- FSTAssertNotContains(@"foo/bar");
- } else {
- FSTAssertContains(FSTTestUnknownDoc("foo/bar", 1));
- }
- }
- - (void)testHandlesPatchMutationThenDocumentThenAck {
- if ([self isTestBaseClass]) return;
- [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
- FSTAssertRemoved(@[ @"foo/bar" ]);
- FSTAssertNotContains(@"foo/bar");
- FSTQuery *query = FSTTestQuery("foo");
- TargetId targetID = [self allocateQuery:query];
- [self applyRemoteEvent:FSTTestAddedRemoteEvent(
- FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced),
- {targetID})];
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar", @"it" : @"base"},
- FSTDocumentStateLocalMutations) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar", @"it" : @"base"},
- FSTDocumentStateLocalMutations));
- [self acknowledgeMutationWithVersion:2];
- // We still haven't seen the remote events for the patch, so the local changes remain, and there
- // are no changes
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar", @"it" : @"base"},
- FSTDocumentStateCommittedMutations) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar", @"it" : @"base"},
- FSTDocumentStateCommittedMutations));
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
- FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar", @"it" : @"base"},
- FSTDocumentStateSynced),
- {targetID}, {})];
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar", @"it" : @"base"}, FSTDocumentStateSynced) ]);
- FSTAssertContains(
- FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar", @"it" : @"base"}, FSTDocumentStateSynced));
- }
- - (void)testHandlesPatchMutationThenAckThenDocument {
- if ([self isTestBaseClass]) return;
- [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
- FSTAssertRemoved(@[ @"foo/bar" ]);
- FSTAssertNotContains(@"foo/bar");
- [self acknowledgeMutationWithVersion:1];
- FSTAssertChanged(@[ FSTTestUnknownDoc("foo/bar", 1) ]);
- // There's no target pinning the doc, and we've ack'd the mutation.
- if ([self gcIsEager]) {
- FSTAssertNotContains(@"foo/bar");
- } else {
- FSTAssertContains(FSTTestUnknownDoc("foo/bar", 1));
- }
- FSTQuery *query = FSTTestQuery("foo");
- TargetId targetID = [self allocateQuery:query];
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
- FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced),
- {targetID}, {})];
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced));
- }
- - (void)testHandlesDeleteMutationThenAck {
- if ([self isTestBaseClass]) return;
- [self writeMutation:FSTTestDeleteMutation(@"foo/bar")];
- FSTAssertRemoved(@[ @"foo/bar" ]);
- FSTAssertContains(FSTTestDeletedDoc("foo/bar", 0, NO));
- [self acknowledgeMutationWithVersion:1];
- FSTAssertRemoved(@[ @"foo/bar" ]);
- // There's no target pinning the doc, and we've ack'd the mutation.
- if ([self gcIsEager]) {
- FSTAssertNotContains(@"foo/bar");
- }
- }
- - (void)testHandlesDocumentThenDeleteMutationThenAck {
- if ([self isTestBaseClass]) return;
- FSTQuery *query = FSTTestQuery("foo");
- TargetId targetID = [self allocateQuery:query];
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
- FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced),
- {targetID}, {})];
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced));
- [self writeMutation:FSTTestDeleteMutation(@"foo/bar")];
- FSTAssertRemoved(@[ @"foo/bar" ]);
- FSTAssertContains(FSTTestDeletedDoc("foo/bar", 0, NO));
- // Remove the target so only the mutation is pinning the document
- [self.localStore releaseQuery:query];
- [self acknowledgeMutationWithVersion:2];
- FSTAssertRemoved(@[ @"foo/bar" ]);
- if ([self gcIsEager]) {
- // Neither the target nor the mutation pin the document, it should be gone.
- FSTAssertNotContains(@"foo/bar");
- }
- }
- - (void)testHandlesDeleteMutationThenDocumentThenAck {
- if ([self isTestBaseClass]) return;
- FSTQuery *query = FSTTestQuery("foo");
- TargetId targetID = [self allocateQuery:query];
- [self writeMutation:FSTTestDeleteMutation(@"foo/bar")];
- FSTAssertRemoved(@[ @"foo/bar" ]);
- FSTAssertContains(FSTTestDeletedDoc("foo/bar", 0, NO));
- // Add the document to a target so it will remain in persistence even when ack'd
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
- FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced),
- {targetID}, {})];
- FSTAssertRemoved(@[ @"foo/bar" ]);
- FSTAssertContains(FSTTestDeletedDoc("foo/bar", 0, NO));
- // Don't need to keep it pinned anymore
- [self.localStore releaseQuery:query];
- [self acknowledgeMutationWithVersion:2];
- FSTAssertRemoved(@[ @"foo/bar" ]);
- if ([self gcIsEager]) {
- // The doc is not pinned in a target and we've acknowledged the mutation. It shouldn't exist
- // anymore.
- FSTAssertNotContains(@"foo/bar");
- }
- }
- - (void)testHandlesDocumentThenDeletedDocumentThenDocument {
- if ([self isTestBaseClass]) return;
- FSTQuery *query = FSTTestQuery("foo");
- TargetId targetID = [self allocateQuery:query];
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
- FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced),
- {targetID}, {})];
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced));
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDeletedDoc("foo/bar", 2, NO), {targetID},
- {})];
- FSTAssertRemoved(@[ @"foo/bar" ]);
- if (![self gcIsEager]) {
- FSTAssertContains(FSTTestDeletedDoc("foo/bar", 2, NO));
- }
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDoc("foo/bar", 3, @{@"it" : @"changed"},
- FSTDocumentStateSynced),
- {targetID}, {})];
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 3, @{@"it" : @"changed"}, FSTDocumentStateSynced) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 3, @{@"it" : @"changed"}, FSTDocumentStateSynced));
- }
- - (void)testHandlesSetMutationThenPatchMutationThenDocumentThenAckThenAck {
- if ([self isTestBaseClass]) return;
- [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"old"})];
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"old"}, FSTDocumentStateLocalMutations) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"old"}, FSTDocumentStateLocalMutations));
- [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
- FSTQuery *query = FSTTestQuery("foo");
- TargetId targetID = [self allocateQuery:query];
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
- FSTTestDoc("foo/bar", 1, @{@"it" : @"base"}, FSTDocumentStateSynced),
- {targetID}, {})];
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
- [self.localStore releaseQuery:query];
- [self acknowledgeMutationWithVersion:2]; // delete mutation
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
- [self acknowledgeMutationWithVersion:3]; // patch mutation
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 3, @{@"foo" : @"bar"}, FSTDocumentStateCommittedMutations) ]);
- if ([self gcIsEager]) {
- // we've ack'd all of the mutations, nothing is keeping this pinned anymore
- FSTAssertNotContains(@"foo/bar");
- } else {
- FSTAssertContains(
- FSTTestDoc("foo/bar", 3, @{@"foo" : @"bar"}, FSTDocumentStateCommittedMutations));
- }
- }
- - (void)testHandlesSetMutationAndPatchMutationTogether {
- if ([self isTestBaseClass]) return;
- [self writeMutations:{
- FSTTestSetMutation(@"foo/bar", @{@"foo" : @"old"}),
- FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})
- }];
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
- }
- - (void)testHandlesSetMutationThenPatchMutationThenReject {
- if ([self isTestBaseClass]) return;
- if (![self gcIsEager]) return;
- [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"old"})];
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"old"}, FSTDocumentStateLocalMutations));
- [self acknowledgeMutationWithVersion:1];
- FSTAssertNotContains(@"foo/bar");
- [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
- // A blind patch is not visible in the cache
- FSTAssertNotContains(@"foo/bar");
- [self rejectMutation];
- FSTAssertNotContains(@"foo/bar");
- }
- - (void)testHandlesSetMutationsAndPatchMutationOfJustOneTogether {
- if ([self isTestBaseClass]) return;
- [self writeMutations:{
- FSTTestSetMutation(@"foo/bar", @{@"foo" : @"old"}),
- FSTTestSetMutation(@"bar/baz", @{@"bar" : @"baz"}),
- FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})
- }];
- FSTAssertChanged((@[
- FSTTestDoc("bar/baz", 0, @{@"bar" : @"baz"}, FSTDocumentStateLocalMutations),
- FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations)
- ]));
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
- FSTAssertContains(FSTTestDoc("bar/baz", 0, @{@"bar" : @"baz"}, FSTDocumentStateLocalMutations));
- }
- - (void)testHandlesDeleteMutationThenPatchMutationThenAckThenAck {
- if ([self isTestBaseClass]) return;
- [self writeMutation:FSTTestDeleteMutation(@"foo/bar")];
- FSTAssertRemoved(@[ @"foo/bar" ]);
- FSTAssertContains(FSTTestDeletedDoc("foo/bar", 0, NO));
- [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
- FSTAssertRemoved(@[ @"foo/bar" ]);
- FSTAssertContains(FSTTestDeletedDoc("foo/bar", 0, NO));
- [self acknowledgeMutationWithVersion:2]; // delete mutation
- FSTAssertRemoved(@[ @"foo/bar" ]);
- FSTAssertContains(FSTTestDeletedDoc("foo/bar", 2, YES));
- [self acknowledgeMutationWithVersion:3]; // patch mutation
- FSTAssertChanged(@[ FSTTestUnknownDoc("foo/bar", 3) ]);
- if ([self gcIsEager]) {
- // There are no more pending mutations, the doc has been dropped
- FSTAssertNotContains(@"foo/bar");
- } else {
- FSTAssertContains(FSTTestUnknownDoc("foo/bar", 3));
- }
- }
- - (void)testCollectsGarbageAfterChangeBatchWithNoTargetIDs {
- if ([self isTestBaseClass]) return;
- if (![self gcIsEager]) return;
- [self applyRemoteEvent:FSTTestUpdateRemoteEventWithLimboTargets(
- FSTTestDeletedDoc("foo/bar", 2, NO), {}, {}, {1})];
- FSTAssertNotContains(@"foo/bar");
- [self applyRemoteEvent:FSTTestUpdateRemoteEventWithLimboTargets(
- FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, FSTDocumentStateSynced),
- {}, {}, {1})];
- FSTAssertNotContains(@"foo/bar");
- }
- - (void)testCollectsGarbageAfterChangeBatch {
- if ([self isTestBaseClass]) return;
- if (![self gcIsEager]) return;
- FSTQuery *query = FSTTestQuery("foo");
- TargetId targetID = [self allocateQuery:query];
- [self applyRemoteEvent:FSTTestAddedRemoteEvent(
- FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, FSTDocumentStateSynced),
- {targetID})];
- FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, FSTDocumentStateSynced));
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
- FSTTestDoc("foo/bar", 2, @{@"foo" : @"baz"}, FSTDocumentStateSynced),
- {}, {targetID})];
- FSTAssertNotContains(@"foo/bar");
- }
- - (void)testCollectsGarbageAfterAcknowledgedMutation {
- if ([self isTestBaseClass]) return;
- if (![self gcIsEager]) return;
- FSTQuery *query = FSTTestQuery("foo");
- TargetId targetID = [self allocateQuery:query];
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
- FSTTestDoc("foo/bar", 0, @{@"foo" : @"old"}, FSTDocumentStateSynced),
- {targetID}, {})];
- [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
- // Release the query so that our target count goes back to 0 and we are considered up-to-date.
- [self.localStore releaseQuery:query];
- [self writeMutation:FSTTestSetMutation(@"foo/bah", @{@"foo" : @"bah"})];
- [self writeMutation:FSTTestDeleteMutation(@"foo/baz")];
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
- FSTAssertContains(FSTTestDoc("foo/bah", 0, @{@"foo" : @"bah"}, FSTDocumentStateLocalMutations));
- FSTAssertContains(FSTTestDeletedDoc("foo/baz", 0, NO));
- [self acknowledgeMutationWithVersion:3];
- FSTAssertNotContains(@"foo/bar");
- FSTAssertContains(FSTTestDoc("foo/bah", 0, @{@"foo" : @"bah"}, FSTDocumentStateLocalMutations));
- FSTAssertContains(FSTTestDeletedDoc("foo/baz", 0, NO));
- [self acknowledgeMutationWithVersion:4];
- FSTAssertNotContains(@"foo/bar");
- FSTAssertNotContains(@"foo/bah");
- FSTAssertContains(FSTTestDeletedDoc("foo/baz", 0, NO));
- [self acknowledgeMutationWithVersion:5];
- FSTAssertNotContains(@"foo/bar");
- FSTAssertNotContains(@"foo/bah");
- FSTAssertNotContains(@"foo/baz");
- }
- - (void)testCollectsGarbageAfterRejectedMutation {
- if ([self isTestBaseClass]) return;
- if (![self gcIsEager]) return;
- FSTQuery *query = FSTTestQuery("foo");
- TargetId targetID = [self allocateQuery:query];
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
- FSTTestDoc("foo/bar", 0, @{@"foo" : @"old"}, FSTDocumentStateSynced),
- {targetID}, {})];
- [self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
- // Release the query so that our target count goes back to 0 and we are considered up-to-date.
- [self.localStore releaseQuery:query];
- [self writeMutation:FSTTestSetMutation(@"foo/bah", @{@"foo" : @"bah"})];
- [self writeMutation:FSTTestDeleteMutation(@"foo/baz")];
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations));
- FSTAssertContains(FSTTestDoc("foo/bah", 0, @{@"foo" : @"bah"}, FSTDocumentStateLocalMutations));
- FSTAssertContains(FSTTestDeletedDoc("foo/baz", 0, NO));
- [self rejectMutation]; // patch mutation
- FSTAssertNotContains(@"foo/bar");
- FSTAssertContains(FSTTestDoc("foo/bah", 0, @{@"foo" : @"bah"}, FSTDocumentStateLocalMutations));
- FSTAssertContains(FSTTestDeletedDoc("foo/baz", 0, NO));
- [self rejectMutation]; // set mutation
- FSTAssertNotContains(@"foo/bar");
- FSTAssertNotContains(@"foo/bah");
- FSTAssertContains(FSTTestDeletedDoc("foo/baz", 0, NO));
- [self rejectMutation]; // delete mutation
- FSTAssertNotContains(@"foo/bar");
- FSTAssertNotContains(@"foo/bah");
- FSTAssertNotContains(@"foo/baz");
- }
- - (void)testPinsDocumentsInTheLocalView {
- if ([self isTestBaseClass]) return;
- if (![self gcIsEager]) return;
- FSTQuery *query = FSTTestQuery("foo");
- TargetId targetID = [self allocateQuery:query];
- [self applyRemoteEvent:FSTTestAddedRemoteEvent(
- FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, FSTDocumentStateSynced),
- {targetID})];
- [self writeMutation:FSTTestSetMutation(@"foo/baz", @{@"foo" : @"baz"})];
- FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, FSTDocumentStateSynced));
- FSTAssertContains(FSTTestDoc("foo/baz", 0, @{@"foo" : @"baz"}, FSTDocumentStateLocalMutations));
- [self notifyLocalViewChanges:FSTTestViewChanges(targetID, @[ @"foo/bar", @"foo/baz" ], @[])];
- FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, FSTDocumentStateSynced));
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
- FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, FSTDocumentStateSynced),
- {}, {targetID})];
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
- FSTTestDoc("foo/baz", 2, @{@"foo" : @"baz"}, FSTDocumentStateSynced),
- {targetID}, {})];
- FSTAssertContains(FSTTestDoc("foo/baz", 2, @{@"foo" : @"baz"}, FSTDocumentStateLocalMutations));
- [self acknowledgeMutationWithVersion:2];
- FSTAssertContains(FSTTestDoc("foo/baz", 2, @{@"foo" : @"baz"}, FSTDocumentStateSynced));
- FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, FSTDocumentStateSynced));
- FSTAssertContains(FSTTestDoc("foo/baz", 2, @{@"foo" : @"baz"}, FSTDocumentStateSynced));
- [self notifyLocalViewChanges:FSTTestViewChanges(targetID, @[], @[ @"foo/bar", @"foo/baz" ])];
- [self.localStore releaseQuery:query];
- FSTAssertNotContains(@"foo/bar");
- FSTAssertNotContains(@"foo/baz");
- }
- - (void)testThrowsAwayDocumentsWithUnknownTargetIDsImmediately {
- if ([self isTestBaseClass]) return;
- if (![self gcIsEager]) return;
- TargetId targetID = 321;
- [self applyRemoteEvent:FSTTestUpdateRemoteEventWithLimboTargets(
- FSTTestDoc("foo/bar", 1, @{}, FSTDocumentStateSynced), {}, {},
- {targetID})];
- FSTAssertNotContains(@"foo/bar");
- }
- - (void)testCanExecuteDocumentQueries {
- if ([self isTestBaseClass]) return;
- [self.localStore locallyWriteMutations:{
- FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"}),
- FSTTestSetMutation(@"foo/baz", @{@"foo" : @"baz"}),
- FSTTestSetMutation(@"foo/bar/Foo/Bar", @{@"Foo" : @"Bar"})
- }];
- FSTQuery *query = FSTTestQuery("foo/bar");
- DocumentMap docs = [self.localStore executeQuery:query];
- XCTAssertEqualObjects(docMapToArray(docs), @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"},
- FSTDocumentStateLocalMutations) ]);
- }
- - (void)testCanExecuteCollectionQueries {
- if ([self isTestBaseClass]) return;
- [self.localStore locallyWriteMutations:{
- FSTTestSetMutation(@"fo/bar", @{@"fo" : @"bar"}),
- FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"}),
- FSTTestSetMutation(@"foo/baz", @{@"foo" : @"baz"}),
- FSTTestSetMutation(@"foo/bar/Foo/Bar", @{@"Foo" : @"Bar"}),
- FSTTestSetMutation(@"fooo/blah", @{@"fooo" : @"blah"})
- }];
- FSTQuery *query = FSTTestQuery("foo");
- DocumentMap docs = [self.localStore executeQuery:query];
- XCTAssertEqualObjects(
- docMapToArray(docs), (@[
- FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations),
- FSTTestDoc("foo/baz", 0, @{@"foo" : @"baz"}, FSTDocumentStateLocalMutations)
- ]));
- }
- - (void)testCanExecuteMixedCollectionQueries {
- if ([self isTestBaseClass]) return;
- FSTQuery *query = FSTTestQuery("foo");
- [self allocateQuery:query];
- FSTAssertTargetID(2);
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
- FSTTestDoc("foo/baz", 10, @{@"a" : @"b"}, FSTDocumentStateSynced), {2},
- {})];
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
- FSTTestDoc("foo/bar", 20, @{@"a" : @"b"}, FSTDocumentStateSynced), {2},
- {})];
- [self.localStore locallyWriteMutations:{ FSTTestSetMutation(@"foo/bonk", @{@"a" : @"b"}) }];
- DocumentMap docs = [self.localStore executeQuery:query];
- XCTAssertEqualObjects(docMapToArray(docs), (@[
- FSTTestDoc("foo/bar", 20, @{@"a" : @"b"}, FSTDocumentStateSynced),
- FSTTestDoc("foo/baz", 10, @{@"a" : @"b"}, FSTDocumentStateSynced),
- FSTTestDoc("foo/bonk", 0, @{@"a" : @"b"}, FSTDocumentStateLocalMutations)
- ]));
- }
- - (void)testPersistsResumeTokens {
- if ([self isTestBaseClass]) return;
- // This test only works in the absence of the FSTEagerGarbageCollector.
- if ([self gcIsEager]) return;
- FSTQuery *query = FSTTestQuery("foo/bar");
- FSTQueryData *queryData = [self.localStore allocateQuery:query];
- ListenSequenceNumber initialSequenceNumber = queryData.sequenceNumber;
- TargetId targetID = queryData.targetID;
- NSData *resumeToken = FSTTestResumeTokenFromSnapshotVersion(1000);
- WatchTargetChange watchChange{WatchTargetChangeState::Current, {targetID}, resumeToken};
- auto metadataProvider = TestTargetMetadataProvider::CreateSingleResultProvider(
- testutil::Key("foo/bar"), std::vector<TargetId>{targetID});
- WatchChangeAggregator aggregator{&metadataProvider};
- aggregator.HandleTargetChange(watchChange);
- RemoteEvent remoteEvent = aggregator.CreateRemoteEvent(testutil::Version(1000));
- [self applyRemoteEvent:remoteEvent];
- // Stop listening so that the query should become inactive (but persistent)
- [self.localStore releaseQuery:query];
- // Should come back with the same resume token
- FSTQueryData *queryData2 = [self.localStore allocateQuery:query];
- XCTAssertEqualObjects(queryData2.resumeToken, resumeToken);
- // The sequence number should have been bumped when we saved the new resume token.
- ListenSequenceNumber newSequenceNumber = queryData2.sequenceNumber;
- XCTAssertGreaterThan(newSequenceNumber, initialSequenceNumber);
- }
- - (void)testRemoteDocumentKeysForTarget {
- if ([self isTestBaseClass]) return;
- FSTQuery *query = FSTTestQuery("foo");
- [self allocateQuery:query];
- FSTAssertTargetID(2);
- [self
- applyRemoteEvent:FSTTestAddedRemoteEvent(
- FSTTestDoc("foo/baz", 10, @{@"a" : @"b"}, FSTDocumentStateSynced), {2})];
- [self
- applyRemoteEvent:FSTTestAddedRemoteEvent(
- FSTTestDoc("foo/bar", 20, @{@"a" : @"b"}, FSTDocumentStateSynced), {2})];
- [self.localStore locallyWriteMutations:{ FSTTestSetMutation(@"foo/bonk", @{@"a" : @"b"}) }];
- DocumentKeySet keys = [self.localStore remoteDocumentKeysForTarget:2];
- DocumentKeySet expected{testutil::Key("foo/bar"), testutil::Key("foo/baz")};
- XCTAssertEqual(keys, expected);
- keys = [self.localStore remoteDocumentKeysForTarget:2];
- XCTAssertEqual(keys, (DocumentKeySet{testutil::Key("foo/bar"), testutil::Key("foo/baz")}));
- }
- // TODO(mrschmidt): The FieldValue.increment() field transform tests below would probably be
- // better implemented as spec tests but currently they don't support transforms.
- - (void)testHandlesSetMutationThenTransformMutationThenTransformMutation {
- if ([self isTestBaseClass]) return;
- [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"sum" : @0})];
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"sum" : @0}, FSTDocumentStateLocalMutations));
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 0, @{@"sum" : @0}, FSTDocumentStateLocalMutations) ]);
- [self writeMutation:FSTTestTransformMutation(
- @"foo/bar", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]})];
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"sum" : @1}, FSTDocumentStateLocalMutations));
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 0, @{@"sum" : @1}, FSTDocumentStateLocalMutations) ]);
- [self writeMutation:FSTTestTransformMutation(
- @"foo/bar", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:2]})];
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"sum" : @3}, FSTDocumentStateLocalMutations));
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 0, @{@"sum" : @3}, FSTDocumentStateLocalMutations) ]);
- }
- - (void)testHandlesSetMutationThenAckThenTransformMutationThenAckThenTransformMutation {
- if ([self isTestBaseClass]) return;
- // Since this test doesn't start a listen, Eager GC removes the documents from the cache as
- // soon as the mutation is applied. This creates a lot of special casing in this unit test but
- // does not expand its test coverage.
- if ([self gcIsEager]) return;
- [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"sum" : @0})];
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"sum" : @0}, FSTDocumentStateLocalMutations));
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 0, @{@"sum" : @0}, FSTDocumentStateLocalMutations) ]);
- [self acknowledgeMutationWithVersion:1];
- FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"sum" : @0}, FSTDocumentStateCommittedMutations));
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 1, @{@"sum" : @0}, FSTDocumentStateCommittedMutations) ]);
- [self writeMutation:FSTTestTransformMutation(
- @"foo/bar", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]})];
- FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"sum" : @1}, FSTDocumentStateLocalMutations));
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"sum" : @1}, FSTDocumentStateLocalMutations) ]);
- [self acknowledgeMutationWithVersion:2 transformResult:@1];
- FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"sum" : @1}, FSTDocumentStateCommittedMutations));
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 2, @{@"sum" : @1}, FSTDocumentStateCommittedMutations) ]);
- [self writeMutation:FSTTestTransformMutation(
- @"foo/bar", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:2]})];
- FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"sum" : @3}, FSTDocumentStateLocalMutations));
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 2, @{@"sum" : @3}, FSTDocumentStateLocalMutations) ]);
- }
- - (void)testHandlesSetMutationThenTransformMutationThenRemoteEventThenTransformMutation {
- if ([self isTestBaseClass]) return;
- FSTQuery *query = FSTTestQuery("foo");
- [self allocateQuery:query];
- FSTAssertTargetID(2);
- [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"sum" : @0})];
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"sum" : @0}, FSTDocumentStateLocalMutations));
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 0, @{@"sum" : @0}, FSTDocumentStateLocalMutations) ]);
- [self
- applyRemoteEvent:FSTTestAddedRemoteEvent(
- FSTTestDoc("foo/bar", 1, @{@"sum" : @0}, FSTDocumentStateSynced), {2})];
- [self acknowledgeMutationWithVersion:1];
- FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"sum" : @0}, FSTDocumentStateSynced));
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"sum" : @0}, FSTDocumentStateSynced) ]);
- [self writeMutation:FSTTestTransformMutation(
- @"foo/bar", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]})];
- FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"sum" : @1}, FSTDocumentStateLocalMutations));
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"sum" : @1}, FSTDocumentStateLocalMutations) ]);
- // The value in this remote event gets ignored since we still have a pending transform mutation.
- [self applyRemoteEvent:FSTTestUpdateRemoteEvent(
- FSTTestDoc("foo/bar", 2, @{@"sum" : @0}, FSTDocumentStateSynced), {2},
- {})];
- FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"sum" : @1}, FSTDocumentStateLocalMutations));
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 2, @{@"sum" : @1}, FSTDocumentStateLocalMutations) ]);
- // Add another increment. Note that we still compute the increment based on the local value.
- [self writeMutation:FSTTestTransformMutation(
- @"foo/bar", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:2]})];
- FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"sum" : @3}, FSTDocumentStateLocalMutations));
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 2, @{@"sum" : @3}, FSTDocumentStateLocalMutations) ]);
- [self acknowledgeMutationWithVersion:3 transformResult:@1];
- FSTAssertContains(FSTTestDoc("foo/bar", 3, @{@"sum" : @3}, FSTDocumentStateLocalMutations));
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 3, @{@"sum" : @3}, FSTDocumentStateLocalMutations) ]);
- [self acknowledgeMutationWithVersion:4 transformResult:@1339];
- FSTAssertContains(
- FSTTestDoc("foo/bar", 4, @{@"sum" : @1339}, FSTDocumentStateCommittedMutations));
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 4, @{@"sum" : @1339}, FSTDocumentStateCommittedMutations) ]);
- }
- - (void)testHoldsBackOnlyNonIdempotentTransforms {
- if ([self isTestBaseClass]) return;
- FSTQuery *query = FSTTestQuery("foo");
- [self allocateQuery:query];
- FSTAssertTargetID(2);
- [self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"sum" : @0, @"array_union" : @[]})];
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 0, @{@"sum" : @0, @"array_union" : @[]},
- FSTDocumentStateLocalMutations) ]);
- [self acknowledgeMutationWithVersion:1];
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"sum" : @0, @"array_union" : @[]},
- FSTDocumentStateCommittedMutations) ]);
- [self applyRemoteEvent:FSTTestAddedRemoteEvent(
- FSTTestDoc("foo/bar", 1, @{@"sum" : @0, @"array_union" : @[]},
- FSTDocumentStateSynced),
- {2})];
- FSTAssertChanged(
- @[ FSTTestDoc("foo/bar", 1, @{@"sum" : @0, @"array_union" : @[]}, FSTDocumentStateSynced) ]);
- [self writeMutations:{
- FSTTestTransformMutation(@"foo/bar",
- @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]}),
- FSTTestTransformMutation(
- @"foo/bar",
- @{@"array_union" : [FIRFieldValue fieldValueForArrayUnion:@[ @"foo" ]]})
- }];
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"sum" : @1, @"array_union" : @[ @"foo" ]},
- FSTDocumentStateLocalMutations) ]);
- // The sum transform is not idempotent and the backend's updated value is ignored. The
- // ArrayUnion transform is recomputed and includes the backend value.
- [self
- applyRemoteEvent:FSTTestUpdateRemoteEvent(
- FSTTestDoc("foo/bar", 1, @{@"sum" : @1337, @"array_union" : @[ @"bar" ]},
- FSTDocumentStateSynced),
- {2}, {})];
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"sum" : @1, @"array_union" : @[ @"bar", @"foo" ]},
- FSTDocumentStateLocalMutations) ]);
- }
- - (void)testHandlesMergeMutationWithTransformThenRemoteEvent {
- if ([self isTestBaseClass]) return;
- FSTQuery *query = FSTTestQuery("foo");
- [self allocateQuery:query];
- FSTAssertTargetID(2);
- [self writeMutations:{
- FSTTestPatchMutation("foo/bar", @{}, {firebase::firestore::testutil::Field("sum")}),
- FSTTestTransformMutation(@"foo/bar",
- @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]})
- }];
- FSTAssertContains(FSTTestDoc("foo/bar", 0, @{@"sum" : @1}, FSTDocumentStateLocalMutations));
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 0, @{@"sum" : @1}, FSTDocumentStateLocalMutations) ]);
- [self applyRemoteEvent:FSTTestAddedRemoteEvent(
- FSTTestDoc("foo/bar", 1, @{@"sum" : @1337}, FSTDocumentStateSynced),
- {2})];
- FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"sum" : @1}, FSTDocumentStateLocalMutations));
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"sum" : @1}, FSTDocumentStateLocalMutations) ]);
- }
- - (void)testHandlesPatchMutationWithTransformThenRemoteEvent {
- if ([self isTestBaseClass]) return;
- FSTQuery *query = FSTTestQuery("foo");
- [self allocateQuery:query];
- FSTAssertTargetID(2);
- [self writeMutations:{
- FSTTestPatchMutation("foo/bar", @{}, {}),
- FSTTestTransformMutation(@"foo/bar",
- @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]})
- }];
- FSTAssertNotContains(@"foo/bar");
- FSTAssertChanged(@[ FSTTestDeletedDoc("foo/bar", 0, NO) ]);
- // Note: This test reflects the current behavior, but it may be preferable to replay the
- // mutation once we receive the first value from the remote event.
- [self applyRemoteEvent:FSTTestAddedRemoteEvent(
- FSTTestDoc("foo/bar", 1, @{@"sum" : @1337}, FSTDocumentStateSynced),
- {2})];
- FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"sum" : @1}, FSTDocumentStateLocalMutations));
- FSTAssertChanged(@[ FSTTestDoc("foo/bar", 1, @{@"sum" : @1}, FSTDocumentStateLocalMutations) ]);
- }
- @end
- NS_ASSUME_NONNULL_END
|