Loading services/core/java/com/android/server/PackageWatchdog.java +8 −4 Original line number Original line Diff line number Diff line Loading @@ -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; Loading Loading @@ -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) { Loading @@ -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<>(); Loading tests/PackageWatchdog/src/com/android/server/PackageWatchdogTest.java +39 −0 Original line number Original line Diff line number Diff line Loading @@ -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() { Loading Loading
services/core/java/com/android/server/PackageWatchdog.java +8 −4 Original line number Original line Diff line number Diff line Loading @@ -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; Loading Loading @@ -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) { Loading @@ -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<>(); Loading
tests/PackageWatchdog/src/com/android/server/PackageWatchdogTest.java +39 −0 Original line number Original line Diff line number Diff line Loading @@ -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() { Loading