Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 75266fb9 authored by Yasin Kilicdere's avatar Yasin Kilicdere
Browse files

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
parent 03882bc8
Loading
Loading
Loading
Loading
+0 −12
Original line number Diff line number Diff line
@@ -45,7 +45,6 @@ public class BroadcastWaiter implements Closeable {
    private final int mTimeoutInSecond;
    private final Set<String> mActions;

    private final Set<String> mActionReceivedForUser = new HashSet<>();
    private final List<BroadcastReceiver> mBroadcastReceivers = new ArrayList<>();

    private final Map<String, Semaphore> 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);
    }
}
+8 −10
Original line number Diff line number Diff line
@@ -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();