Tony Mak | 0146d26 | 2017-06-01 11:02:59 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | package com.android.server.pm; |
| 17 | |
| 18 | import static org.junit.Assert.assertNotNull; |
| 19 | import static org.junit.Assert.assertTrue; |
| 20 | |
| 21 | import android.app.ActivityManager; |
| 22 | import android.app.IStopUserCallback; |
| 23 | import android.content.Context; |
| 24 | import android.content.pm.UserInfo; |
| 25 | import android.os.RemoteException; |
| 26 | import android.os.UserManager; |
Tony Mak | 0146d26 | 2017-06-01 11:02:59 +0100 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 29 | import androidx.test.InstrumentationRegistry; |
| 30 | import androidx.test.filters.LargeTest; |
| 31 | import androidx.test.runner.AndroidJUnit4; |
| 32 | |
Tony Mak | 0146d26 | 2017-06-01 11:02:59 +0100 | [diff] [blame] | 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
| 36 | |
| 37 | import java.io.IOException; |
| 38 | import java.util.concurrent.CountDownLatch; |
| 39 | import java.util.concurrent.TimeUnit; |
| 40 | |
| 41 | /** |
| 42 | * To run the test: |
| 43 | * bit FrameworksServicesTests:com.android.server.pm.UserLifecycleStressTest |
| 44 | */ |
| 45 | @RunWith(AndroidJUnit4.class) |
| 46 | @LargeTest |
| 47 | public class UserLifecycleStressTest { |
| 48 | private static final String TAG = "UserLifecycleStressTest"; |
| 49 | // TODO: Make this smaller once we have improved it. |
| 50 | private static final int MAX_TIME_STOP_USER_IN_SECOND = 30; |
| 51 | private static final int NUM_ITERATIONS_STOP_USER = 10; |
| 52 | private static final int WAIT_BEFORE_STOP_USER_IN_SECOND = 3; |
| 53 | |
| 54 | private Context mContext; |
| 55 | private UserManager mUserManager; |
| 56 | private ActivityManager mActivityManager; |
| 57 | |
| 58 | @Before |
| 59 | public void setup() { |
| 60 | mContext = InstrumentationRegistry.getInstrumentation().getContext(); |
| 61 | mUserManager = mContext.getSystemService(UserManager.class); |
| 62 | mActivityManager = mContext.getSystemService(ActivityManager.class); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Create and stop user 10 times in a row. Check stop user can be finished in a reasonable |
| 67 | * amount of time. |
| 68 | */ |
| 69 | @Test |
| 70 | public void stopManagedProfileStressTest() |
| 71 | throws IOException, RemoteException, InterruptedException { |
| 72 | for (int i = 0; i < NUM_ITERATIONS_STOP_USER; i++) { |
| 73 | final UserInfo userInfo = mUserManager.createProfileForUser("TestUser", |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame^] | 74 | UserManager.USER_TYPE_PROFILE_MANAGED, 0, mActivityManager.getCurrentUser()); |
Tony Mak | 0146d26 | 2017-06-01 11:02:59 +0100 | [diff] [blame] | 75 | assertNotNull(userInfo); |
| 76 | try { |
| 77 | assertTrue( |
| 78 | "Failed to start the profile", |
| 79 | ActivityManager.getService().startUserInBackground(userInfo.id)); |
| 80 | // Seems the broadcast queue is getting more busy if we wait a few seconds before |
| 81 | // stopping the user. |
| 82 | TimeUnit.SECONDS.sleep(WAIT_BEFORE_STOP_USER_IN_SECOND); |
| 83 | stopUser(userInfo.id); |
| 84 | } finally { |
| 85 | mUserManager.removeUser(userInfo.id); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private void stopUser(int userId) throws RemoteException, InterruptedException { |
| 91 | final long startTime = System.currentTimeMillis(); |
| 92 | CountDownLatch countDownLatch = new CountDownLatch(1); |
| 93 | ActivityManager.getService(). |
| 94 | stopUser(userId, true, |
| 95 | new IStopUserCallback.Stub() { |
| 96 | @Override |
| 97 | public void userStopped(int userId) throws RemoteException { |
| 98 | countDownLatch.countDown(); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void userStopAborted(int userId) throws RemoteException { |
| 103 | |
| 104 | } |
| 105 | }); |
| 106 | boolean stoppedBeforeTimeout = |
| 107 | countDownLatch.await(MAX_TIME_STOP_USER_IN_SECOND, TimeUnit.SECONDS); |
| 108 | assertTrue( |
| 109 | "Take more than " + MAX_TIME_STOP_USER_IN_SECOND + "s to stop user", |
| 110 | stoppedBeforeTimeout); |
| 111 | Log.d(TAG, "stopUser takes " + (System.currentTimeMillis() - startTime) + " ms"); |
| 112 | } |
| 113 | } |
| 114 | |