-
Notifications
You must be signed in to change notification settings - Fork 624
Index-Free (1/6): Adding LocalStoreTestHelper for NO_CHANGE event #614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
import static com.google.firebase.firestore.testutil.TestUtil.doc; | ||
import static com.google.firebase.firestore.testutil.TestUtil.key; | ||
import static com.google.firebase.firestore.testutil.TestUtil.map; | ||
import static com.google.firebase.firestore.testutil.TestUtil.noChangeEvent; | ||
import static com.google.firebase.firestore.testutil.TestUtil.patchMutation; | ||
import static com.google.firebase.firestore.testutil.TestUtil.query; | ||
import static com.google.firebase.firestore.testutil.TestUtil.resumeToken; | ||
|
@@ -46,7 +47,6 @@ | |
import com.google.firebase.database.collection.ImmutableSortedMap; | ||
import com.google.firebase.database.collection.ImmutableSortedSet; | ||
import com.google.firebase.firestore.FieldValue; | ||
import com.google.firebase.firestore.TestUtil.TestTargetMetadataProvider; | ||
import com.google.firebase.firestore.auth.User; | ||
import com.google.firebase.firestore.core.Query; | ||
import com.google.firebase.firestore.model.Document; | ||
|
@@ -61,13 +61,9 @@ | |
import com.google.firebase.firestore.model.mutation.MutationResult; | ||
import com.google.firebase.firestore.model.mutation.SetMutation; | ||
import com.google.firebase.firestore.remote.RemoteEvent; | ||
import com.google.firebase.firestore.remote.WatchChange.WatchTargetChange; | ||
import com.google.firebase.firestore.remote.WatchChange.WatchTargetChangeType; | ||
import com.google.firebase.firestore.remote.WatchChangeAggregator; | ||
import com.google.firebase.firestore.remote.WatchStream; | ||
import com.google.firebase.firestore.remote.WriteStream; | ||
import com.google.firebase.firestore.testutil.TestUtil; | ||
import com.google.protobuf.ByteString; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
@@ -912,26 +908,15 @@ public void testPersistsResumeTokens() { | |
|
||
Query query = query("foo/bar"); | ||
int targetId = allocateQuery(query); | ||
ByteString resumeToken = resumeToken(1000); | ||
|
||
QueryData queryData = TestUtil.queryData(targetId, QueryPurpose.LISTEN, "foo/bar"); | ||
TestTargetMetadataProvider testTargetMetadataProvider = new TestTargetMetadataProvider(); | ||
testTargetMetadataProvider.setSyncedKeys(queryData, DocumentKey.emptyKeySet()); | ||
|
||
WatchChangeAggregator aggregator = new WatchChangeAggregator(testTargetMetadataProvider); | ||
|
||
WatchTargetChange watchChange = | ||
new WatchTargetChange(WatchTargetChangeType.Current, asList(targetId), resumeToken); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a current change, not a no-change. Why are these are substitutable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
aggregator.handleTargetChange(watchChange); | ||
RemoteEvent remoteEvent = aggregator.createRemoteEvent(version(1000)); | ||
applyRemoteEvent(remoteEvent); | ||
applyRemoteEvent(noChangeEvent(targetId, 1000)); | ||
|
||
// Stop listening so that the query should become inactive (but persistent) | ||
localStore.releaseQuery(query); | ||
|
||
// Should come back with the same resume token | ||
QueryData queryData2 = localStore.allocateQuery(query); | ||
assertEquals(resumeToken, queryData2.getResumeToken()); | ||
assertEquals(resumeToken(1000), queryData2.getResumeToken()); | ||
} | ||
|
||
@Test | ||
|
@@ -943,35 +928,18 @@ public void testDoesNotReplaceResumeTokenWithEmptyByteString() { | |
|
||
Query query = query("foo/bar"); | ||
int targetId = allocateQuery(query); | ||
ByteString resumeToken = resumeToken(1000); | ||
|
||
QueryData queryData = TestUtil.queryData(targetId, QueryPurpose.LISTEN, "foo/bar"); | ||
TestTargetMetadataProvider testTargetMetadataProvider = new TestTargetMetadataProvider(); | ||
testTargetMetadataProvider.setSyncedKeys(queryData, DocumentKey.emptyKeySet()); | ||
|
||
WatchChangeAggregator aggregator1 = new WatchChangeAggregator(testTargetMetadataProvider); | ||
|
||
WatchTargetChange watchChange1 = | ||
new WatchTargetChange(WatchTargetChangeType.Current, asList(targetId), resumeToken); | ||
aggregator1.handleTargetChange(watchChange1); | ||
RemoteEvent remoteEvent1 = aggregator1.createRemoteEvent(version(1000)); | ||
applyRemoteEvent(remoteEvent1); | ||
applyRemoteEvent(noChangeEvent(targetId, 1000)); | ||
|
||
// New message with empty resume token should not replace the old resume token | ||
WatchChangeAggregator aggregator2 = new WatchChangeAggregator(testTargetMetadataProvider); | ||
WatchTargetChange watchChange2 = | ||
new WatchTargetChange( | ||
WatchTargetChangeType.Current, asList(targetId), WatchStream.EMPTY_RESUME_TOKEN); | ||
aggregator2.handleTargetChange(watchChange2); | ||
RemoteEvent remoteEvent2 = aggregator2.createRemoteEvent(version(2000)); | ||
applyRemoteEvent(remoteEvent2); | ||
applyRemoteEvent(TestUtil.noChangeEvent(targetId, 2000, WatchStream.EMPTY_RESUME_TOKEN)); | ||
|
||
// Stop listening so that the query should become inactive (but persistent) | ||
localStore.releaseQuery(query); | ||
|
||
// Should come back with the same resume token | ||
QueryData queryData2 = localStore.allocateQuery(query); | ||
assertEquals(resumeToken, queryData2.getResumeToken()); | ||
assertEquals(resumeToken(1000), queryData2.getResumeToken()); | ||
} | ||
|
||
@Test | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
.../src/testUtil/java/com/google/firebase/firestore/testutil/TestTargetMetadataProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2019 Google LLC | ||
// | ||
// 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. | ||
|
||
package com.google.firebase.firestore.testutil; | ||
|
||
import com.google.firebase.database.collection.ImmutableSortedSet; | ||
import com.google.firebase.firestore.local.QueryData; | ||
import com.google.firebase.firestore.model.DocumentKey; | ||
import com.google.firebase.firestore.remote.WatchChangeAggregator; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* An implementation of TargetMetadataProvider that provides controlled access to the | ||
* `TargetMetadataProvider` callbacks. Any target accessed via these callbacks must be registered | ||
* beforehand via `setSyncedKeys()`. | ||
*/ | ||
public class TestTargetMetadataProvider implements WatchChangeAggregator.TargetMetadataProvider { | ||
final Map<Integer, ImmutableSortedSet<DocumentKey>> syncedKeys = new HashMap<>(); | ||
final Map<Integer, QueryData> queryData = new HashMap<>(); | ||
|
||
@Override | ||
public ImmutableSortedSet<DocumentKey> getRemoteKeysForTarget(int targetId) { | ||
return syncedKeys.get(targetId) != null ? syncedKeys.get(targetId) : DocumentKey.emptyKeySet(); | ||
} | ||
|
||
@androidx.annotation.Nullable | ||
@Override | ||
public QueryData getQueryDataForTarget(int targetId) { | ||
return queryData.get(targetId); | ||
} | ||
|
||
/** Sets or replaces the local state for the provided query data. */ | ||
public void setSyncedKeys(QueryData queryData, ImmutableSortedSet<DocumentKey> keys) { | ||
this.queryData.put(queryData.getTargetId(), queryData); | ||
this.syncedKeys.put(queryData.getTargetId(), keys); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for doing this. At some point we should probably cut these down to the minimum, given the assumption there's no reasonable way to share with the main Firestore testutil.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, only half of the methods are currently used, but it doesn't seem trivial to just put them in the test class (so I refrained from doing this for now).