Skip to content

Commit 8850386

Browse files
authored
Merge a08a955 into f08e0c7
2 parents f08e0c7 + a08a955 commit 8850386

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/FirestoreTest.java

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

1717
import static com.google.firebase.firestore.AccessHelper.getAsyncQueue;
1818
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.checkOnlineAndOfflineResultsMatch;
19+
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.getLargestDocContent;
1920
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.isRunningAgainstEmulator;
2021
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.newTestSettings;
2122
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.provider;
@@ -1988,4 +1989,121 @@ public void snapshotListenerSortsInvalidUnicodeStringsAsServer() {
19881989

19891990
checkOnlineAndOfflineResultsMatch(colRef, orderedQuery, expectedDocIds.toArray(new String[0]));
19901991
}
1992+
1993+
@Test
1994+
public void testCanCRUDAndQueryLargeDocuments() {
1995+
CollectionReference collRef = testCollection();
1996+
DocumentReference docRef = collRef.document();
1997+
Map<String, Object> data = getLargestDocContent();
1998+
1999+
// Set
2000+
waitFor(docRef.set(data));
2001+
2002+
// Get
2003+
DocumentSnapshot snapshot = waitFor(docRef.get());
2004+
assertEquals(data, snapshot.getData());
2005+
2006+
// Update
2007+
Map<String, Object> newData = getLargestDocContent();
2008+
waitFor(docRef.update(newData));
2009+
snapshot = waitFor(docRef.get());
2010+
assertEquals(newData, snapshot.getData());
2011+
2012+
// Query
2013+
QuerySnapshot querySnapshot = waitFor(collRef.get());
2014+
assertEquals(querySnapshot.size(), 1);
2015+
assertEquals(newData, querySnapshot.getDocuments().get(0).getData());
2016+
2017+
// Delete
2018+
waitFor(docRef.delete());
2019+
snapshot = waitFor(docRef.get());
2020+
assertFalse(snapshot.exists());
2021+
}
2022+
2023+
@Test
2024+
public void testCanCRUDLargeDocumentsInsideTransaction() {
2025+
CollectionReference collRef = testCollection();
2026+
2027+
DocumentReference docRef1 = collRef.document();
2028+
DocumentReference docRef2 = collRef.document();
2029+
DocumentReference docRef3 = collRef.document();
2030+
Map<String, Object> data = getLargestDocContent();
2031+
Map<String, Object> newData = getLargestDocContent();
2032+
waitFor(docRef1.set(data));
2033+
waitFor(docRef3.set(data));
2034+
2035+
waitFor(
2036+
collRef
2037+
.getFirestore()
2038+
.runTransaction(
2039+
transaction -> {
2040+
// Get and update
2041+
DocumentSnapshot snapshot = transaction.get(docRef1);
2042+
assertEquals(data, snapshot.getData());
2043+
transaction.update(docRef1, newData);
2044+
2045+
// Set
2046+
transaction.set(docRef2, data);
2047+
2048+
// Delete
2049+
transaction.delete(docRef3);
2050+
return null;
2051+
}));
2052+
2053+
DocumentSnapshot snapshot = waitFor(docRef1.get());
2054+
assertEquals(newData, snapshot.getData());
2055+
2056+
snapshot = waitFor(docRef2.get());
2057+
assertEquals(data, snapshot.getData());
2058+
2059+
snapshot = waitFor(docRef3.get());
2060+
assertFalse(snapshot.exists());
2061+
}
2062+
2063+
@Test
2064+
public void listenToLargeQuerySnapshot() throws Exception {
2065+
CollectionReference collRef = testCollection();
2066+
DocumentReference docRef = collRef.document();
2067+
Map<String, Object> data = getLargestDocContent();
2068+
waitFor(docRef.set(data));
2069+
2070+
CountDownLatch latch = new CountDownLatch(1);
2071+
List<QuerySnapshot> querySnapshots = new ArrayList<>();
2072+
ListenerRegistration registration =
2073+
collRef.addSnapshotListener(
2074+
(value, error) -> {
2075+
querySnapshots.add(value);
2076+
latch.countDown();
2077+
});
2078+
2079+
latch.await();
2080+
registration.remove();
2081+
2082+
assertEquals(querySnapshots.size(), 1);
2083+
assertEquals(querySnapshots.get(0).getDocuments().size(), 1);
2084+
assertEquals(data, querySnapshots.get(0).getDocuments().get(0).getData());
2085+
}
2086+
2087+
@Test
2088+
public void listenToLargeDocumentSnapshot() throws Exception {
2089+
DocumentReference docRef = testDocument();
2090+
Map<String, Object> data = getLargestDocContent();
2091+
waitFor(docRef.set(data));
2092+
2093+
CountDownLatch latch = new CountDownLatch(1);
2094+
List<DocumentSnapshot> documentSnapshots = new ArrayList<>();
2095+
2096+
ListenerRegistration registration =
2097+
docRef.addSnapshotListener(
2098+
(value, error) -> {
2099+
documentSnapshots.add(value);
2100+
latch.countDown();
2101+
});
2102+
2103+
latch.await();
2104+
registration.remove();
2105+
2106+
assertEquals(documentSnapshots.size(), 1);
2107+
assertEquals(data, documentSnapshots.get(0).getData());
2108+
}
19912109
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.google.firebase.FirebaseApp;
3131
import com.google.firebase.FirebaseOptions;
3232
import com.google.firebase.firestore.AccessHelper;
33+
import com.google.firebase.firestore.Blob;
3334
import com.google.firebase.firestore.BuildConfig;
3435
import com.google.firebase.firestore.CollectionReference;
3536
import com.google.firebase.firestore.DocumentReference;
@@ -52,9 +53,11 @@
5253
import com.google.firebase.firestore.util.Logger;
5354
import com.google.firebase.firestore.util.Logger.Level;
5455
import java.util.ArrayList;
56+
import java.util.Collections;
5557
import java.util.HashMap;
5658
import java.util.List;
5759
import java.util.Map;
60+
import java.util.Random;
5861
import java.util.concurrent.CountDownLatch;
5962
import java.util.concurrent.ExecutionException;
6063
import java.util.concurrent.Semaphore;
@@ -125,6 +128,10 @@ public enum TargetBackend {
125128

126129
private static boolean backendPrimed = false;
127130

131+
private static final Random RANDOM = new Random();
132+
133+
private static final int MAX_BYTES_PER_FIELD_VALUE = 1048487;
134+
128135
// FirebaseOptions needed to create a test FirebaseApp.
129136
private static final FirebaseOptions OPTIONS =
130137
new FirebaseOptions.Builder()
@@ -561,4 +568,17 @@ public static void checkOnlineAndOfflineResultsMatch(
561568
assertEquals(expectedDocIds, querySnapshotToIds(docsFromServer));
562569
}
563570
}
571+
572+
/**
573+
* Returns a Blob with the size equal to the largest number of bytes allowed to
574+
* be stored in a Firestore document.
575+
*/
576+
public static Map<String, Object> getLargestDocContent() {
577+
// Subtract 8 for '__name__', 20 for its value, and 4 for 'blob'.
578+
int numBytesToUse = MAX_BYTES_PER_FIELD_VALUE - 8 - 20 - 4;
579+
byte[] bytes = new byte[numBytesToUse];
580+
// Fill the byte array with random values
581+
RANDOM.nextBytes(bytes);
582+
return Collections.singletonMap("blob", Blob.fromBytes(bytes));
583+
}
564584
}

0 commit comments

Comments
 (0)