Fix a race in setExplicitHealthCheckEnabled() (4/n)

Sometimes the property change callback is not called within the
sleep timeout. Let's call updateConfigs() to apply device config
changes immediately to eliminate the race condition.

Bug: 178675924
Test: atest PackageWatchdogTest
Change-Id: I2b3ce79eac36cfc5ef98a62750142bb6d936e043
This commit is contained in:
JW Wang
2021-01-29 21:10:17 +08:00
parent 757da37e0b
commit f054155797
2 changed files with 11 additions and 6 deletions

View File

@@ -994,7 +994,8 @@ public class PackageWatchdog {
* Health check is enabled or disabled after reading the flags
* from DeviceConfig.
*/
private void updateConfigs() {
@VisibleForTesting
void updateConfigs() {
synchronized (mLock) {
mTriggerFailureCount = DeviceConfig.getInt(
DeviceConfig.NAMESPACE_ROLLBACK,

View File

@@ -98,6 +98,8 @@ public class PackageWatchdogTest {
private final TestClock mTestClock = new TestClock();
private TestLooper mTestLooper;
private Context mSpyContext;
// Keep track of all created watchdogs to apply device config changes
private List<PackageWatchdog> mAllocatedWatchdogs;
@Mock
private ConnectivityModuleConnector mConnectivityModuleConnector;
@Mock
@@ -166,12 +168,15 @@ public class PackageWatchdogTest {
DeviceConfig.setProperty(DeviceConfig.NAMESPACE_ROLLBACK,
PackageWatchdog.PROPERTY_WATCHDOG_TRIGGER_FAILURE_COUNT,
Integer.toString(PackageWatchdog.DEFAULT_TRIGGER_FAILURE_COUNT), false);
mAllocatedWatchdogs = new ArrayList<>();
}
@After
public void tearDown() throws Exception {
dropShellPermissions();
mSession.finishMocking();
mAllocatedWatchdogs.clear();
}
@Test
@@ -1295,11 +1300,9 @@ public class PackageWatchdogTest {
DeviceConfig.setProperty(DeviceConfig.NAMESPACE_ROLLBACK,
PackageWatchdog.PROPERTY_WATCHDOG_EXPLICIT_HEALTH_CHECK_ENABLED,
Boolean.toString(enabled), /*makeDefault*/false);
//give time for DeviceConfig to broadcast the property value change
try {
Thread.sleep(SHORT_DURATION);
} catch (InterruptedException e) {
fail("Thread.sleep unexpectedly failed!");
// Call updateConfigs() so device config changes take effect immediately
for (PackageWatchdog watchdog : mAllocatedWatchdogs) {
watchdog.updateConfigs();
}
}
@@ -1348,6 +1351,7 @@ public class PackageWatchdogTest {
verify(mConnectivityModuleConnector).registerHealthListener(
mConnectivityModuleCallbackCaptor.capture());
}
mAllocatedWatchdogs.add(watchdog);
return watchdog;
}