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

Commit 86926216 authored by Chun-Wei Wang's avatar Chun-Wei Wang Committed by Android (Google) Code Review
Browse files

Merge "Use a default value in case of an invalid parameter"

parents e33dcad8 e0f2f3d8
Loading
Loading
Loading
Loading
+8 −4
Original line number Original line Diff line number Diff line
@@ -87,6 +87,8 @@ public class PackageWatchdog {
    // Number of package failures within the duration above before we notify observers
    // Number of package failures within the duration above before we notify observers
    @VisibleForTesting
    @VisibleForTesting
    static final int DEFAULT_TRIGGER_FAILURE_COUNT = 5;
    static final int DEFAULT_TRIGGER_FAILURE_COUNT = 5;
    @VisibleForTesting
    static final long DEFAULT_OBSERVING_DURATION_MS = TimeUnit.DAYS.toMillis(2);
    // Whether explicit health checks are enabled or not
    // Whether explicit health checks are enabled or not
    private static final boolean DEFAULT_EXPLICIT_HEALTH_CHECK_ENABLED = true;
    private static final boolean DEFAULT_EXPLICIT_HEALTH_CHECK_ENABLED = true;


@@ -225,8 +227,10 @@ public class PackageWatchdog {
     * check state will be reset to a default depending on if the package is contained in
     * check state will be reset to a default depending on if the package is contained in
     * {@link mPackagesWithExplicitHealthCheckEnabled}.
     * {@link mPackagesWithExplicitHealthCheckEnabled}.
     *
     *
     * @throws IllegalArgumentException if {@code packageNames} is empty
     * <p>If {@code packageNames} is empty, this will be a no-op.
     * or {@code durationMs} is less than 1
     *
     * <p>If {@code durationMs} is less than 1, a default monitoring duration
     * {@link #DEFAULT_OBSERVING_DURATION_MS} will be used.
     */
     */
    public void startObservingHealth(PackageHealthObserver observer, List<String> packageNames,
    public void startObservingHealth(PackageHealthObserver observer, List<String> packageNames,
            long durationMs) {
            long durationMs) {
@@ -235,9 +239,9 @@ public class PackageWatchdog {
            return;
            return;
        }
        }
        if (durationMs < 1) {
        if (durationMs < 1) {
            // TODO: Instead of failing, monitor for default? 48hrs?
            Slog.wtf(TAG, "Invalid duration " + durationMs + "ms for observer "
            throw new IllegalArgumentException("Invalid duration " + durationMs + "ms for observer "
                    + observer.getName() + ". Not observing packages " + packageNames);
                    + observer.getName() + ". Not observing packages " + packageNames);
            durationMs = DEFAULT_OBSERVING_DURATION_MS;
        }
        }


        List<MonitoredPackage> packages = new ArrayList<>();
        List<MonitoredPackage> packages = new ArrayList<>();
+39 −0
Original line number Original line Diff line number Diff line
@@ -765,6 +765,45 @@ public class PackageWatchdogTest {
        assertThat(observer.mHealthCheckFailedPackages).containsExactly(APP_B);
        assertThat(observer.mHealthCheckFailedPackages).containsExactly(APP_B);
    }
    }


    /**
     * Test default monitoring duration is used when PackageWatchdog#startObservingHealth is offered
     * an invalid durationMs.
     */
    @Test
    public void testInvalidMonitoringDuration_beforeExpiry() {
        PackageWatchdog watchdog = createWatchdog();
        TestObserver observer = new TestObserver(OBSERVER_NAME_1);

        watchdog.startObservingHealth(observer, Arrays.asList(APP_A), -1);
        // Note: Don't move too close to the expiration time otherwise the handler will be thrashed
        // by PackageWatchdog#scheduleNextSyncStateLocked which keeps posting runnables with very
        // small timeouts.
        moveTimeForwardAndDispatch(PackageWatchdog.DEFAULT_OBSERVING_DURATION_MS - 100);
        raiseFatalFailureAndDispatch(watchdog,
                Arrays.asList(new VersionedPackage(APP_A, VERSION_CODE)));

        // We should receive APP_A since the observer hasn't expired
        assertThat(observer.mHealthCheckFailedPackages).containsExactly(APP_A);
    }

    /**
     * Test default monitoring duration is used when PackageWatchdog#startObservingHealth is offered
     * an invalid durationMs.
     */
    @Test
    public void testInvalidMonitoringDuration_afterExpiry() {
        PackageWatchdog watchdog = createWatchdog();
        TestObserver observer = new TestObserver(OBSERVER_NAME_1);

        watchdog.startObservingHealth(observer, Arrays.asList(APP_A), -1);
        moveTimeForwardAndDispatch(PackageWatchdog.DEFAULT_OBSERVING_DURATION_MS + 1);
        raiseFatalFailureAndDispatch(watchdog,
                Arrays.asList(new VersionedPackage(APP_A, VERSION_CODE)));

        // We should receive nothing since the observer has expired
        assertThat(observer.mHealthCheckFailedPackages).isEmpty();
    }

    /** Test we are notified when enough failures are triggered within any window. */
    /** Test we are notified when enough failures are triggered within any window. */
    @Test
    @Test
    public void testFailureTriggerWindow() {
    public void testFailureTriggerWindow() {