Skip to content

Commit 3169c89

Browse files
committed
test: Test large documents.
1 parent 213f63c commit 3169c89

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITSystemTest.java

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static com.google.cloud.firestore.LocalFirestoreHelper.UPDATE_SINGLE_FIELD_OBJECT;
2626
import static com.google.cloud.firestore.LocalFirestoreHelper.fullPath;
2727
import static com.google.cloud.firestore.LocalFirestoreHelper.map;
28+
import static com.google.cloud.firestore.it.TestHelper.getLargestDocContent;
2829
import static com.google.cloud.firestore.it.TestHelper.isRunningAgainstFirestoreEmulator;
2930
import static com.google.common.truth.Truth.assertThat;
3031
import static java.util.Arrays.asList;
@@ -2311,4 +2312,133 @@ public void testEnforcesTimeouts() {
23112312
FirestoreException.class,
23122313
() -> collection.document().listCollections().iterator().hasNext());
23132314
}
2315+
2316+
@Test
2317+
public void testCanCRUDAndQueryLargeDocuments() throws Exception {
2318+
// Note, do not use the 'randomColl' because its format contanis the
2319+
// test name, and that makes it difficult to correctly calculate the
2320+
// largest number of bytes we can put in the document.
2321+
CollectionReference collRef = firestore.collection(LocalFirestoreHelper.autoId());
2322+
DocumentReference docRef = collRef.document();
2323+
Map<String, Object> data = getLargestDocContent();
2324+
2325+
// Set
2326+
docRef.set(data).get();
2327+
2328+
// Get
2329+
DocumentSnapshot snapshot = docRef.get().get();
2330+
assertEquals(data, snapshot.getData());
2331+
2332+
// Update
2333+
Map<String, Object> newData = getLargestDocContent();
2334+
docRef.update(newData).get();
2335+
snapshot = docRef.get().get();
2336+
assertEquals(newData, snapshot.getData());
2337+
2338+
// Query
2339+
QuerySnapshot querySnapshot = collRef.get().get();
2340+
assertEquals(querySnapshot.size(), 1);
2341+
assertEquals(newData, querySnapshot.getDocuments().get(0).getData());
2342+
2343+
// Delete
2344+
docRef.delete().get();
2345+
snapshot = docRef.get().get();
2346+
assertFalse(snapshot.exists());
2347+
}
2348+
2349+
@Test
2350+
public void testCanCRUDLargeDocumentsInsideTransaction() throws Exception {
2351+
// Note, do not use the 'randomColl' because its format contanis the
2352+
// test name, and that makes it difficult to correctly calculate the
2353+
// largest number of bytes we can put in the document.
2354+
CollectionReference collRef = firestore.collection(LocalFirestoreHelper.autoId());
2355+
DocumentReference docRef1 = collRef.document();
2356+
DocumentReference docRef2 = collRef.document();
2357+
DocumentReference docRef3 = collRef.document();
2358+
Map<String, Object> data = getLargestDocContent();
2359+
Map<String, Object> newData = getLargestDocContent();
2360+
docRef1.set(data).get();
2361+
docRef3.set(data).get();
2362+
2363+
collRef
2364+
.getFirestore()
2365+
.runTransaction(
2366+
transaction -> {
2367+
// Get and update
2368+
DocumentSnapshot snapshot = transaction.get(docRef1).get();
2369+
assertEquals(data, snapshot.getData());
2370+
transaction.update(docRef1, newData);
2371+
2372+
// Set
2373+
transaction.set(docRef2, data);
2374+
2375+
// Delete
2376+
transaction.delete(docRef3);
2377+
return null;
2378+
})
2379+
.get();
2380+
2381+
DocumentSnapshot snapshot = docRef1.get().get();
2382+
assertEquals(newData, snapshot.getData());
2383+
2384+
snapshot = docRef2.get().get();
2385+
assertEquals(data, snapshot.getData());
2386+
2387+
snapshot = docRef3.get().get();
2388+
assertFalse(snapshot.exists());
2389+
}
2390+
2391+
@Test
2392+
public void listenToLargeQuerySnapshot() throws Exception {
2393+
// Note, do not use the 'randomColl' because its format contanis the
2394+
// test name, and that makes it difficult to correctly calculate the
2395+
// largest number of bytes we can put in the document.
2396+
CollectionReference collRef = firestore.collection(LocalFirestoreHelper.autoId());
2397+
DocumentReference docRef = collRef.document();
2398+
Map<String, Object> data = getLargestDocContent();
2399+
docRef.set(data).get();
2400+
2401+
CountDownLatch latch = new CountDownLatch(1);
2402+
List<QuerySnapshot> querySnapshots = new ArrayList<>();
2403+
ListenerRegistration registration =
2404+
collRef.addSnapshotListener(
2405+
(value, error) -> {
2406+
querySnapshots.add(value);
2407+
latch.countDown();
2408+
});
2409+
2410+
latch.await();
2411+
registration.remove();
2412+
2413+
assertEquals(querySnapshots.size(), 1);
2414+
assertEquals(querySnapshots.get(0).getDocuments().size(), 1);
2415+
assertEquals(data, querySnapshots.get(0).getDocuments().get(0).getData());
2416+
}
2417+
2418+
@Test
2419+
public void listenToLargeDocumentSnapshot() throws Exception {
2420+
// Note, do not use the 'randomColl' because its format contanis the
2421+
// test name, and that makes it difficult to correctly calculate the
2422+
// largest number of bytes we can put in the document.
2423+
CollectionReference collRef = firestore.collection(LocalFirestoreHelper.autoId());
2424+
DocumentReference docRef = collRef.document();
2425+
Map<String, Object> data = getLargestDocContent();
2426+
docRef.set(data).get();
2427+
2428+
CountDownLatch latch = new CountDownLatch(1);
2429+
List<DocumentSnapshot> documentSnapshots = new ArrayList<>();
2430+
2431+
ListenerRegistration registration =
2432+
docRef.addSnapshotListener(
2433+
(value, error) -> {
2434+
documentSnapshots.add(value);
2435+
latch.countDown();
2436+
});
2437+
2438+
latch.await();
2439+
registration.remove();
2440+
2441+
assertEquals(documentSnapshots.size(), 1);
2442+
assertEquals(data, documentSnapshots.get(0).getData());
2443+
}
23142444
}

