Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 737da876 authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Ensure BatteryUsageStats test isolation in regards to instance leak detection

If one of the unit tests failed, its BatteryUsageStats instance could
leak, only to be detected when the next test method is executed,
wrongly attributing the leak to that other test.

Bug: 403030823
Test: atest PowerStatsTests;atest PowerStatsTestsRavenwood
Flag: EXEMPT test only
Change-Id: Ifa4cb1f29559785f9de1dde709d2c24da070d51a
parent 46bca737
Loading
Loading
Loading
Loading
+23 −10
Original line number Diff line number Diff line
@@ -129,12 +129,6 @@ public final class BatteryUsageStats implements Parcelable, Closeable {
    // Max window size. CursorWindow uses only as much memory as needed.
    private static final long BATTERY_CONSUMER_CURSOR_WINDOW_SIZE = 20_000_000; // bytes

    /**
     * Used by tests to ensure all BatteryUsageStats instances are closed.
     */
    @VisibleForTesting
    public static boolean DEBUG_INSTANCE_COUNT;

    private static final int STATSD_PULL_ATOM_MAX_BYTES = 45000;

    private static final int[] UID_USAGE_TIME_PROCESS_STATES = {
@@ -1267,11 +1261,16 @@ public final class BatteryUsageStats implements Parcelable, Closeable {
        }
    }

    /*
     * Used by tests to ensure all BatteryUsageStats instances are closed.
     */
    private static volatile boolean sInstanceLeakDetectionEnabled;

    @GuardedBy("BatteryUsageStats.class")
    private static Map<CursorWindow, Exception> sInstances;

    private static void onCursorWindowAllocated(CursorWindow window) {
        if (!DEBUG_INSTANCE_COUNT) {
        if (!sInstanceLeakDetectionEnabled) {
            return;
        }

@@ -1284,7 +1283,7 @@ public final class BatteryUsageStats implements Parcelable, Closeable {
    }

    private static void onCursorWindowReleased(CursorWindow window) {
        if (!DEBUG_INSTANCE_COUNT) {
        if (!sInstanceLeakDetectionEnabled) {
            return;
        }

@@ -1293,13 +1292,27 @@ public final class BatteryUsageStats implements Parcelable, Closeable {
        }
    }

    /**
     * Enables detection of leaked BatteryUsageStats instances, meaning instances that are created
     * but not closed during the test execution.
     */
    @VisibleForTesting
    public static void enableInstanceLeakDetection() {
        sInstanceLeakDetectionEnabled = true;
        synchronized (BatteryUsageStats.class) {
            if (sInstances != null) {
                sInstances.clear();
            }
        }
    }

    /**
     * Used by tests to ensure all BatteryUsageStats instances are closed.
     */
    @VisibleForTesting
    public static void assertAllInstancesClosed() {
        if (!DEBUG_INSTANCE_COUNT) {
            throw new IllegalStateException("DEBUG_INSTANCE_COUNT is false");
        if (!sInstanceLeakDetectionEnabled) {
            throw new IllegalStateException("Instance leak detection is not enabled");
        }

        synchronized (BatteryUsageStats.class) {
+1 −1
Original line number Diff line number Diff line
@@ -340,7 +340,7 @@ public class BatteryUsageStatsRule implements TestRule {
    }

    private void before() {
        BatteryUsageStats.DEBUG_INSTANCE_COUNT = true;
        BatteryUsageStats.enableInstanceLeakDetection();
        HandlerThread bgThread = new HandlerThread("bg thread");
        bgThread.setUncaughtExceptionHandler((thread, throwable)-> {
            mThrowable = throwable;