From 1951fca624515c627e1b2af558c39e52e6879f18 Mon Sep 17 00:00:00 2001 From: Jin Seop Kim Date: Mon, 16 Nov 2026 11:20:14 -0500 Subject: [PATCH] chore(firestore): prime IT backend once per JVM and increase pipeline seed timeout --- .../google/cloud/firestore/it/ITBaseTest.java | 85 ++++++++++--------- .../cloud/firestore/it/ITPipelineTest.java | 2 +- 2 files changed, 46 insertions(+), 41 deletions(-) diff --git a/java-firestore/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITBaseTest.java b/java-firestore/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITBaseTest.java index cf6e86a696b1..73783cd03417 100644 --- a/java-firestore/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITBaseTest.java +++ b/java-firestore/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITBaseTest.java @@ -49,7 +49,7 @@ public abstract class ITBaseTest { protected Firestore firestore; protected FirestoreSpy firestoreSpy; private FirestoreOptions firestoreOptions; - private boolean backendPrimed = false; + private static volatile boolean backendPrimed = false; protected enum FirestoreEdition { STANDARD, @@ -126,47 +126,52 @@ public void before() throws Exception { public void primeBackend() throws Exception { if (backendPrimed) return; - backendPrimed = true; - CompletableFuture watchInitialized = new CompletableFuture<>(); - CompletableFuture watchUpdateReceived = new CompletableFuture<>(); - DocumentReference docRef = firestore.collection(autoId()).document(); - ListenerRegistration listenerRegistration = - docRef.addSnapshotListener( - (snapshot, error) -> { - if (error != null) { - logger.log( - Level.SEVERE, "Prime backend received error in snapshot listener.", error); - if (!watchInitialized.isDone()) { - watchInitialized.completeExceptionally(error); - } else if (!watchUpdateReceived.isDone()) { - watchUpdateReceived.completeExceptionally(error); + synchronized (ITBaseTest.class) { + if (backendPrimed) return; + + CompletableFuture watchInitialized = new CompletableFuture<>(); + CompletableFuture watchUpdateReceived = new CompletableFuture<>(); + DocumentReference docRef = firestore.collection(autoId()).document(); + ListenerRegistration listenerRegistration = + docRef.addSnapshotListener( + (snapshot, error) -> { + if (error != null) { + logger.log( + Level.SEVERE, "Prime backend received error in snapshot listener.", error); + if (!watchInitialized.isDone()) { + watchInitialized.completeExceptionally(error); + } else if (!watchUpdateReceived.isDone()) { + watchUpdateReceived.completeExceptionally(error); + } } - } - if (snapshot != null) { - if ("done".equals(snapshot.get("value"))) { - watchUpdateReceived.complete(null); - } else { - watchInitialized.complete(null); + if (snapshot != null) { + if ("done".equals(snapshot.get("value"))) { + watchUpdateReceived.complete(null); + } else { + watchInitialized.complete(null); + } } - } - }); - - // Wait for watch to initialize and deliver first event. - watchInitialized.get(PRIMING_TIMEOUT_MS, TimeUnit.MILLISECONDS); - - // Use a transaction to perform a write without triggering any local events. - docRef - .getFirestore() - .runTransaction( - transaction -> { - transaction.set(docRef, map("value", "done")); - return null; - }); - - // Wait to see the write on the watch stream. - watchUpdateReceived.get(PRIMING_TIMEOUT_MS, TimeUnit.MILLISECONDS); - - listenerRegistration.remove(); + }); + try { + // Wait for watch to initialize and deliver first event. + watchInitialized.get(PRIMING_TIMEOUT_MS, TimeUnit.MILLISECONDS); + + // Use a transaction to perform a write without triggering any local events. + docRef + .getFirestore() + .runTransaction( + transaction -> { + transaction.set(docRef, map("value", "done")); + return null; + }); + + // Wait to see the write on the watch stream. + watchUpdateReceived.get(PRIMING_TIMEOUT_MS, TimeUnit.MILLISECONDS); + backendPrimed = true; + } finally { + listenerRegistration.remove(); + } + } } @After diff --git a/java-firestore/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITPipelineTest.java b/java-firestore/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITPipelineTest.java index 12f746f83654..68a3a9565d10 100644 --- a/java-firestore/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITPipelineTest.java +++ b/java-firestore/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITPipelineTest.java @@ -170,7 +170,7 @@ public CollectionReference testCollectionWithDocs(Map> doc : docs.entrySet()) { - collection.document(doc.getKey()).set(doc.getValue()).get(5, TimeUnit.SECONDS); + collection.document(doc.getKey()).set(doc.getValue()).get(30, TimeUnit.SECONDS); } return collection; }