-
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,7 @@ | |
import com.google.firebase.firestore.model.value.ObjectValue; | ||
import com.google.firebase.firestore.remote.RemoteEvent; | ||
import com.google.firebase.firestore.remote.TargetChange; | ||
import com.google.firebase.firestore.remote.WatchChange; | ||
import com.google.firebase.firestore.remote.WatchChange.DocumentChange; | ||
import com.google.firebase.firestore.remote.WatchChangeAggregator; | ||
import com.google.protobuf.ByteString; | ||
|
@@ -374,6 +375,24 @@ public static Map<Integer, QueryData> activeLimboQueries(String docKey, Integer. | |
return activeLimboQueries(docKey, asList(targets)); | ||
} | ||
|
||
public static RemoteEvent noChangeEvent(int targetId, int version) { | ||
return noChangeEvent(targetId, version, resumeToken(version)); | ||
} | ||
|
||
public static RemoteEvent noChangeEvent(int targetId, int version, ByteString resumeToken) { | ||
QueryData queryData = TestUtil.queryData(targetId, QueryPurpose.LISTEN, "foo/bar"); | ||
TestTargetMetadataProvider testTargetMetadataProvider = new TestTargetMetadataProvider(); | ||
testTargetMetadataProvider.setSyncedKeys(queryData, DocumentKey.emptyKeySet()); | ||
|
||
WatchChangeAggregator aggregator = new WatchChangeAggregator(testTargetMetadataProvider); | ||
|
||
WatchChange.WatchTargetChange watchChange = | ||
new WatchChange.WatchTargetChange( | ||
WatchChange.WatchTargetChangeType.NoChange, asList(targetId), resumeToken); | ||
aggregator.handleTargetChange(watchChange); | ||
return aggregator.createRemoteEvent(version(version)); | ||
} | ||
|
||
public static RemoteEvent addedRemoteEvent( | ||
MaybeDocument doc, List<Integer> updatedInTargets, List<Integer> removedFromTargets) { | ||
DocumentChange change = | ||
|
@@ -526,6 +545,36 @@ public static ByteString streamToken(String contents) { | |
return ByteString.copyFrom(contents, Charsets.UTF_8); | ||
} | ||
|
||
/** | ||
* 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 static class TestTargetMetadataProvider | ||
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. Consider making this just a top-level class (i.e. in its own source file). 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. Done |
||
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); | ||
} | ||
} | ||
|
||
private static Map<String, Object> fromJsonString(String json) { | ||
try { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
|
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).