Skip to content

Commit 87edb09

Browse files
committed
Spec tests
1 parent 2a3b2d5 commit 87edb09

25 files changed

+10747
-261
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ public void testPendingWriteTaskResolveWhenOfflineIfThereIsNoPending() {
12431243
}
12441244

12451245
@Test
1246-
public void testGetDocumentWithMemoryLruGCEnabled() {
1246+
public void testCanGetDocumentWithMemoryLruGCEnabled() {
12471247
FirebaseFirestore db = testFirestore();
12481248
db.setFirestoreSettings(
12491249
new FirebaseFirestoreSettings.Builder(db.getFirestoreSettings())
@@ -1261,7 +1261,7 @@ public void testGetDocumentWithMemoryLruGCEnabled() {
12611261
}
12621262

12631263
@Test
1264-
public void testGetDocumentWithMemoryEagerGcEnabled() {
1264+
public void testCannotGetDocumentWithMemoryEagerGcEnabled() {
12651265
FirebaseFirestore db = testFirestore();
12661266
db.setFirestoreSettings(
12671267
new FirebaseFirestoreSettings.Builder(db.getFirestoreSettings())

firebase-firestore/src/main/java/com/google/firebase/firestore/local/LruGarbageCollector.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ public static Params WithCacheSizeBytes(long cacheSizeBytes) {
6161
return new Params(cacheSizeBytes, 10, 1000);
6262
}
6363

64-
final long minBytesThreshold;
65-
final int percentileToCollect;
64+
// Not final for testing purposes.
65+
long minBytesThreshold;
66+
// Not final for testing purposes.
67+
int percentileToCollect;
6668
final int maximumSequenceNumbersToCollect;
6769

6870
Params(long minBytesThreshold, int percentileToCollect, int maximumSequenceNumbersToCollect) {
@@ -163,6 +165,13 @@ public GCScheduler newScheduler(AsyncQueue asyncQueue, LocalStore localStore) {
163165
return new GCScheduler(asyncQueue, localStore);
164166
}
165167

168+
// Visible for testing purposes only.
169+
public LruGarbageCollector withNewThreshold(long cacheThreshold) {
170+
this.params.minBytesThreshold = cacheThreshold;
171+
this.params.percentileToCollect = 100;
172+
return this;
173+
}
174+
166175
/** Given a percentile of target to collect, returns the number of targets to collect. */
167176
int calculateQueryCount(int percentile) {
168177
long targetCount = delegate.getSequenceNumberCount();

firebase-firestore/src/main/java/com/google/firebase/firestore/local/MemoryLruReferenceDelegate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import java.util.Map;
2828

2929
/** Provides LRU garbage collection functionality for MemoryPersistence. */
30-
class MemoryLruReferenceDelegate implements ReferenceDelegate, LruDelegate {
30+
public class MemoryLruReferenceDelegate implements ReferenceDelegate, LruDelegate {
3131
private final MemoryPersistence persistence;
3232
private final LocalSerializer serializer;
3333
private final Map<DocumentKey, Long> orphanedSequenceNumbers;

firebase-firestore/src/main/java/com/google/firebase/firestore/local/MemoryPersistence.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public boolean isStarted() {
8585
}
8686

8787
@Override
88-
ReferenceDelegate getReferenceDelegate() {
88+
public ReferenceDelegate getReferenceDelegate() {
8989
return referenceDelegate;
9090
}
9191

firebase-firestore/src/main/java/com/google/firebase/firestore/local/Persistence.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public abstract class Persistence {
6767

6868
public abstract boolean isStarted();
6969

70-
abstract ReferenceDelegate getReferenceDelegate();
70+
// Visible for testing purposes.
71+
public abstract ReferenceDelegate getReferenceDelegate();
7172

7273
/**
7374
* Returns a MutationQueue representing the persisted mutations for the given user.

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteLruReferenceDelegate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.util.List;
2626

2727
/** Provides LRU functionality for SQLite persistence. */
28-
class SQLiteLruReferenceDelegate implements ReferenceDelegate, LruDelegate {
28+
public class SQLiteLruReferenceDelegate implements ReferenceDelegate, LruDelegate {
2929
/**
3030
* The batch size for orphaned document GC in `removeOrphanedDocuments()`.
3131
*

firebase-firestore/src/test/java/com/google/firebase/firestore/spec/MemorySpecTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ protected boolean isExcluded(Set<String> tags) {
4040

4141
@Override
4242
protected MemoryComponentProvider initializeComponentProvider(
43-
ComponentProvider.Configuration configuration, boolean garbageCollectionEnabled) {
43+
ComponentProvider.Configuration configuration, boolean useEagerGc) {
4444
MemoryComponentProvider provider =
4545
new MemoryComponentProvider() {
4646
@Override
4747
protected Persistence createPersistence(Configuration configuration) {
48-
if (garbageCollectionEnabled) {
48+
if (useEagerGc) {
4949
return MemoryPersistence.createEagerGcMemoryPersistence();
5050
} else {
5151
DatabaseId databaseId = DatabaseId.forProject("projectId");

firebase-firestore/src/test/java/com/google/firebase/firestore/spec/SpecTestCase.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.google.common.collect.Sets;
3939
import com.google.firebase.database.collection.ImmutableSortedSet;
4040
import com.google.firebase.firestore.EventListener;
41+
import com.google.firebase.firestore.FirebaseFirestore;
4142
import com.google.firebase.firestore.FirebaseFirestoreException;
4243
import com.google.firebase.firestore.FirebaseFirestoreSettings;
4344
import com.google.firebase.firestore.LoadBundleTask;
@@ -54,9 +55,13 @@
5455
import com.google.firebase.firestore.core.Query;
5556
import com.google.firebase.firestore.core.QueryListener;
5657
import com.google.firebase.firestore.core.SyncEngine;
58+
import com.google.firebase.firestore.local.LocalStore;
59+
import com.google.firebase.firestore.local.LruGarbageCollector;
60+
import com.google.firebase.firestore.local.MemoryLruReferenceDelegate;
5761
import com.google.firebase.firestore.local.Persistence;
5862
import com.google.firebase.firestore.local.PersistenceTestHelpers;
5963
import com.google.firebase.firestore.local.QueryPurpose;
64+
import com.google.firebase.firestore.local.SQLiteLruReferenceDelegate;
6065
import com.google.firebase.firestore.local.TargetData;
6166
import com.google.firebase.firestore.model.DocumentKey;
6267
import com.google.firebase.firestore.model.MutableDocument;
@@ -158,7 +163,7 @@ public abstract class SpecTestCase implements RemoteStoreCallback {
158163
? Sets.newHashSet("no-android", "multi-client")
159164
: Sets.newHashSet("no-android", BENCHMARK_TAG, "multi-client");
160165

161-
private boolean garbageCollectionEnabled;
166+
private boolean useEagerGcForMemory;
162167
private int maxConcurrentLimboResolutions;
163168
private boolean networkEnabled = true;
164169

@@ -170,7 +175,9 @@ public abstract class SpecTestCase implements RemoteStoreCallback {
170175
private AsyncQueue queue;
171176
private MockDatastore datastore;
172177
private RemoteStore remoteStore;
178+
private LocalStore localStore;
173179
private SyncEngine syncEngine;
180+
private LruGarbageCollector lruGarbageCollector;
174181
private EventManager eventManager;
175182
private DatabaseInfo databaseInfo;
176183

@@ -268,7 +275,7 @@ protected void specSetUp(JSONObject config) {
268275

269276
outstandingWrites = new HashMap<>();
270277

271-
this.garbageCollectionEnabled = config.optBoolean("useGarbageCollection", false);
278+
this.useEagerGcForMemory = config.optBoolean("useEagerGCForMemory", true);
272279
this.maxConcurrentLimboResolutions =
273280
config.optInt("maxConcurrentLimboResolutions", Integer.MAX_VALUE);
274281

@@ -317,10 +324,19 @@ private void initClient() {
317324
maxConcurrentLimboResolutions,
318325
new FirebaseFirestoreSettings.Builder().build());
319326

320-
ComponentProvider provider =
321-
initializeComponentProvider(configuration, garbageCollectionEnabled);
327+
ComponentProvider provider = initializeComponentProvider(configuration, useEagerGcForMemory);
322328
localPersistence = provider.getPersistence();
329+
if (localPersistence.getReferenceDelegate() instanceof SQLiteLruReferenceDelegate) {
330+
lruGarbageCollector =
331+
((SQLiteLruReferenceDelegate) localPersistence.getReferenceDelegate())
332+
.getGarbageCollector();
333+
} else if (localPersistence.getReferenceDelegate() instanceof MemoryLruReferenceDelegate) {
334+
lruGarbageCollector =
335+
((MemoryLruReferenceDelegate) localPersistence.getReferenceDelegate())
336+
.getGarbageCollector();
337+
}
323338
remoteStore = provider.getRemoteStore();
339+
localStore = provider.getLocalStore();
324340
syncEngine = provider.getSyncEngine();
325341
eventManager = provider.getEventManager();
326342
}
@@ -473,6 +489,7 @@ private List<Integer> parseIntList(@Nullable JSONArray arr) throws JSONException
473489
//
474490

475491
private void doListen(JSONObject listenSpec) throws Exception {
492+
FirebaseFirestore.setLoggingEnabled(true);
476493
int expectedId = listenSpec.getInt("targetId");
477494
Query query = parseQuery(listenSpec.getJSONObject("query"));
478495
// TODO: Allow customizing listen options in spec tests
@@ -789,6 +806,15 @@ private void doChangeUser(@Nullable String uid) throws Exception {
789806
queue.runSync(() -> syncEngine.handleCredentialChange(currentUser));
790807
}
791808

809+
private void doTriggerLruGc(long cacheThreshold) throws Exception {
810+
queue.runSync(
811+
() -> {
812+
if (lruGarbageCollector != null) {
813+
localStore.collectGarbage(lruGarbageCollector.withNewThreshold(cacheThreshold));
814+
}
815+
});
816+
}
817+
792818
private void doRestart() throws Exception {
793819
queue.runSync(
794820
() -> {
@@ -861,6 +887,9 @@ private void doStep(JSONObject step) throws Exception {
861887
// "changeUser".
862888
String uid = step.isNull("changeUser") ? null : step.getString("changeUser");
863889
doChangeUser(uid);
890+
} else if (step.has("triggerLruGC")) {
891+
long cacheThreshold = step.getLong("triggerLruGC");
892+
doTriggerLruGc(cacheThreshold);
864893
} else if (step.has("restart")) {
865894
doRestart();
866895
} else if (step.has("applyClientState")) {

0 commit comments

Comments
 (0)