Loading services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java +3 −3 Original line number Diff line number Diff line Loading @@ -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) { Loading Loading @@ -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 Loading @@ -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); } Loading services/tests/servicestests/src/com/android/server/pm/UserRemovalWaiter.java +8 −22 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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) { Loading @@ -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)); } Loading @@ -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); } } } Loading
services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java +3 −3 Original line number Diff line number Diff line Loading @@ -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) { Loading Loading @@ -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 Loading @@ -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); } Loading
services/tests/servicestests/src/com/android/server/pm/UserRemovalWaiter.java +8 −22 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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) { Loading @@ -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)); } Loading @@ -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); } } }