Fix flaky queriesCannotBeSortedByAnUncommittedServerTimestamp test
@@ -461,6 +461,7 @@ public class ValidationTest {
TaskCompletionSource<Void> offlineCallbackDone = new TaskCompletionSource<>();
TaskCompletionSource<Void> onlineCallbackDone = new TaskCompletionSource<>();
+ ListenerRegistration listenerRegistration =
collection.addSnapshotListener(
(snapshot, error) -> {
assertNotNull(snapshot);
@@ -472,8 +473,8 @@ public class ValidationTest {
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.
+ // Offline snapshot. Since the server timestamp is uncommitted, we shouldn't be able
+ // to query by it.
assertThrows(
IllegalArgumentException.class,
() ->
@@ -481,15 +482,17 @@ public class ValidationTest {
.orderBy("timestamp")
.endAt(docSnap)
.addSnapshotListener((snapshot2, error2) -> {}));
- offlineCallbackDone.setResult(null);
+ // Use `trySetResult` since the callbacks fires twice if the WatchStream
+ // acknowledges the Write before the WriteStream.
+ offlineCallbackDone.trySetResult(null);
} else {
- // Online snapshot. Since the server timestamp is committed, we should be able to query
- // by it.
+ // 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);
+ onlineCallbackDone.trySetResult(null);
}
});
@@ -499,6 +502,8 @@ public class ValidationTest {
waitFor(collection.firestore.getClient().enableNetwork());
waitFor(onlineCallbackDone.getTask());
+
+ listenerRegistration.remove();
}
@Test
