Skip to content

Forbid queries endAt an uncommitted server timestamp. #138

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 1 commit into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.google.firebase.firestore;

import static com.google.common.truth.Truth.assertThat;
import static com.google.firebase.firestore.testutil.Assert.assertThrows;
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testAlternateFirestore;
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testCollection;
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testCollectionWithDocs;
Expand All @@ -30,6 +32,7 @@

import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import com.google.android.gms.tasks.TaskCompletionSource;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.firestore.Transaction.Function;
Expand Down Expand Up @@ -432,6 +435,56 @@ public void queriesCannotBeCreatedFromDocumentsMissingSortValues() {
expectError(() -> query.endAt(snapshot), reason);
}

@Test
public void queriesCannotBeSortedByAnUncommittedServerTimestamp() {
CollectionReference collection = testCollection();

// Ensure the server timestamp stays uncommitted for the first half of the test
waitFor(collection.firestore.getClient().disableNetwork());

TaskCompletionSource<Void> offlineCallbackDone = new TaskCompletionSource<>();
TaskCompletionSource<Void> onlineCallbackDone = new TaskCompletionSource<>();

collection.addSnapshotListener(
(snapshot, error) -> {
assertNotNull(snapshot);

// Skip the initial empty snapshot.
if (snapshot.isEmpty()) return;

assertThat(snapshot.getDocuments()).hasSize(1);
DocumentSnapshot docSnap = snapshot.getDocuments().get(0);

if (snapshot.getMetadata().hasPendingWrites()) {
// Offline snapshot. Since the server timestamp is uncommitted, we shouldn't be able to
// query by it.
assertThrows(
IllegalArgumentException.class,
() ->
collection
.orderBy("timestamp")
.endAt(docSnap)
.addSnapshotListener((snapshot2, error2) -> {}));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is addSnapshotListener necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not strictly. It is necessary to trigger the internal error that would otherwise occur though. For that reason, I'd like to keep the snapshot listener in the 'online' case just a few lines down. And if I'm keeping it there, then keeping it here for symmetry seems to make some sense. (Though I don't mind removing it either.) wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.

offlineCallbackDone.setResult(null);
} else {
// Online snapshot. Since the server timestamp is committed, we should be able to query
// by it.
collection
.orderBy("timestamp")
.endAt(docSnap)
.addSnapshotListener((snapshot2, error2) -> {});
onlineCallbackDone.setResult(null);
}
});

DocumentReference document = collection.document();
document.set(map("timestamp", FieldValue.serverTimestamp()));
waitFor(offlineCallbackDone.getTask());

waitFor(collection.firestore.getClient().enableNetwork());
waitFor(onlineCallbackDone.getTask());
}

@Test
public void queriesMustNotHaveMoreComponentsThanOrderBy() {
CollectionReference collection = testCollection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.google.firebase.firestore.model.ResourcePath;
import com.google.firebase.firestore.model.value.FieldValue;
import com.google.firebase.firestore.model.value.ReferenceValue;
import com.google.firebase.firestore.model.value.ServerTimestampValue;
import com.google.firebase.firestore.util.ExecutorEventListener;
import com.google.firebase.firestore.util.Executors;
import com.google.firebase.firestore.util.ListenerRegistrationImpl;
Expand Down Expand Up @@ -582,7 +583,8 @@ public Query endAt(Object... fieldValues) {
* <p>Note that the Bound will always include the key of the document and so only the provided
* document will compare equal to the returned position.
*
* <p>Will throw if the document does not contain all fields of the order by of the query.
* <p>Will throw if the document does not contain all fields of the order by of the query or if
* any of the fields in the order by are an uncommitted server timestamp.
*/
private Bound boundFromDocumentSnapshot(
String methodName, DocumentSnapshot snapshot, boolean before) {
Expand All @@ -606,7 +608,14 @@ private Bound boundFromDocumentSnapshot(
components.add(ReferenceValue.valueOf(firestore.getDatabaseId(), document.getKey()));
} else {
FieldValue value = document.getField(orderBy.getField());
if (value != null) {
if (value instanceof ServerTimestampValue) {
throw new IllegalArgumentException(
"Invalid query. You are trying to start or end a query using a document for which "
+ "the field '"
+ orderBy.getField()
+ "' is an uncommitted server timestamp. (Since the value of this field is "
+ "unknown, you cannot start/end a query with it.)");
} else if (value != null) {
components.add(value);
} else {
throw new IllegalArgumentException(
Expand Down