|
16 | 16 |
|
17 | 17 | import static com.google.firebase.firestore.testutil.TestUtil.map;
|
18 | 18 | import static com.google.firebase.firestore.util.Util.autoId;
|
19 |
| -import static junit.framework.Assert.assertEquals; |
20 | 19 | import static junit.framework.Assert.assertNull;
|
21 | 20 | import static junit.framework.Assert.fail;
|
22 | 21 |
|
@@ -64,7 +63,7 @@ public class IntegrationTestUtil {
|
64 | 63 | private static final Map<FirebaseFirestore, Boolean> firestoreStatus = new HashMap<>();
|
65 | 64 |
|
66 | 65 | /** 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; |
68 | 67 |
|
69 | 68 | /**
|
70 | 69 | * 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() {
|
112 | 111 | */
|
113 | 112 | public static FirebaseFirestore testFirestore(FirebaseFirestoreSettings settings) {
|
114 | 113 | FirebaseFirestore firestore = testFirestore(provider.projectId(), Level.DEBUG, settings);
|
115 |
| - if (!backendPrimed) { |
116 |
| - backendPrimed = true; |
117 |
| - primeBackend(); |
118 |
| - } |
| 114 | + primeBackend(); |
119 | 115 | return firestore;
|
120 | 116 | }
|
121 | 117 |
|
122 | 118 | 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 | + } |
144 | 151 | }
|
145 | 152 |
|
146 | 153 | /** Initializes a new Firestore instance that uses a non-existing default project. */
|
|
0 commit comments