|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.firestore; |
| 16 | + |
| 17 | +import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testFirestore; |
| 18 | +import static com.google.firebase.firestore.testutil.IntegrationTestUtil.waitFor; |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertTrue; |
| 21 | + |
| 22 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 23 | +import com.google.android.gms.tasks.Task; |
| 24 | +import java.util.Arrays; |
| 25 | +import org.junit.After; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | + |
| 29 | +@RunWith(AndroidJUnit4.class) |
| 30 | +public class LargeDocumentTest { |
| 31 | + |
| 32 | + // These tests require a pre-seeded database containing specific large documents. |
| 33 | + private static final String SEED_COLLECTION = "serverSdkTests"; |
| 34 | + private static final String DOC_15_9MB_UNICODE = "doc_15_9MB_unicode"; |
| 35 | + private static final String COL_LARGE_DOCS = "col_large_docs"; |
| 36 | + |
| 37 | + // Extended timeout required for large document tests due to gRPC flow control |
| 38 | + // window defaults (64KB), which result in longer read times over the network. |
| 39 | + private static final int TIMEOUT_MS = 60000; |
| 40 | + |
| 41 | + @After |
| 42 | + public void tearDown() { |
| 43 | + com.google.firebase.firestore.testutil.IntegrationTestUtil.tearDown(); |
| 44 | + } |
| 45 | + |
| 46 | + private String generateString(int sizeInBytes) { |
| 47 | + char[] chars = new char[sizeInBytes]; |
| 48 | + Arrays.fill(chars, 'a'); |
| 49 | + return new String(chars); |
| 50 | + } |
| 51 | + |
| 52 | + @Test(timeout = TIMEOUT_MS) |
| 53 | + public void testReadAndCacheLargeUnicodeDocument() { |
| 54 | + FirebaseFirestore db = testFirestore(); |
| 55 | + DocumentReference docRef = db.collection(SEED_COLLECTION).document(DOC_15_9MB_UNICODE); |
| 56 | + |
| 57 | + DocumentSnapshot serverSnapshot = waitFor(docRef.get(Source.SERVER)); |
| 58 | + assertTrue(serverSnapshot.exists()); |
| 59 | + |
| 60 | + waitFor(db.disableNetwork()); |
| 61 | + |
| 62 | + DocumentSnapshot cacheSnapshot = waitFor(docRef.get(Source.CACHE)); |
| 63 | + assertTrue(cacheSnapshot.exists()); |
| 64 | + |
| 65 | + assertEquals(serverSnapshot.getData(), cacheSnapshot.getData()); |
| 66 | + |
| 67 | + waitFor(db.enableNetwork()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test(timeout = TIMEOUT_MS) |
| 71 | + public void testCacheIntegrityWithMultipleLargeDocuments() { |
| 72 | + FirebaseFirestore db = testFirestore(); |
| 73 | + |
| 74 | + // Copy existing test environment settings but set a normal cache size |
| 75 | + // to ensure we don't accidentally trigger async GC during the test. |
| 76 | + FirebaseFirestoreSettings existingSettings = db.getFirestoreSettings(); |
| 77 | + FirebaseFirestoreSettings settings = |
| 78 | + new FirebaseFirestoreSettings.Builder(existingSettings) |
| 79 | + .setLocalCacheSettings( |
| 80 | + PersistentCacheSettings.newBuilder().setSizeBytes(104857600).build()) // 100MB |
| 81 | + .build(); |
| 82 | + db.setFirestoreSettings(settings); |
| 83 | + |
| 84 | + CollectionReference colRef = db.collection(COL_LARGE_DOCS); |
| 85 | + DocumentReference docA = colRef.document("doc_a"); |
| 86 | + DocumentReference docB = colRef.document("doc_b"); |
| 87 | + |
| 88 | + waitFor(docA.get(Source.SERVER)); |
| 89 | + waitFor(docB.get(Source.SERVER)); |
| 90 | + |
| 91 | + waitFor(db.disableNetwork()); |
| 92 | + |
| 93 | + DocumentSnapshot cacheSnapshotA = waitFor(docA.get(Source.CACHE)); |
| 94 | + DocumentSnapshot cacheSnapshotB = waitFor(docB.get(Source.CACHE)); |
| 95 | + |
| 96 | + assertTrue("docA should exist in cache", cacheSnapshotA.exists()); |
| 97 | + assertTrue("docB should exist in cache", cacheSnapshotB.exists()); |
| 98 | + |
| 99 | + // Sanity check |
| 100 | + assertTrue(cacheSnapshotA.getData().size() > 0); |
| 101 | + assertTrue(cacheSnapshotB.getData().size() > 0); |
| 102 | + |
| 103 | + waitFor(db.enableNetwork()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test(timeout = TIMEOUT_MS) |
| 107 | + public void testWatchStreamInitializationAndDiff() throws Exception { |
| 108 | + FirebaseFirestore db = testFirestore(); |
| 109 | + DocumentReference docRef = db.collection(SEED_COLLECTION).document(DOC_15_9MB_UNICODE); |
| 110 | + |
| 111 | + // Verify that the initial snapshot of a large document is received successfully |
| 112 | + // without triggering stream cancellation loops. |
| 113 | + Task<DocumentSnapshot> firstSnapshotTask = docRef.get(Source.SERVER); |
| 114 | + DocumentSnapshot firstSnapshot = waitFor(firstSnapshotTask); |
| 115 | + assertTrue(firstSnapshot.exists()); |
| 116 | + |
| 117 | + // TODO: Enable the differential update assertions below once client SDK write streams |
| 118 | + // support the 16MB limit. |
| 119 | + /* |
| 120 | + Map<String, Object> updateData = new HashMap<>(); |
| 121 | + updateData.put("differential_field", "updated_value"); |
| 122 | + waitFor(docRef.update(updateData)); |
| 123 | +
|
| 124 | + // Wait for the snapshot listener to fire a second time to verify stream continuity. |
| 125 | + */ |
| 126 | + } |
| 127 | + |
| 128 | + // TODO: Enable this test. Currently it times out after not receiving a response from the backend. |
| 129 | + /* |
| 130 | + @Test(timeout = TIMEOUT_MS) |
| 131 | + public void testOversizedPayloadRejection() { |
| 132 | + FirebaseFirestore db = testFirestore(); |
| 133 | + DocumentReference docRef = db.collection(SEED_COLLECTION).document("temp_oversized_doc"); |
| 134 | +
|
| 135 | + Map<String, Object> data = new HashMap<>(); |
| 136 | + // 16.1MB payload |
| 137 | + int oversizedPayloadBytes = (16 * 1024 * 1024) + 102400; |
| 138 | + data.put("largeField", generateString(oversizedPayloadBytes)); |
| 139 | +
|
| 140 | + try { |
| 141 | + waitFor(docRef.set(data)); |
| 142 | + fail("Setting a document exceeding the maximum size limit should fail."); |
| 143 | + } catch (Exception e) { |
| 144 | + assertTrue(e.getCause() instanceof FirebaseFirestoreException); |
| 145 | + FirebaseFirestoreException firestoreException = (FirebaseFirestoreException) e.getCause(); |
| 146 | +
|
| 147 | + assertEquals(Code.INVALID_ARGUMENT, firestoreException.getCode()); |
| 148 | + } |
| 149 | + } |
| 150 | + */ |
| 151 | + |
| 152 | + @Test(timeout = TIMEOUT_MS) |
| 153 | + public void testTransactionReadModifyWrite() { |
| 154 | + FirebaseFirestore db = testFirestore(); |
| 155 | + DocumentReference docRef = db.collection(SEED_COLLECTION).document(DOC_15_9MB_UNICODE); |
| 156 | + |
| 157 | + Task<Void> transactionTask = |
| 158 | + db.runTransaction( |
| 159 | + transaction -> { |
| 160 | + DocumentSnapshot snapshot = transaction.get(docRef); |
| 161 | + assertTrue(snapshot.exists()); |
| 162 | + |
| 163 | + transaction.update(docRef, "transaction_timestamp", System.currentTimeMillis()); |
| 164 | + return null; |
| 165 | + }); |
| 166 | + |
| 167 | + waitFor(transactionTask); |
| 168 | + } |
| 169 | + |
| 170 | + @Test(timeout = TIMEOUT_MS) |
| 171 | + public void testQueryLargeDocuments() { |
| 172 | + FirebaseFirestore db = testFirestore(); |
| 173 | + CollectionReference colRef = db.collection(COL_LARGE_DOCS); |
| 174 | + |
| 175 | + Query query = colRef.whereIn(FieldPath.documentId(), Arrays.asList("doc_a", "doc_b")); |
| 176 | + |
| 177 | + QuerySnapshot serverSnapshot = waitFor(query.get(Source.SERVER)); |
| 178 | + assertEquals( |
| 179 | + "Query should return exactly 2 large documents from server", 2, serverSnapshot.size()); |
| 180 | + |
| 181 | + waitFor(db.disableNetwork()); |
| 182 | + |
| 183 | + QuerySnapshot cacheSnapshot = waitFor(query.get(Source.CACHE)); |
| 184 | + assertEquals( |
| 185 | + "Query should return exactly 2 large documents from cache", 2, cacheSnapshot.size()); |
| 186 | + |
| 187 | + assertEquals( |
| 188 | + "Cached query payload should exactly match server query payload", |
| 189 | + serverSnapshot.getDocuments().get(0).getData(), |
| 190 | + cacheSnapshot.getDocuments().get(0).getData()); |
| 191 | + |
| 192 | + waitFor(db.enableNetwork()); |
| 193 | + } |
| 194 | + |
| 195 | + @Test(timeout = TIMEOUT_MS) |
| 196 | + public void testQueryLargeDocumentsForcesLocalScan() { |
| 197 | + FirebaseFirestore db = testFirestore(); |
| 198 | + CollectionReference colRef = db.collection(COL_LARGE_DOCS); |
| 199 | + |
| 200 | + waitFor(colRef.document("doc_a").get(Source.SERVER)); |
| 201 | + waitFor(colRef.document("doc_b").get(Source.SERVER)); |
| 202 | + |
| 203 | + waitFor(db.disableNetwork()); |
| 204 | + |
| 205 | + Query query = colRef.orderBy(FieldPath.documentId()).limit(2); |
| 206 | + |
| 207 | + // Execute the query offline |
| 208 | + QuerySnapshot cacheSnapshot = waitFor(query.get(Source.CACHE)); |
| 209 | + |
| 210 | + assertEquals( |
| 211 | + "Query should find and return exactly 2 large documents from cache", |
| 212 | + 2, |
| 213 | + cacheSnapshot.size()); |
| 214 | + |
| 215 | + assertTrue( |
| 216 | + "Payload should not be empty", cacheSnapshot.getDocuments().get(0).getData().size() > 0); |
| 217 | + |
| 218 | + waitFor(db.enableNetwork()); |
| 219 | + } |
| 220 | +} |
0 commit comments