Skip to content
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 @@ -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;
Comment thread
jinseopkim0 marked this conversation as resolved.
Resolved

protected enum FirestoreEdition {
STANDARD,
Expand Down Expand Up @@ -126,47 +126,52 @@ public void before() throws Exception {
public void primeBackend() throws Exception {
if (backendPrimed) return;

backendPrimed = true;
CompletableFuture<Void> watchInitialized = new CompletableFuture<>();
CompletableFuture<Void> 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<Void> watchInitialized = new CompletableFuture<>();
CompletableFuture<Void> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public CollectionReference testCollectionWithDocs(Map<String, Map<String, Object
throws ExecutionException, InterruptedException, TimeoutException {
CollectionReference collection = firestore.collection(LocalFirestoreHelper.autoId());
for (Map.Entry<String, Map<String, Object>> 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;
}
Expand Down
Loading