38
38
import com .google .common .collect .Sets ;
39
39
import com .google .firebase .database .collection .ImmutableSortedSet ;
40
40
import com .google .firebase .firestore .EventListener ;
41
+ import com .google .firebase .firestore .FirebaseFirestore ;
41
42
import com .google .firebase .firestore .FirebaseFirestoreException ;
42
43
import com .google .firebase .firestore .FirebaseFirestoreSettings ;
43
44
import com .google .firebase .firestore .LoadBundleTask ;
54
55
import com .google .firebase .firestore .core .Query ;
55
56
import com .google .firebase .firestore .core .QueryListener ;
56
57
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 ;
57
61
import com .google .firebase .firestore .local .Persistence ;
58
62
import com .google .firebase .firestore .local .PersistenceTestHelpers ;
59
63
import com .google .firebase .firestore .local .QueryPurpose ;
64
+ import com .google .firebase .firestore .local .SQLiteLruReferenceDelegate ;
60
65
import com .google .firebase .firestore .local .TargetData ;
61
66
import com .google .firebase .firestore .model .DocumentKey ;
62
67
import com .google .firebase .firestore .model .MutableDocument ;
@@ -158,7 +163,7 @@ public abstract class SpecTestCase implements RemoteStoreCallback {
158
163
? Sets .newHashSet ("no-android" , "multi-client" )
159
164
: Sets .newHashSet ("no-android" , BENCHMARK_TAG , "multi-client" );
160
165
161
- private boolean garbageCollectionEnabled ;
166
+ private boolean useEagerGcForMemory ;
162
167
private int maxConcurrentLimboResolutions ;
163
168
private boolean networkEnabled = true ;
164
169
@@ -170,7 +175,9 @@ public abstract class SpecTestCase implements RemoteStoreCallback {
170
175
private AsyncQueue queue ;
171
176
private MockDatastore datastore ;
172
177
private RemoteStore remoteStore ;
178
+ private LocalStore localStore ;
173
179
private SyncEngine syncEngine ;
180
+ private LruGarbageCollector lruGarbageCollector ;
174
181
private EventManager eventManager ;
175
182
private DatabaseInfo databaseInfo ;
176
183
@@ -268,7 +275,7 @@ protected void specSetUp(JSONObject config) {
268
275
269
276
outstandingWrites = new HashMap <>();
270
277
271
- this .garbageCollectionEnabled = config .optBoolean ("useGarbageCollection " , false );
278
+ this .useEagerGcForMemory = config .optBoolean ("useEagerGCForMemory " , true );
272
279
this .maxConcurrentLimboResolutions =
273
280
config .optInt ("maxConcurrentLimboResolutions" , Integer .MAX_VALUE );
274
281
@@ -317,10 +324,19 @@ private void initClient() {
317
324
maxConcurrentLimboResolutions ,
318
325
new FirebaseFirestoreSettings .Builder ().build ());
319
326
320
- ComponentProvider provider =
321
- initializeComponentProvider (configuration , garbageCollectionEnabled );
327
+ ComponentProvider provider = initializeComponentProvider (configuration , useEagerGcForMemory );
322
328
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
+ }
323
338
remoteStore = provider .getRemoteStore ();
339
+ localStore = provider .getLocalStore ();
324
340
syncEngine = provider .getSyncEngine ();
325
341
eventManager = provider .getEventManager ();
326
342
}
@@ -473,6 +489,7 @@ private List<Integer> parseIntList(@Nullable JSONArray arr) throws JSONException
473
489
//
474
490
475
491
private void doListen (JSONObject listenSpec ) throws Exception {
492
+ FirebaseFirestore .setLoggingEnabled (true );
476
493
int expectedId = listenSpec .getInt ("targetId" );
477
494
Query query = parseQuery (listenSpec .getJSONObject ("query" ));
478
495
// TODO: Allow customizing listen options in spec tests
@@ -789,6 +806,15 @@ private void doChangeUser(@Nullable String uid) throws Exception {
789
806
queue .runSync (() -> syncEngine .handleCredentialChange (currentUser ));
790
807
}
791
808
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
+
792
818
private void doRestart () throws Exception {
793
819
queue .runSync (
794
820
() -> {
@@ -861,6 +887,9 @@ private void doStep(JSONObject step) throws Exception {
861
887
// "changeUser".
862
888
String uid = step .isNull ("changeUser" ) ? null : step .getString ("changeUser" );
863
889
doChangeUser (uid );
890
+ } else if (step .has ("triggerLruGC" )) {
891
+ long cacheThreshold = step .getLong ("triggerLruGC" );
892
+ doTriggerLruGc (cacheThreshold );
864
893
} else if (step .has ("restart" )) {
865
894
doRestart ();
866
895
} else if (step .has ("applyClientState" )) {
0 commit comments