Skip to content

Commit

Permalink
Refactor Remote Event
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Jun 5, 2018
1 parent 9307f48 commit dfe5c51
Show file tree
Hide file tree
Showing 16 changed files with 1,330 additions and 1,175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForRunning = "YES"
buildForTesting = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6EDD3AD120BF247500C33877"
BuildableName = "Firestore_FuzzTests_iOS.xctest"
BlueprintName = "Firestore_FuzzTests_iOS"
ReferencedContainer = "container:Firestore.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
Expand Down
14 changes: 3 additions & 11 deletions Firestore/Example/Tests/Core/FSTQueryListenerTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ - (void)testRaisesEventForEmptyCollectionAfterSync {

FSTView *view = [[FSTView alloc] initWithQuery:query remoteDocuments:DocumentKeySet{}];
FSTViewSnapshot *snap1 = FSTTestApplyChanges(view, @[], nil);

FSTTargetChange *ackTarget =
[FSTTargetChange changeWithDocuments:@[]
currentStatusUpdate:FSTCurrentStatusUpdateMarkCurrent];
FSTViewSnapshot *snap2 = FSTTestApplyChanges(view, @[], ackTarget);
FSTViewSnapshot *snap2 = FSTTestApplyChanges(view, @[], FSTTestTargetChangeMarkCurrent());

[listener queryDidChangeViewSnapshot:snap1];
XCTAssertEqualObjects(accum, @[]);
Expand Down Expand Up @@ -188,9 +184,7 @@ - (void)testDoesNotRaiseEventsForMetadataChangesUnlessSpecified {
FSTView *view = [[FSTView alloc] initWithQuery:query remoteDocuments:DocumentKeySet{}];
FSTViewSnapshot *snap1 = FSTTestApplyChanges(view, @[ doc1 ], nil);

FSTTargetChange *ackTarget =
[FSTTargetChange changeWithDocuments:@[ doc1 ]
currentStatusUpdate:FSTCurrentStatusUpdateMarkCurrent];
FSTTargetChange *ackTarget = FSTTestTargetChangeAckDocuments({doc1.key});
FSTViewSnapshot *snap2 = FSTTestApplyChanges(view, @[], ackTarget);
FSTViewSnapshot *snap3 = FSTTestApplyChanges(view, @[ doc2 ], nil);

Expand Down Expand Up @@ -342,9 +336,7 @@ - (void)testWillWaitForSyncIfOnline {
FSTViewSnapshot *snap1 = FSTTestApplyChanges(view, @[ doc1 ], nil);
FSTViewSnapshot *snap2 = FSTTestApplyChanges(view, @[ doc2 ], nil);
FSTViewSnapshot *snap3 =
FSTTestApplyChanges(view, @[],
[FSTTargetChange changeWithDocuments:@[ doc1, doc2 ]
currentStatusUpdate:FSTCurrentStatusUpdateMarkCurrent]);
FSTTestApplyChanges(view, @[], FSTTestTargetChangeAckDocuments({doc1.key, doc2.key}));

[listener applyChangedOnlineState:FSTOnlineStateOnline]; // no event
[listener queryDidChangeViewSnapshot:snap1];
Expand Down
46 changes: 16 additions & 30 deletions Firestore/Example/Tests/Core/FSTViewTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ - (void)testAddsDocumentsBasedOnQuery {
FSTDocument *doc2 = FSTTestDoc("rooms/eros/messages/2", 0, @{@"text" : @"msg2"}, NO);
FSTDocument *doc3 = FSTTestDoc("rooms/other/messages/1", 0, @{@"text" : @"msg3"}, NO);

FSTViewSnapshot *_Nullable snapshot =
FSTTestApplyChanges(view, @[ doc1, doc2, doc3 ],
[FSTTargetChange changeWithDocuments:@[ doc1, doc2, doc3 ]
currentStatusUpdate:FSTCurrentStatusUpdateMarkCurrent]);
FSTViewSnapshot *_Nullable snapshot = FSTTestApplyChanges(
view, @[ doc1, doc2, doc3 ], FSTTestTargetChangeAckDocuments({doc1.key, doc2.key, doc3.key}));

XCTAssertEqual(snapshot.query, query);

Expand Down Expand Up @@ -90,8 +88,7 @@ - (void)testRemovesDocuments {
// delete doc2, add doc3
FSTViewSnapshot *snapshot =
FSTTestApplyChanges(view, @[ FSTTestDeletedDoc("rooms/eros/messages/2", 0), doc3 ],
[FSTTargetChange changeWithDocuments:@[ doc1, doc3 ]
currentStatusUpdate:FSTCurrentStatusUpdateMarkCurrent]);
FSTTestTargetChangeAckDocuments({doc1.key, doc3.key}));

XCTAssertEqual(snapshot.query, query);

Expand Down Expand Up @@ -216,10 +213,8 @@ - (void)testRemovesDocumentsForQueryWithLimit {
FSTTestApplyChanges(view, @[ doc1, doc3 ], nil);

// add doc2, which should push out doc3
FSTViewSnapshot *snapshot =
FSTTestApplyChanges(view, @[ doc2 ],
[FSTTargetChange changeWithDocuments:@[ doc1, doc2, doc3 ]
currentStatusUpdate:FSTCurrentStatusUpdateMarkCurrent]);
FSTViewSnapshot *snapshot = FSTTestApplyChanges(
view, @[ doc2 ], FSTTestTargetChangeAckDocuments({doc1.key, doc2.key, doc3.key}));

XCTAssertEqual(snapshot.query, query);

Expand Down Expand Up @@ -263,9 +258,8 @@ - (void)testDoesntReportChangesForDocumentBeyondLimitOfQuery {
previousChanges:viewDocChanges];
FSTViewSnapshot *snapshot =
[view applyChangesToDocuments:viewDocChanges
targetChange:[FSTTargetChange
changeWithDocuments:@[ doc1, doc2, doc3, doc4 ]
currentStatusUpdate:FSTCurrentStatusUpdateMarkCurrent]]
targetChange:FSTTestTargetChangeAckDocuments(
{doc1.key, doc2.key, doc3.key, doc4.key})]
.snapshot;

XCTAssertEqual(snapshot.query, query);
Expand Down Expand Up @@ -294,27 +288,21 @@ - (void)testKeepsTrackOfLimboDocuments {
applyChangesToDocuments:[view computeChangesWithDocuments:FSTTestDocUpdates(@[ doc1 ])]];
XCTAssertEqualObjects(change.limboChanges, @[]);

change =
[view applyChangesToDocuments:[view computeChangesWithDocuments:FSTTestDocUpdates(@[])]
targetChange:[FSTTargetChange
changeWithDocuments:@[]
currentStatusUpdate:FSTCurrentStatusUpdateMarkCurrent]];
change = [view applyChangesToDocuments:[view computeChangesWithDocuments:FSTTestDocUpdates(@[])]
targetChange:FSTTestTargetChangeMarkCurrent()];
XCTAssertEqualObjects(
change.limboChanges,
@[ [FSTLimboDocumentChange changeWithType:FSTLimboDocumentChangeTypeAdded key:doc1.key] ]);

change = [view
applyChangesToDocuments:[view computeChangesWithDocuments:FSTTestDocUpdates(@[])]
targetChange:[FSTTargetChange changeWithDocuments:@[ doc1 ]
currentStatusUpdate:FSTCurrentStatusUpdateNone]];
change = [view applyChangesToDocuments:[view computeChangesWithDocuments:FSTTestDocUpdates(@[])]
targetChange:FSTTestTargetChangeAckDocuments({doc1.key})];
XCTAssertEqualObjects(
change.limboChanges,
@[ [FSTLimboDocumentChange changeWithType:FSTLimboDocumentChangeTypeRemoved key:doc1.key] ]);

change = [view
applyChangesToDocuments:[view computeChangesWithDocuments:FSTTestDocUpdates(@[ doc2 ])]
targetChange:[FSTTargetChange changeWithDocuments:@[ doc2 ]
currentStatusUpdate:FSTCurrentStatusUpdateNone]];
change =
[view applyChangesToDocuments:[view computeChangesWithDocuments:FSTTestDocUpdates(@[ doc2 ])]
targetChange:FSTTestTargetChangeAckDocuments({doc2.key})];
XCTAssertEqualObjects(change.limboChanges, @[]);

change = [view
Expand Down Expand Up @@ -343,11 +331,9 @@ - (void)testResumingQueryCreatesNoLimbos {
FSTView *view =
[[FSTView alloc] initWithQuery:query remoteDocuments:DocumentKeySet{doc1.key, doc2.key}];

FSTTargetChange *markCurrent =
[FSTTargetChange changeWithDocuments:@[]
currentStatusUpdate:FSTCurrentStatusUpdateMarkCurrent];
FSTViewDocumentChanges *changes = [view computeChangesWithDocuments:FSTTestDocUpdates(@[])];
FSTViewChange *change = [view applyChangesToDocuments:changes targetChange:markCurrent];
FSTViewChange *change =
[view applyChangesToDocuments:changes targetChange:FSTTestTargetChangeMarkCurrent()];
XCTAssertEqualObjects(change.limboChanges, @[]);
}

Expand Down
5 changes: 5 additions & 0 deletions Firestore/Example/Tests/Integration/FSTDatastoreTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
using firebase::firestore::auth::EmptyCredentialsProvider;
using firebase::firestore::core::DatabaseInfo;
using firebase::firestore::model::DatabaseId;
using firebase::firestore::model::DocumentKeySet;
using firebase::firestore::model::Precondition;
using firebase::firestore::model::TargetId;

Expand Down Expand Up @@ -121,6 +122,10 @@ - (void)rejectFailedWriteWithBatchID:(FSTBatchID)batchID error:(NSError *)error
HARD_FAIL("Not implemented");
}

- (DocumentKeySet)remoteKeysForTarget:(FSTBoxedTargetID *)targetId {
return DocumentKeySet{};
}

- (void)applyRemoteEvent:(FSTRemoteEvent *)remoteEvent {
[self.listenEvents addObject:remoteEvent];
XCTestExpectation *expectation = [self.listenEventExpectations objectAtIndex:0];
Expand Down
31 changes: 14 additions & 17 deletions Firestore/Example/Tests/Local/FSTLocalStoreTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,8 @@ - (void)testCollectsGarbageAfterChangeBatch {
FSTQuery *query = FSTTestQuery("foo");
FSTTargetID targetID = [self allocateQuery:query];

[self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, NO),
@[ @(targetID) ], @[])];
[self applyRemoteEvent:FSTTestAddedRemoteEvent(FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, NO),
@[ @(targetID) ])];
[self collectGarbage];
FSTAssertContains(FSTTestDoc("foo/bar", 2, @{@"foo" : @"bar"}, NO));

Expand Down Expand Up @@ -703,8 +703,8 @@ - (void)testPinsDocumentsInTheLocalView {
FSTQuery *query = FSTTestQuery("foo");
FSTTargetID targetID = [self allocateQuery:query];

[self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, NO),
@[ @(targetID) ], @[])];
[self applyRemoteEvent:FSTTestAddedRemoteEvent(FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, NO),
@[ @(targetID) ])];
[self writeMutation:FSTTestSetMutation(@"foo/baz", @{@"foo" : @"baz"})];
[self collectGarbage];
FSTAssertContains(FSTTestDoc("foo/bar", 1, @{@"foo" : @"bar"}, NO));
Expand Down Expand Up @@ -805,21 +805,18 @@ - (void)testPersistsResumeTokens {
FSTBoxedTargetID *targetID = @(queryData.targetID);
NSData *resumeToken = FSTTestResumeTokenFromSnapshotVersion(1000);

FSTWatchChange *watchChange =
FSTWatchTargetChange *watchChange =
[FSTWatchTargetChange changeWithState:FSTWatchTargetChangeStateCurrent
targetIDs:@[ targetID ]
resumeToken:resumeToken];
NSMutableDictionary<FSTBoxedTargetID *, FSTQueryData *> *listens =
[NSMutableDictionary dictionary];
listens[targetID] = queryData;
NSMutableDictionary<FSTBoxedTargetID *, NSNumber *> *pendingResponses =
[NSMutableDictionary dictionary];
FSTWatchChangeAggregator *aggregator =
[[FSTWatchChangeAggregator alloc] initWithSnapshotVersion:testutil::Version(1000)
listenTargets:listens
pendingTargetResponses:pendingResponses];
[aggregator addWatchChanges:@[ watchChange ]];
FSTRemoteEvent *remoteEvent = [aggregator remoteEvent];
FSTWatchChangeAggregator *aggregator = [[FSTWatchChangeAggregator alloc]
initWithTargetMetadataProvider:[FSTTestTargetMetadataProvider
withSingleResultAtKey:testutil::Key("foo/bar")]];
[aggregator handleTargetChange:watchChange];
FSTRemoteEvent *remoteEvent = [aggregator remoteEventAtSnapshotVersion:testutil::Version(1000)];
[self applyRemoteEvent:remoteEvent];

// Stop listening so that the query should become inactive (but persistent)
Expand All @@ -842,10 +839,10 @@ - (void)testRemoteDocumentKeysForTarget {
[self allocateQuery:query];
FSTAssertTargetID(2);

[self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDoc("foo/baz", 10, @{@"a" : @"b"}, NO),
@[ @2 ], @[])];
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(FSTTestDoc("foo/bar", 20, @{@"a" : @"b"}, NO),
@[ @2 ], @[])];
[self applyRemoteEvent:FSTTestAddedRemoteEvent(FSTTestDoc("foo/baz", 10, @{@"a" : @"b"}, NO),
@[ @2 ])];
[self applyRemoteEvent:FSTTestAddedRemoteEvent(FSTTestDoc("foo/bar", 20, @{@"a" : @"b"}, NO),
@[ @2 ])];

[self.localStore locallyWriteMutations:@[ FSTTestSetMutation(@"foo/bonk", @{@"a" : @"b"}) ]];

Expand Down
Loading

0 comments on commit dfe5c51

Please sign in to comment.