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

Commit ca8cc723 authored by Yasin Kilicdere's avatar Yasin Kilicdere
Browse files

UserRemovalWaiter - Stop querying alive users in addition to broadcast

In UserRemovalWaiter, in addition to waiting for ACTION_USER_REMOVED
broadcast, alive users were queried every one second, in case the
broadcast was delayed. But it turns out querying users is adding
unnecessary complexity and making the test less stable.
This CL reverts that experimental change.

Bug: 264534887
Test: atest com.android.server.pm.UserManagerTest
Change-Id: I20b9b1d3e808706336ba260192c8ea02eba49658
parent 2d57bcbf
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -1122,7 +1122,7 @@ public final class UserManagerTest {

    @Nullable
    private UserInfo getUser(int id) {
        List<UserInfo> list = mUserManager.getAliveUsers();
        List<UserInfo> list = mUserManager.getUsers();

        for (UserInfo user : list) {
            if (user.id == id) {
@@ -1277,7 +1277,7 @@ public final class UserManagerTest {
    @MediumTest
    @Test
    public void testConcurrentUserCreate() throws Exception {
        int userCount = mUserManager.getAliveUsers().size();
        int userCount = mUserManager.getUsers().size();
        int maxSupportedUsers = UserManager.getMaxSupportedUsers();
        int canBeCreatedCount = maxSupportedUsers - userCount;
        // Test exceeding the limit while running in parallel
@@ -1300,7 +1300,7 @@ public final class UserManagerTest {
                "Could not create " + createUsersCount + " users in " + timeout + " seconds")
                .that(es.awaitTermination(timeout, TimeUnit.SECONDS))
                .isTrue();
        assertThat(mUserManager.getAliveUsers().size()).isEqualTo(maxSupportedUsers);
        assertThat(mUserManager.getUsers().size()).isEqualTo(maxSupportedUsers);
        assertThat(created.get()).isEqualTo(canBeCreatedCount);
    }

+8 −22
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;

import java.io.Closeable;
@@ -36,9 +35,8 @@ import java.util.concurrent.TimeUnit;
public class UserRemovalWaiter extends BroadcastReceiver implements Closeable {

    private final Context mContext;
    private final UserManager mUserManager;
    private final String mTag;
    private final long mTimeoutMillis;
    private final long mTimeoutInSeconds;
    private final Map<Integer, CountDownLatch> mMap = new ConcurrentHashMap<>();

    private CountDownLatch getLatch(final int userId) {
@@ -47,9 +45,8 @@ public class UserRemovalWaiter extends BroadcastReceiver implements Closeable {

    public UserRemovalWaiter(Context context, String tag, int timeoutInSeconds) {
        mContext = context;
        mUserManager = UserManager.get(mContext);
        mTag = tag;
        mTimeoutMillis = timeoutInSeconds * 1000L;
        mTimeoutInSeconds = timeoutInSeconds;

        mContext.registerReceiver(this, new IntentFilter(Intent.ACTION_USER_REMOVED));
    }
@@ -73,28 +70,17 @@ public class UserRemovalWaiter extends BroadcastReceiver implements Closeable {
     */
    public void waitFor(int userId) {
        Log.i(mTag, "Waiting for user " + userId + " to be removed");
        CountDownLatch latch = getLatch(userId);

        long startTime = System.currentTimeMillis();
        while (System.currentTimeMillis() - startTime < mTimeoutMillis) {
            if (hasUserGone(userId) || waitLatchForOneSecond(latch)) {
        try {
            if (getLatch(userId).await(mTimeoutInSeconds, TimeUnit.SECONDS)) {
                Log.i(mTag, "User " + userId + " is removed in "
                        + (System.currentTimeMillis() - startTime) + " ms");
                return;
            }
        }
            } else {
                fail("Timeout waiting for user removal. userId = " + userId);
            }

    private boolean hasUserGone(int userId) {
        return mUserManager.getAliveUsers().stream().noneMatch(x -> x.id == userId);
    }

    private boolean waitLatchForOneSecond(CountDownLatch latch) {
        try {
            return latch.await(1, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            Log.e(mTag, "Thread interrupted unexpectedly.", e);
            return false;
            throw new AssertionError("Thread interrupted unexpectedly.", e);
        }
    }
}