Skip to content

Commit 342eaf2

Browse files
author
Michael Lehenbauer
committed
Rework to use listener to avoid EventAccumulator / timeout complication.
1 parent 12bde3a commit 342eaf2

File tree

2 files changed

+37
-39
lines changed

2 files changed

+37
-39
lines changed

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/testutil/EventAccumulator.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
package com.google.firebase.firestore.testutil;
1616

17-
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.OPERATION_WAIT_TIMEOUT_MS;
1817
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.waitFor;
1918
import static com.google.firebase.firestore.util.Assert.hardAssert;
2019

@@ -48,30 +47,22 @@ public EventListener<T> listener() {
4847
};
4948
}
5049

51-
public List<T> await(int numEvents, long timeoutMs) {
50+
public List<T> await(int numEvents) {
5251
synchronized (this) {
5352
hardAssert(completion == null, "calling await while another await is running");
5453
completion = new TaskCompletionSource<>();
5554
maxEvents = maxEvents + numEvents;
5655
checkFulfilled();
5756
}
5857

59-
waitFor(completion.getTask(), timeoutMs);
58+
waitFor(completion.getTask());
6059
completion = null;
6160
return events.subList(maxEvents - numEvents, maxEvents);
6261
}
6362

64-
public List<T> await(int numEvents) {
65-
return await(numEvents, OPERATION_WAIT_TIMEOUT_MS);
66-
}
67-
6863
// Await 1 event.
69-
public T await(long timeoutMs) {
70-
return await(1, timeoutMs).get(0);
71-
}
72-
7364
public T await() {
74-
return await(1, OPERATION_WAIT_TIMEOUT_MS).get(0);
65+
return await(1).get(0);
7566
}
7667

7768
/** Waits for a snapshot with pending writes. */

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/testutil/IntegrationTestUtil.java

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import static com.google.firebase.firestore.testutil.TestUtil.map;
1818
import static com.google.firebase.firestore.util.Util.autoId;
19-
import static junit.framework.Assert.assertEquals;
2019
import static junit.framework.Assert.assertNull;
2120
import static junit.framework.Assert.fail;
2221

@@ -64,7 +63,7 @@ public class IntegrationTestUtil {
6463
private static final Map<FirebaseFirestore, Boolean> firestoreStatus = new HashMap<>();
6564

6665
/** Default amount of time to wait for a given operation to complete, used by waitFor() helper. */
67-
static final long OPERATION_WAIT_TIMEOUT_MS = 30000;
66+
private static final long OPERATION_WAIT_TIMEOUT_MS = 30000;
6867

6968
/**
7069
* Firestore databases can be subject to a ~30s "cold start" delay if they have not been used
@@ -112,35 +111,43 @@ public static FirebaseFirestore testFirestore() {
112111
*/
113112
public static FirebaseFirestore testFirestore(FirebaseFirestoreSettings settings) {
114113
FirebaseFirestore firestore = testFirestore(provider.projectId(), Level.DEBUG, settings);
115-
if (!backendPrimed) {
116-
backendPrimed = true;
117-
primeBackend();
118-
}
114+
primeBackend();
119115
return firestore;
120116
}
121117

122118
private static void primeBackend() {
123-
EventAccumulator<DocumentSnapshot> accumulator = new EventAccumulator<>();
124-
DocumentReference docRef = testDocument();
125-
ListenerRegistration listenerRegistration = docRef.addSnapshotListener(accumulator.listener());
126-
127-
// Wait for watch to initialize and deliver first event.
128-
accumulator.awaitRemoteEvent();
129-
130-
// Use a transaction to perform a write without triggering any local events.
131-
docRef
132-
.getFirestore()
133-
.runTransaction(
134-
transaction -> {
135-
transaction.set(docRef, map("value", "done"));
136-
return null;
137-
});
138-
139-
// Wait to see the write on the watch stream.
140-
DocumentSnapshot docSnap = accumulator.await(PRIMING_TIMEOUT_MS);
141-
assertEquals("done", docSnap.get("value"));
142-
143-
listenerRegistration.remove();
119+
if (!backendPrimed) {
120+
backendPrimed = true;
121+
TaskCompletionSource<Void> watchInitialized = new TaskCompletionSource<>();
122+
TaskCompletionSource<Void> watchUpdateReceived = new TaskCompletionSource<>();
123+
DocumentReference docRef = testDocument();
124+
ListenerRegistration listenerRegistration =
125+
docRef.addSnapshotListener(
126+
(snapshot, error) -> {
127+
if ("done".equals(snapshot.get("value"))) {
128+
watchUpdateReceived.setResult(null);
129+
} else {
130+
watchInitialized.setResult(null);
131+
}
132+
});
133+
134+
// Wait for watch to initialize and deliver first event.
135+
waitFor(watchInitialized.getTask());
136+
137+
// Use a transaction to perform a write without triggering any local events.
138+
docRef
139+
.getFirestore()
140+
.runTransaction(
141+
transaction -> {
142+
transaction.set(docRef, map("value", "done"));
143+
return null;
144+
});
145+
146+
// Wait to see the write on the watch stream.
147+
waitFor(watchUpdateReceived.getTask(), PRIMING_TIMEOUT_MS);
148+
149+
listenerRegistration.remove();
150+
}
144151
}
145152

146153
/** Initializes a new Firestore instance that uses a non-existing default project. */

0 commit comments

Comments
 (0)