google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/TestHelper.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
package com.google.cloud.firestore.it;
1818

1919
import com.google.api.core.ApiFuture;
20+
import com.google.cloud.firestore.Blob;
2021
import com.google.cloud.firestore.Firestore;
22+
import java.util.Collections;
23+
import java.util.Map;
24+
import java.util.Random;
2125
import java.util.concurrent.ExecutorService;
2226
import java.util.concurrent.Executors;
2327
import java.util.concurrent.atomic.AtomicBoolean;
@@ -55,4 +59,20 @@ public static void await(ApiFuture<?> future) throws InterruptedException {
5559

5660
executor.shutdown();
5761
}
62+
63+
/**
64+
* Returns a Blob with the size equal to the largest number of bytes allowed to be stored in a
65+
* Firestore document.
66+
*/
67+
public static Map<String, Object> getLargestDocContent() {
68+
int MAX_BYTES_PER_FIELD_VALUE = 1048487;
69+
// Subtract 8 for '__name__', 20 for its value, and 4 for 'blob'.
70+
int numBytesToUse = MAX_BYTES_PER_FIELD_VALUE - 7 - 20 - 4;
71+
72+
byte[] bytes = new byte[numBytesToUse];
73+
// Fill the byte array with random values
74+
Random random = new Random();
75+
random.nextBytes(bytes);
76+
return Collections.singletonMap("blob", Blob.fromBytes(bytes));
77+
}
5878
}

0 commit comments

Comments
 (0)