From 75266fb9d258bd859fb8f9fee89648284dc1bf9a Mon Sep 17 00:00:00 2001 From: Yasin Kilicdere Date: Wed, 4 Jan 2023 14:17:40 +0000 Subject: Force stop a user before removing it in perf tests. Removing users immediately after starting them was causing system crashes on other parts of the Android. In removeUsers() it was waited for ACTION_MEDIA_MOUNTED before removing started users. But it was deciding whether a user has been started by checking if ACTION_USER_STARTED broadcast was received for that user. Depending on a broadcast was wrong because it might not have been received in time. Changing that part with UM.isUserRunning API would fix the issue but this time since the user might be started and stopped multiple times during the tests, checking for ACTION_MEDIA_MOUNTED was making the code too complicated since it could be received multiple times. This CL simply force stops the user before removing it to solve the problem. Bug: 262407660 Bug: 236229621 Bug: 233240023 Test: atest UserLifecycleTests Change-Id: Ica715e88d88761d969d02c761ea845d5b82b17e9 --- .../src/android/multiuser/BroadcastWaiter.java | 12 ------------ .../src/android/multiuser/UserLifecycleTests.java | 18 ++++++++---------- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/apct-tests/perftests/multiuser/src/android/multiuser/BroadcastWaiter.java b/apct-tests/perftests/multiuser/src/android/multiuser/BroadcastWaiter.java index dcabca476925..727c682b5202 100644 --- a/apct-tests/perftests/multiuser/src/android/multiuser/BroadcastWaiter.java +++ b/apct-tests/perftests/multiuser/src/android/multiuser/BroadcastWaiter.java @@ -45,7 +45,6 @@ public class BroadcastWaiter implements Closeable { private final int mTimeoutInSecond; private final Set mActions; - private final Set mActionReceivedForUser = new HashSet<>(); private final List mBroadcastReceivers = new ArrayList<>(); private final Map mSemaphoresMap = new ConcurrentHashMap<>(); @@ -80,7 +79,6 @@ public class BroadcastWaiter implements Closeable { final String data = intent.getDataString(); Log.d(mTag, "Received " + action + " for user " + userId + (!TextUtils.isEmpty(data) ? " with " + data : "")); - mActionReceivedForUser.add(action + userId); getSemaphore(action, userId).release(); } } @@ -95,10 +93,6 @@ public class BroadcastWaiter implements Closeable { mBroadcastReceivers.forEach(mContext::unregisterReceiver); } - public boolean hasActionBeenReceivedForUser(String action, int userId) { - return mActionReceivedForUser.contains(action + userId); - } - private boolean waitActionForUser(String action, int userId) { Log.d(mTag, "#waitActionForUser(action: " + action + ", userId: " + userId + ")"); @@ -129,7 +123,6 @@ public class BroadcastWaiter implements Closeable { public String runThenWaitForBroadcasts(int userId, FunctionalUtils.ThrowingRunnable runnable, String... actions) { for (String action : actions) { - mActionReceivedForUser.remove(action + userId); getSemaphore(action, userId).drainPermits(); } runnable.run(); @@ -140,9 +133,4 @@ public class BroadcastWaiter implements Closeable { } return null; } - - public boolean waitActionForUserIfNotReceivedYet(String action, int userId) { - return hasActionBeenReceivedForUser(action, userId) - || waitActionForUser(action, userId); - } } diff --git a/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java b/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java index b2bd8d7f5d5d..3f9b54cb8578 100644 --- a/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java +++ b/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java @@ -167,7 +167,7 @@ public class UserLifecycleTests { /** Tests creating a new user. */ @Test(timeout = TIMEOUT_MAX_TEST_TIME_MS) - public void createUser() { + public void createUser() throws RemoteException { while (mRunner.keepRunning()) { Log.i(TAG, "Starting timer"); final int userId = createUserNoFlags(); @@ -229,7 +229,7 @@ public class UserLifecycleTests { * Measures the time until unlock listener is triggered and user is unlocked. */ @Test(timeout = TIMEOUT_MAX_TEST_TIME_MS) - public void startAndUnlockUser() { + public void startAndUnlockUser() throws RemoteException { while (mRunner.keepRunning()) { mRunner.pauseTiming(); final int userId = createUserNoFlags(); @@ -451,7 +451,7 @@ public class UserLifecycleTests { /** Tests creating a new profile. */ @Test(timeout = TIMEOUT_MAX_TEST_TIME_MS) - public void managedProfileCreate() { + public void managedProfileCreate() throws RemoteException { assumeTrue(mHasManagedUserFeature); while (mRunner.keepRunning()) { @@ -468,7 +468,7 @@ public class UserLifecycleTests { /** Tests starting (unlocking) an uninitialized profile. */ @Test(timeout = TIMEOUT_MAX_TEST_TIME_MS) - public void managedProfileUnlock() { + public void managedProfileUnlock() throws RemoteException { assumeTrue(mHasManagedUserFeature); while (mRunner.keepRunning()) { @@ -639,7 +639,7 @@ public class UserLifecycleTests { // TODO: This is just a POC. Do this properly and add more. /** Tests starting (unlocking) a newly-created profile using the user-type-pkg-whitelist. */ @Test(timeout = TIMEOUT_MAX_TEST_TIME_MS) - public void managedProfileUnlock_usingWhitelist() { + public void managedProfileUnlock_usingWhitelist() throws RemoteException { assumeTrue(mHasManagedUserFeature); final int origMode = getUserTypePackageWhitelistMode(); setUserTypePackageWhitelistMode(USER_TYPE_PACKAGE_WHITELIST_MODE_ENFORCE @@ -665,7 +665,7 @@ public class UserLifecycleTests { } /** Tests starting (unlocking) a newly-created profile NOT using the user-type-pkg-whitelist. */ @Test(timeout = TIMEOUT_MAX_TEST_TIME_MS) - public void managedProfileUnlock_notUsingWhitelist() { + public void managedProfileUnlock_notUsingWhitelist() throws RemoteException { assumeTrue(mHasManagedUserFeature); final int origMode = getUserTypePackageWhitelistMode(); setUserTypePackageWhitelistMode(USER_TYPE_PACKAGE_WHITELIST_MODE_DISABLE); @@ -908,10 +908,8 @@ public class UserLifecycleTests { result != null && result.contains("Failed")); } - private void removeUser(int userId) { - if (mBroadcastWaiter.hasActionBeenReceivedForUser(Intent.ACTION_USER_STARTED, userId)) { - mBroadcastWaiter.waitActionForUserIfNotReceivedYet(Intent.ACTION_MEDIA_MOUNTED, userId); - } + private void removeUser(int userId) throws RemoteException { + stopUserAfterWaitingForBroadcastIdle(userId, true); try { mUm.removeUser(userId); final long startTime = System.currentTimeMillis(); -- cgit v1.2.3-59-g8ed1b