From 374719b8d717b2952caeebda9cd1da8f0382c158 Mon Sep 17 00:00:00 2001 From: Kweku Adams Date: Thu, 28 Feb 2019 13:53:21 -0800 Subject: [PATCH] Fixing flakiness of AppStandbyController test. Since AppStandbyController uses a handler to notify listeners of a state change, there is some inherent latency between changing the state and getting the notification. testEnabledState needs to wait until the paroleListener has been notified that parole is on before continuing with the test. In order to test this, I added Thread.sleep(27) to informParoleStateChanged. Without this change, the test fails every few iterations. With the change, the test doesn't fail. Bug: 119774928 Test: for i in `seq 10`; do atest com.android.server.usage.AppStandbyControllerTests#testEnabledState; done Change-Id: Ic5c7313d6bca18c966837ba357615aa4619edec6 --- .../usage/AppStandbyControllerTests.java | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/services/tests/servicestests/src/com/android/server/usage/AppStandbyControllerTests.java b/services/tests/servicestests/src/com/android/server/usage/AppStandbyControllerTests.java index 8171469d4da4d..6ec864c052588 100644 --- a/services/tests/servicestests/src/com/android/server/usage/AppStandbyControllerTests.java +++ b/services/tests/servicestests/src/com/android/server/usage/AppStandbyControllerTests.java @@ -61,7 +61,6 @@ import android.util.ArraySet; import android.view.Display; import androidx.test.InstrumentationRegistry; -import androidx.test.filters.FlakyTest; import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; @@ -323,6 +322,8 @@ public class AppStandbyControllerTests { private boolean mOnParole = false; private CountDownLatch mLatch; private long mLastParoleChangeTime; + private boolean mIsExpecting = false; + private boolean mExpectedParoleState; public boolean getParoleState() { synchronized (this) { @@ -333,6 +334,15 @@ public class AppStandbyControllerTests { public void rearmLatch() { synchronized (this) { mLatch = new CountDownLatch(1); + mIsExpecting = false; + } + } + + public void rearmLatch(boolean expectedParoleState) { + synchronized (this) { + mLatch = new CountDownLatch(1); + mIsExpecting = true; + mExpectedParoleState = expectedParoleState; } } @@ -358,7 +368,9 @@ public class AppStandbyControllerTests { if (mLatch != null && mLatch.getCount() > 0) { mOnParole = isParoleOn; mLastParoleChangeTime = getCurrentTime(); - mLatch.countDown(); + if (!mIsExpecting || isParoleOn == mExpectedParoleState) { + mLatch.countDown(); + } } } } @@ -417,17 +429,23 @@ public class AppStandbyControllerTests { } @Test - @FlakyTest(bugId = 119774928) public void testEnabledState() throws Exception { TestParoleListener paroleListener = new TestParoleListener(); + paroleListener.rearmLatch(true); mController.addListener(paroleListener); long lastUpdateTime; // Test that listeners are notified if enabled changes when the device is not in parole. setChargingState(mController, false); - // Start off not enabled. Device is effectively on permanent parole. + // Start off not enabled. Device is effectively in permanent parole. setAppIdleEnabled(mController, false); + // Since AppStandbyController uses a handler to notify listeners of a state change, there is + // some inherent latency between changing the state and getting the notification. We need to + // wait until the paroleListener has been notified that parole is on before continuing with + // the test. + paroleListener.awaitOnLatch(STABLE_CHARGING_THRESHOLD * 3 / 2); + assertTrue(paroleListener.mOnParole); // Enable controller paroleListener.rearmLatch();