Skip to content

Test large documents #7040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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 @@ -16,6 +16,7 @@

import static com.google.firebase.firestore.AccessHelper.getAsyncQueue;
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.checkOnlineAndOfflineResultsMatch;
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.getLargestDocContent;
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.isRunningAgainstEmulator;
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.newTestSettings;
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.provider;
Expand Down Expand Up @@ -1988,4 +1989,121 @@ public void snapshotListenerSortsInvalidUnicodeStringsAsServer() {

checkOnlineAndOfflineResultsMatch(colRef, orderedQuery, expectedDocIds.toArray(new String[0]));
}

@Test
public void testCanCRUDAndQueryLargeDocuments() {
CollectionReference collRef = testCollection();
DocumentReference docRef = collRef.document();
Map<String, Object> data = getLargestDocContent();

// Set
waitFor(docRef.set(data));

// Get
DocumentSnapshot snapshot = waitFor(docRef.get());
assertEquals(data, snapshot.getData());

// Update
Map<String, Object> newData = getLargestDocContent();
waitFor(docRef.update(newData));
snapshot = waitFor(docRef.get());
assertEquals(newData, snapshot.getData());

// Query
QuerySnapshot querySnapshot = waitFor(collRef.get());
assertEquals(querySnapshot.size(), 1);
assertEquals(newData, querySnapshot.getDocuments().get(0).getData());

// Delete
waitFor(docRef.delete());
snapshot = waitFor(docRef.get());
assertFalse(snapshot.exists());
}
Comment on lines +1993 to +2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider extracting the common parts of these tests into helper methods to reduce code duplication and improve readability.


@Test
public void testCanCRUDLargeDocumentsInsideTransaction() {
CollectionReference collRef = testCollection();

DocumentReference docRef1 = collRef.document();
DocumentReference docRef2 = collRef.document();
DocumentReference docRef3 = collRef.document();
Map<String, Object> data = getLargestDocContent();
Map<String, Object> newData = getLargestDocContent();
waitFor(docRef1.set(data));
waitFor(docRef3.set(data));

waitFor(
collRef
.getFirestore()
.runTransaction(
transaction -> {
// Get and update
DocumentSnapshot snapshot = transaction.get(docRef1);
assertEquals(data, snapshot.getData());
transaction.update(docRef1, newData);

// Set
transaction.set(docRef2, data);

// Delete
transaction.delete(docRef3);
return null;
}));

DocumentSnapshot snapshot = waitFor(docRef1.get());
assertEquals(newData, snapshot.getData());

snapshot = waitFor(docRef2.get());
assertEquals(data, snapshot.getData());

snapshot = waitFor(docRef3.get());
assertFalse(snapshot.exists());
}

@Test
public void listenToLargeQuerySnapshot() throws Exception {
CollectionReference collRef = testCollection();
DocumentReference docRef = collRef.document();
Map<String, Object> data = getLargestDocContent();
waitFor(docRef.set(data));

CountDownLatch latch = new CountDownLatch(1);
List<QuerySnapshot> querySnapshots = new ArrayList<>();
ListenerRegistration registration =
collRef.addSnapshotListener(
(value, error) -> {
querySnapshots.add(value);
latch.countDown();
});

latch.await();
registration.remove();

assertEquals(querySnapshots.size(), 1);
assertEquals(querySnapshots.get(0).getDocuments().size(), 1);
assertEquals(data, querySnapshots.get(0).getDocuments().get(0).getData());
}

@Test
public void listenToLargeDocumentSnapshot() throws Exception {
DocumentReference docRef = testDocument();
Map<String, Object> data = getLargestDocContent();
waitFor(docRef.set(data));

CountDownLatch latch = new CountDownLatch(1);
List<DocumentSnapshot> documentSnapshots = new ArrayList<>();

ListenerRegistration registration =
docRef.addSnapshotListener(
(value, error) -> {
documentSnapshots.add(value);
latch.countDown();
});

latch.await();
registration.remove();

assertEquals(documentSnapshots.size(), 1);
assertEquals(data, documentSnapshots.get(0).getData());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.firestore.AccessHelper;
import com.google.firebase.firestore.Blob;
import com.google.firebase.firestore.BuildConfig;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
Expand All @@ -52,9 +53,11 @@
import com.google.firebase.firestore.util.Logger;
import com.google.firebase.firestore.util.Logger.Level;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Semaphore;
Expand Down Expand Up @@ -561,4 +564,20 @@ public static void checkOnlineAndOfflineResultsMatch(
assertEquals(expectedDocIds, querySnapshotToIds(docsFromServer));
}
}

/**
* Returns a Blob with the size equal to the largest number of bytes allowed to
* be stored in a Firestore document.
*/
public static Map<String, Object> getLargestDocContent() {
int MAX_BYTES_PER_FIELD_VALUE = 1048487;
// Subtract 8 for '__name__', 20 for its value, and 4 for 'blob'.
int numBytesToUse = MAX_BYTES_PER_FIELD_VALUE - 8 - 20 - 4;

byte[] bytes = new byte[numBytesToUse];
// Fill the byte array with random values
Random random = new Random();
random.nextBytes(bytes);
return Collections.singletonMap("blob", Blob.fromBytes(bytes));
}
}
Loading