Merge "Only sync requests when there are changes to report" into rvc-dev am: 1714207393
Change-Id: I2776eecc1368f8b49fcdb419f66924fbf8607a34
This commit is contained in:
@@ -161,6 +161,9 @@ public class PackageWatchdog {
|
|||||||
private final Runnable mSaveToFile = this::saveToFile;
|
private final Runnable mSaveToFile = this::saveToFile;
|
||||||
private final SystemClock mSystemClock;
|
private final SystemClock mSystemClock;
|
||||||
private final BootThreshold mBootThreshold;
|
private final BootThreshold mBootThreshold;
|
||||||
|
// The set of packages that have been synced with the ExplicitHealthCheckController
|
||||||
|
@GuardedBy("mLock")
|
||||||
|
private Set<String> mRequestedHealthCheckPackages = new ArraySet<>();
|
||||||
@GuardedBy("mLock")
|
@GuardedBy("mLock")
|
||||||
private boolean mIsPackagesReady;
|
private boolean mIsPackagesReady;
|
||||||
// Flag to control whether explicit health checks are supported or not
|
// Flag to control whether explicit health checks are supported or not
|
||||||
@@ -624,17 +627,22 @@ public class PackageWatchdog {
|
|||||||
* @see #syncRequestsAsync
|
* @see #syncRequestsAsync
|
||||||
*/
|
*/
|
||||||
private void syncRequests() {
|
private void syncRequests() {
|
||||||
Set<String> packages = null;
|
boolean syncRequired = false;
|
||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
if (mIsPackagesReady) {
|
if (mIsPackagesReady) {
|
||||||
packages = getPackagesPendingHealthChecksLocked();
|
Set<String> packages = getPackagesPendingHealthChecksLocked();
|
||||||
|
if (!packages.equals(mRequestedHealthCheckPackages)) {
|
||||||
|
syncRequired = true;
|
||||||
|
mRequestedHealthCheckPackages = packages;
|
||||||
|
}
|
||||||
} // else, we will sync requests when packages become ready
|
} // else, we will sync requests when packages become ready
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call outside lock to avoid holding lock when calling into the controller.
|
// Call outside lock to avoid holding lock when calling into the controller.
|
||||||
if (packages != null) {
|
if (syncRequired) {
|
||||||
Slog.i(TAG, "Syncing health check requests for packages: " + packages);
|
Slog.i(TAG, "Syncing health check requests for packages: "
|
||||||
mHealthCheckController.syncRequests(packages);
|
+ mRequestedHealthCheckPackages);
|
||||||
|
mHealthCheckController.syncRequests(mRequestedHealthCheckPackages);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1063,6 +1063,52 @@ public class PackageWatchdogTest {
|
|||||||
assertThat(bootObserver2.mitigatedBootLoop()).isFalse();
|
assertThat(bootObserver2.mitigatedBootLoop()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test to verify that Package Watchdog syncs health check requests with the controller
|
||||||
|
* correctly, and that the requests are only synced when the set of observed packages
|
||||||
|
* changes.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testSyncHealthCheckRequests() {
|
||||||
|
TestController testController = spy(TestController.class);
|
||||||
|
testController.setSupportedPackages(List.of(APP_A, APP_B, APP_C));
|
||||||
|
PackageWatchdog watchdog = createWatchdog(testController, true);
|
||||||
|
|
||||||
|
TestObserver testObserver1 = new TestObserver(OBSERVER_NAME_1);
|
||||||
|
watchdog.registerHealthObserver(testObserver1);
|
||||||
|
watchdog.startObservingHealth(testObserver1, List.of(APP_A), LONG_DURATION);
|
||||||
|
mTestLooper.dispatchAll();
|
||||||
|
|
||||||
|
TestObserver testObserver2 = new TestObserver(OBSERVER_NAME_2);
|
||||||
|
watchdog.registerHealthObserver(testObserver2);
|
||||||
|
watchdog.startObservingHealth(testObserver2, List.of(APP_B), LONG_DURATION);
|
||||||
|
mTestLooper.dispatchAll();
|
||||||
|
|
||||||
|
TestObserver testObserver3 = new TestObserver(OBSERVER_NAME_3);
|
||||||
|
watchdog.registerHealthObserver(testObserver3);
|
||||||
|
watchdog.startObservingHealth(testObserver3, List.of(APP_C), LONG_DURATION);
|
||||||
|
mTestLooper.dispatchAll();
|
||||||
|
|
||||||
|
watchdog.unregisterHealthObserver(testObserver1);
|
||||||
|
mTestLooper.dispatchAll();
|
||||||
|
|
||||||
|
watchdog.unregisterHealthObserver(testObserver2);
|
||||||
|
mTestLooper.dispatchAll();
|
||||||
|
|
||||||
|
watchdog.unregisterHealthObserver(testObserver3);
|
||||||
|
mTestLooper.dispatchAll();
|
||||||
|
|
||||||
|
List<Set> expectedSyncRequests = List.of(
|
||||||
|
Set.of(APP_A),
|
||||||
|
Set.of(APP_A, APP_B),
|
||||||
|
Set.of(APP_A, APP_B, APP_C),
|
||||||
|
Set.of(APP_B, APP_C),
|
||||||
|
Set.of(APP_C),
|
||||||
|
Set.of()
|
||||||
|
);
|
||||||
|
assertThat(testController.getSyncRequests()).isEqualTo(expectedSyncRequests);
|
||||||
|
}
|
||||||
|
|
||||||
private void adoptShellPermissions(String... permissions) {
|
private void adoptShellPermissions(String... permissions) {
|
||||||
InstrumentationRegistry
|
InstrumentationRegistry
|
||||||
.getInstrumentation()
|
.getInstrumentation()
|
||||||
@@ -1219,6 +1265,7 @@ public class PackageWatchdogTest {
|
|||||||
private Consumer<String> mPassedConsumer;
|
private Consumer<String> mPassedConsumer;
|
||||||
private Consumer<List<PackageConfig>> mSupportedConsumer;
|
private Consumer<List<PackageConfig>> mSupportedConsumer;
|
||||||
private Runnable mNotifySyncRunnable;
|
private Runnable mNotifySyncRunnable;
|
||||||
|
private List<Set> mSyncRequests = new ArrayList<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setEnabled(boolean enabled) {
|
public void setEnabled(boolean enabled) {
|
||||||
@@ -1238,6 +1285,7 @@ public class PackageWatchdogTest {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void syncRequests(Set<String> packages) {
|
public void syncRequests(Set<String> packages) {
|
||||||
|
mSyncRequests.add(packages);
|
||||||
mRequestedPackages.clear();
|
mRequestedPackages.clear();
|
||||||
if (mIsEnabled) {
|
if (mIsEnabled) {
|
||||||
packages.retainAll(mSupportedPackages);
|
packages.retainAll(mSupportedPackages);
|
||||||
@@ -1268,6 +1316,10 @@ public class PackageWatchdogTest {
|
|||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Set> getSyncRequests() {
|
||||||
|
return mSyncRequests;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class TestClock implements PackageWatchdog.SystemClock {
|
private static class TestClock implements PackageWatchdog.SystemClock {
|
||||||
|
|||||||
Reference in New Issue
Block a user