Debian Codegraph @

libartservice/service/javatests/com/android/server/art/BackgroundDexoptJobTest.java

raw
1 /*
2 * Copyright (C) 2022 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
17 package com.android.server.art;
18
19 import static com.android.server.art.model.Config.Callback;
20
21 import static com.google.common.truth.Truth.assertThat;
22
23 import static org.mockito.Mockito.any;
24 import static org.mockito.Mockito.anyBoolean;
25 import static org.mockito.Mockito.anyInt;
26 import static org.mockito.Mockito.eq;
27 import static org.mockito.Mockito.lenient;
28 import static org.mockito.Mockito.never;
29 import static org.mockito.Mockito.same;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import android.app.job.JobInfo;
35 import android.app.job.JobParameters;
36 import android.app.job.JobScheduler;
37 import android.os.CancellationSignal;
38 import android.os.SystemProperties;
39
40 import androidx.test.filters.SmallTest;
41 import androidx.test.runner.AndroidJUnit4;
42
43 import com.android.server.art.BackgroundDexoptJob.CompletedResult;
44 import com.android.server.art.BackgroundDexoptJob.FatalErrorResult;
45 import com.android.server.art.BackgroundDexoptJob.Result;
46 import com.android.server.art.model.ArtFlags;
47 import com.android.server.art.model.Config;
48 import com.android.server.art.model.DexoptResult;
49 import com.android.server.art.testing.StaticMockitoRule;
50 import com.android.server.pm.PackageManagerLocal;
51
52 import org.junit.Before;
53 import org.junit.Rule;
54 import org.junit.Test;
55 import org.junit.runner.RunWith;
56 import org.mockito.ArgumentCaptor;
57 import org.mockito.Mock;
58
59 import java.util.concurrent.Future;
60 import java.util.concurrent.Semaphore;
61 import java.util.concurrent.TimeUnit;
62
63 @SmallTest
64 @RunWith(AndroidJUnit4.class)
65 public class BackgroundDexoptJobTest {
66 private static final long TIMEOUT_SEC = 10;
67
68 @Rule
69 public StaticMockitoRule mockitoRule =
70 new StaticMockitoRule(SystemProperties.class, BackgroundDexoptJobService.class);
71
72 @Mock private BackgroundDexoptJob.Injector mInjector;
73 @Mock private ArtManagerLocal mArtManagerLocal;
74 @Mock private PackageManagerLocal mPackageManagerLocal;
75 @Mock private PackageManagerLocal.FilteredSnapshot mSnapshot;
76 @Mock private JobScheduler mJobScheduler;
77 @Mock private DexoptResult mDexoptResult;
78 @Mock private BackgroundDexoptJobService mJobService;
79 @Mock private JobParameters mJobParameters;
80 private Config mConfig;
81 private BackgroundDexoptJob mBackgroundDexoptJob;
82 private Semaphore mJobFinishedCalled = new Semaphore(0);
83
84 @Before
85 public void setUp() throws Exception {
86 lenient()
87 .when(SystemProperties.getBoolean(eq("pm.dexopt.disable_bg_dexopt"), anyBoolean()))
88 .thenReturn(false);
89
90 lenient().when(mPackageManagerLocal.withFilteredSnapshot()).thenReturn(mSnapshot);
91
92 mConfig = new Config();
93
94 lenient().when(mInjector.getArtManagerLocal()).thenReturn(mArtManagerLocal);
95 lenient().when(mInjector.getPackageManagerLocal()).thenReturn(mPackageManagerLocal);
96 lenient().when(mInjector.getConfig()).thenReturn(mConfig);
97 lenient().when(mInjector.getJobScheduler()).thenReturn(mJobScheduler);
98
99 mBackgroundDexoptJob = new BackgroundDexoptJob(mInjector);
100 lenient().when(BackgroundDexoptJobService.getJob()).thenReturn(mBackgroundDexoptJob);
101
102 lenient()
103 .doAnswer(invocation -> {
104 mJobFinishedCalled.release();
105 return null;
106 })
107 .when(mJobService)
108 .jobFinished(any(), anyBoolean());
109
110 lenient()
111 .when(mJobParameters.getStopReason())
112 .thenReturn(JobParameters.STOP_REASON_UNDEFINED);
113 }
114
115 @Test
116 public void testStart() {
117 when(mArtManagerLocal.dexoptPackages(
118 same(mSnapshot), eq(ReasonMapping.REASON_BG_DEXOPT), any(), any(), any()))
119 .thenReturn(mDexoptResult);
120
121 Result result = Utils.getFuture(mBackgroundDexoptJob.start());
122 assertThat(result).isInstanceOf(CompletedResult.class);
123 assertThat(((CompletedResult) result).dexoptResult()).isSameInstanceAs(mDexoptResult);
124
125 verify(mArtManagerLocal).cleanup(same(mSnapshot));
126 }
127
128 @Test
129 public void testStartAlreadyRunning() {
130 Semaphore dexoptDone = new Semaphore(0);
131 when(mArtManagerLocal.dexoptPackages(any(), any(), any(), any(), any()))
132 .thenAnswer(invocation -> {
133 assertThat(dexoptDone.tryAcquire(TIMEOUT_SEC, TimeUnit.SECONDS)).isTrue();
134 return mDexoptResult;
135 });
136
137 Future<Result> future1 = mBackgroundDexoptJob.start();
138 Future<Result> future2 = mBackgroundDexoptJob.start();
139 assertThat(future1).isSameInstanceAs(future2);
140
141 dexoptDone.release();
142 Utils.getFuture(future1);
143
144 verify(mArtManagerLocal, times(1)).dexoptPackages(any(), any(), any(), any(), any());
145 }
146
147 @Test
148 public void testStartAnother() {
149 when(mArtManagerLocal.dexoptPackages(any(), any(), any(), any(), any()))
150 .thenReturn(mDexoptResult);
151
152 Future<Result> future1 = mBackgroundDexoptJob.start();
153 Utils.getFuture(future1);
154 Future<Result> future2 = mBackgroundDexoptJob.start();
155 Utils.getFuture(future2);
156 assertThat(future1).isNotSameInstanceAs(future2);
157 }
158
159 @Test
160 public void testStartFatalError() {
161 when(mArtManagerLocal.dexoptPackages(any(), any(), any(), any(), any()))
162 .thenThrow(IllegalStateException.class);
163
164 Result result = Utils.getFuture(mBackgroundDexoptJob.start());
165 assertThat(result).isInstanceOf(FatalErrorResult.class);
166 }
167
168 @Test
169 public void testStartIgnoreDisabled() {
170 lenient()
171 .when(SystemProperties.getBoolean(eq("pm.dexopt.disable_bg_dexopt"), anyBoolean()))
172 .thenReturn(true);
173
174 when(mArtManagerLocal.dexoptPackages(any(), any(), any(), any(), any()))
175 .thenReturn(mDexoptResult);
176
177 // The `start` method should ignore the system property. The system property is for
178 // `schedule`.
179 Utils.getFuture(mBackgroundDexoptJob.start());
180 }
181
182 @Test
183 public void testCancel() {
184 Semaphore dexoptCancelled = new Semaphore(0);
185 when(mArtManagerLocal.dexoptPackages(any(), any(), any(), any(), any()))
186 .thenAnswer(invocation -> {
187 assertThat(dexoptCancelled.tryAcquire(TIMEOUT_SEC, TimeUnit.SECONDS)).isTrue();
188 var cancellationSignal = invocation.<CancellationSignal>getArgument(2);
189 assertThat(cancellationSignal.isCanceled()).isTrue();
190 return mDexoptResult;
191 });
192
193 Future<Result> future = mBackgroundDexoptJob.start();
194 mBackgroundDexoptJob.cancel();
195 dexoptCancelled.release();
196 Utils.getFuture(future);
197 }
198
199 @Test
200 public void testSchedule() {
201 var captor = ArgumentCaptor.forClass(JobInfo.class);
202 when(mJobScheduler.schedule(captor.capture())).thenReturn(JobScheduler.RESULT_SUCCESS);
203
204 assertThat(mBackgroundDexoptJob.schedule()).isEqualTo(ArtFlags.SCHEDULE_SUCCESS);
205
206 JobInfo jobInfo = captor.getValue();
207 assertThat(jobInfo.getIntervalMillis()).isEqualTo(BackgroundDexoptJob.JOB_INTERVAL_MS);
208 assertThat(jobInfo.isRequireDeviceIdle()).isTrue();
209 assertThat(jobInfo.isRequireCharging()).isTrue();
210 assertThat(jobInfo.isRequireBatteryNotLow()).isTrue();
211 assertThat(jobInfo.isRequireStorageNotLow()).isFalse();
212 }
213
214 @Test
215 public void testScheduleDisabled() {
216 when(SystemProperties.getBoolean(eq("pm.dexopt.disable_bg_dexopt"), anyBoolean()))
217 .thenReturn(true);
218
219 assertThat(mBackgroundDexoptJob.schedule())
220 .isEqualTo(ArtFlags.SCHEDULE_DISABLED_BY_SYSPROP);
221
222 verify(mJobScheduler, never()).schedule(any());
223 }
224
225 @Test
226 public void testScheduleOverride() {
227 mConfig.setScheduleBackgroundDexoptJobCallback(Runnable::run, builder -> {
228 builder.setRequiresBatteryNotLow(false);
229 builder.setPriority(JobInfo.PRIORITY_LOW);
230 });
231
232 var captor = ArgumentCaptor.forClass(JobInfo.class);
233 when(mJobScheduler.schedule(captor.capture())).thenReturn(JobScheduler.RESULT_SUCCESS);
234
235 assertThat(mBackgroundDexoptJob.schedule()).isEqualTo(ArtFlags.SCHEDULE_SUCCESS);
236
237 JobInfo jobInfo = captor.getValue();
238 assertThat(jobInfo.getIntervalMillis()).isEqualTo(BackgroundDexoptJob.JOB_INTERVAL_MS);
239 assertThat(jobInfo.isRequireDeviceIdle()).isTrue();
240 assertThat(jobInfo.isRequireCharging()).isTrue();
241 assertThat(jobInfo.isRequireBatteryNotLow()).isFalse();
242 assertThat(jobInfo.getPriority()).isEqualTo(JobInfo.PRIORITY_LOW);
243 }
244
245 @Test
246 public void testScheduleOverrideCleared() {
247 mConfig.setScheduleBackgroundDexoptJobCallback(
248 Runnable::run, builder -> { builder.setRequiresBatteryNotLow(false); });
249 mConfig.clearScheduleBackgroundDexoptJobCallback();
250
251 var captor = ArgumentCaptor.forClass(JobInfo.class);
252 when(mJobScheduler.schedule(captor.capture())).thenReturn(JobScheduler.RESULT_SUCCESS);
253
254 assertThat(mBackgroundDexoptJob.schedule()).isEqualTo(ArtFlags.SCHEDULE_SUCCESS);
255
256 JobInfo jobInfo = captor.getValue();
257 assertThat(jobInfo.isRequireBatteryNotLow()).isTrue();
258 }
259
260 @Test(expected = IllegalStateException.class)
261 public void testScheduleOverrideStorageNotLow() {
262 mConfig.setScheduleBackgroundDexoptJobCallback(
263 Runnable::run, builder -> { builder.setRequiresStorageNotLow(true); });
264
265 mBackgroundDexoptJob.schedule();
266 }
267
268 @Test
269 public void testUnschedule() {
270 mBackgroundDexoptJob.unschedule();
271 verify(mJobScheduler).cancel(anyInt());
272 }
273
274 @Test
275 public void testWantsRescheduleFalsePerformed() throws Exception {
276 when(mDexoptResult.getFinalStatus()).thenReturn(DexoptResult.DEXOPT_PERFORMED);
277 when(mArtManagerLocal.dexoptPackages(any(), any(), any(), any(), any()))
278 .thenReturn(mDexoptResult);
279
280 mBackgroundDexoptJob.onStartJob(mJobService, mJobParameters);
281 assertThat(mJobFinishedCalled.tryAcquire(TIMEOUT_SEC, TimeUnit.SECONDS)).isTrue();
282
283 verify(mJobService).jobFinished(any(), eq(false) /* wantsReschedule */);
284 }
285
286 @Test
287 public void testWantsRescheduleFalseFatalError() throws Exception {
288 when(mArtManagerLocal.dexoptPackages(any(), any(), any(), any(), any()))
289 .thenThrow(RuntimeException.class);
290
291 mBackgroundDexoptJob.onStartJob(mJobService, mJobParameters);
292 assertThat(mJobFinishedCalled.tryAcquire(TIMEOUT_SEC, TimeUnit.SECONDS)).isTrue();
293
294 verify(mJobService).jobFinished(any(), eq(false) /* wantsReschedule */);
295 }
296
297 @Test
298 public void testWantsRescheduleTrue() throws Exception {
299 when(mDexoptResult.getFinalStatus()).thenReturn(DexoptResult.DEXOPT_CANCELLED);
300 when(mArtManagerLocal.dexoptPackages(any(), any(), any(), any(), any()))
301 .thenReturn(mDexoptResult);
302
303 mBackgroundDexoptJob.onStartJob(mJobService, mJobParameters);
304 assertThat(mJobFinishedCalled.tryAcquire(TIMEOUT_SEC, TimeUnit.SECONDS)).isTrue();
305
306 verify(mJobService).jobFinished(any(), eq(true) /* wantsReschedule */);
307 }
308 }