blob: 7916bd37060ec56bcbdecf02de3986742eface5c [file] [log] [blame]
Tony Mak0146d262017-06-01 11:02:59 +01001/*
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 */
16package com.android.server.pm;
17
18import static org.junit.Assert.assertNotNull;
19import static org.junit.Assert.assertTrue;
20
21import android.app.ActivityManager;
22import android.app.IStopUserCallback;
23import android.content.Context;
24import android.content.pm.UserInfo;
25import android.os.RemoteException;
26import android.os.UserManager;
Tony Mak0146d262017-06-01 11:02:59 +010027import android.util.Log;
28
Brett Chabota26eda92018-07-23 13:08:30 -070029import androidx.test.InstrumentationRegistry;
30import androidx.test.filters.LargeTest;
31import androidx.test.runner.AndroidJUnit4;
32
Tony Mak0146d262017-06-01 11:02:59 +010033import org.junit.Before;
34import org.junit.Test;
35import org.junit.runner.RunWith;
36
37import java.io.IOException;
38import java.util.concurrent.CountDownLatch;
39import 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
47public 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",
Bookatz029832a2019-10-04 16:50:22 -070074 UserManager.USER_TYPE_PROFILE_MANAGED, 0, mActivityManager.getCurrentUser());
Tony Mak0146d262017-06-01 11:02:59 +010075 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