Skip to content

Commit ad8fd39

Browse files
authored
Enable strict mode to detect leaks of SQLite objects (#386)
In particular this will detect leaked Cursors.
1 parent 1b6c34f commit ad8fd39

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import android.content.Context;
2323
import android.net.SSLCertificateSocketFactory;
24+
import android.os.StrictMode;
2425
import android.support.test.InstrumentationRegistry;
2526
import com.google.android.gms.tasks.Task;
2627
import com.google.android.gms.tasks.TaskCompletionSource;
@@ -84,6 +85,7 @@ public class IntegrationTestUtil {
8485

8586
private static final FirestoreProvider provider = new FirestoreProvider();
8687

88+
private static boolean strictModeEnabled = false;
8789
private static boolean backendPrimed = false;
8890

8991
public static FirestoreProvider provider() {
@@ -202,6 +204,27 @@ public static FirebaseFirestore testAlternateFirestore() {
202204
return testFirestore(BAD_PROJECT_ID, Level.DEBUG, newTestSettings());
203205
}
204206

207+
/**
208+
* Enable strict mode for integration tests. Currently checks for leaked SQLite or other Closeable
209+
* objects.
210+
*
211+
* <p>If a leak is found, Android will log the leak and kill the test.
212+
*/
213+
private static void ensureStrictMode() {
214+
if (strictModeEnabled) {
215+
return;
216+
}
217+
218+
strictModeEnabled = true;
219+
StrictMode.setVmPolicy(
220+
new StrictMode.VmPolicy.Builder()
221+
.detectLeakedSqlLiteObjects()
222+
.detectLeakedClosableObjects()
223+
.penaltyLog()
224+
.penaltyDeath()
225+
.build());
226+
}
227+
205228
private static void clearPersistence(
206229
Context context, DatabaseId databaseId, String persistenceKey) {
207230
@SuppressWarnings("VisibleForTests")
@@ -228,6 +251,7 @@ public static FirebaseFirestore testFirestore(
228251
DatabaseId databaseId = DatabaseId.forDatabase(projectId, DatabaseId.DEFAULT_DATABASE_ID);
229252
String persistenceKey = "db" + firestoreStatus.size();
230253

254+
ensureStrictMode();
231255
clearPersistence(context, databaseId, persistenceKey);
232256

233257
AsyncQueue asyncQueue = null;

0 commit comments

Comments
 (0)