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

Commit dfe1bc2f authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Fix a bug where the package stats query timeout may crash." into nyc-mr2-dev

parents f6d8e1f6 cf76a16e
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -106,7 +106,7 @@ public class DiskStatsLoggingService extends JobService {

    @VisibleForTesting
    static class LogRunnable implements Runnable {
        private static final long TIMEOUT_MILLIS = TimeUnit.MINUTES.toMillis(5);
        private static final long TIMEOUT_MILLIS = TimeUnit.MINUTES.toMillis(10);

        private JobService mJobService;
        private JobParameters mParams;
@@ -147,11 +147,17 @@ public class DiskStatsLoggingService extends JobService {
            FileCollector.MeasurementResult downloads =
                    FileCollector.getMeasurementResult(mDownloadsDirectory);

            logToFile(mainCategories, downloads, mCollector.getPackageStats(TIMEOUT_MILLIS),
                    mSystemSize);
            boolean needsReschedule = true;
            List<PackageStats> stats = mCollector.getPackageStats(TIMEOUT_MILLIS);
            if (stats != null) {
                needsReschedule = false;
                logToFile(mainCategories, downloads, stats, mSystemSize);
            } else {
                Log.w("TAG", "Timed out while fetching package stats.");
            }

            if (mJobService != null) {
                mJobService.jobFinished(mParams, false);
                mJobService.jobFinished(mParams, needsReschedule);
            }
        }

+21 −0
Original line number Diff line number Diff line
@@ -18,9 +18,15 @@ package com.android.server.storage;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.job.JobService;
import android.app.job.JobParameters;
import android.content.pm.PackageStats;
import android.test.AndroidTestCase;

@@ -130,6 +136,21 @@ public class DiskStatsLoggingServiceTest extends AndroidTestCase {
        assertThat(json.getJSONArray(DiskStatsFileLogger.APP_CACHES_KEY).length()).isEqualTo(1L);
    }

    @Test
    public void testDontCrashOnPackageStatsTimeout() throws Exception {
        when(mCollector.getPackageStats(anyInt())).thenReturn(null);

        LogRunnable task = new LogRunnable();
        task.setAppCollector(mCollector);
        task.setDownloadsDirectory(mDownloads.getRoot());
        task.setRootDirectory(mRootDirectory.getRoot());
        task.setLogOutputFile(mInputFile);
        task.setSystemSize(10L);
        task.run();

        // No exception should be thrown.
    }

    private void writeDataToFile(File f, String data) throws Exception{
        PrintStream out = new PrintStream(f);
        out.print(data);