• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.appsearch;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import android.app.appsearch.testutil.AppSearchTestUtils;
21 import android.app.appsearch.testutil.FakeAppSearchConfig;
22 import android.platform.test.annotations.RequiresFlagsEnabled;
23 
24 import com.android.appsearch.flags.Flags;
25 import com.android.server.appsearch.icing.proto.GetOptimizeInfoResultProto;
26 import com.android.server.appsearch.icing.proto.StatusProto;
27 
28 import org.junit.Ignore;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.RuleChain;
32 
33 import java.util.concurrent.TimeUnit;
34 
35 // NOTE: The tests in this class are based on the underlying assumption that
36 // time_optimize_threshold > min_time_optimize_threshold. This ensures that setting
37 // timeSinceLastOptimize to time_optimize_threshold - 1 does not make it lesser than
38 // min_time_optimize_threshold (otherwise shouldOptimize() would return false for test cases that
39 // check byteThreshold and docCountThreshold).
40 public class ServiceOptimizeStrategyTest {
41     ServiceAppSearchConfig mAppSearchConfig = new FakeAppSearchConfig();
42     ServiceOptimizeStrategy mServiceOptimizeStrategy =
43             new ServiceOptimizeStrategy(mAppSearchConfig);
44     @Rule public final RuleChain mRuleChain = AppSearchTestUtils.createCommonTestRules();
45 
46     @Test
testTimeOptimizeThreshold_isGreaterThan_minTimeOptimizeThreshold()47     public void testTimeOptimizeThreshold_isGreaterThan_minTimeOptimizeThreshold() {
48         assertThat(mAppSearchConfig.getCachedTimeOptimizeThresholdMs())
49                 .isGreaterThan(mAppSearchConfig.getCachedMinTimeOptimizeThresholdMs());
50     }
51 
52     @Test
testShouldNotOptimize_underAllThresholds()53     public void testShouldNotOptimize_underAllThresholds() {
54         GetOptimizeInfoResultProto optimizeInfo =
55                 GetOptimizeInfoResultProto.newBuilder()
56                         .setTimeSinceLastOptimizeMs(
57                                 mAppSearchConfig.getCachedTimeOptimizeThresholdMs() - 1)
58                         .setEstimatedOptimizableBytes(
59                                 mAppSearchConfig.getCachedBytesOptimizeThreshold() - 1)
60                         .setOptimizableDocs(
61                                 mAppSearchConfig.getCachedDocCountOptimizeThreshold() - 1)
62                         .setStatus(StatusProto.newBuilder().setCode(StatusProto.Code.OK).build())
63                         .build();
64         assertThat(mServiceOptimizeStrategy.shouldOptimize(optimizeInfo)).isFalse();
65     }
66 
67     @Test
testShouldOptimize_byteThreshold()68     public void testShouldOptimize_byteThreshold() {
69         GetOptimizeInfoResultProto optimizeInfo =
70                 GetOptimizeInfoResultProto.newBuilder()
71                         .setTimeSinceLastOptimizeMs(
72                                 mAppSearchConfig.getCachedTimeOptimizeThresholdMs() - 1)
73                         .setEstimatedOptimizableBytes(
74                                 mAppSearchConfig.getCachedBytesOptimizeThreshold())
75                         .setOptimizableDocs(
76                                 mAppSearchConfig.getCachedDocCountOptimizeThreshold() - 1)
77                         .setStatus(StatusProto.newBuilder().setCode(StatusProto.Code.OK).build())
78                         .build();
79         assertThat(mServiceOptimizeStrategy.shouldOptimize(optimizeInfo)).isTrue();
80     }
81 
82     @Test
testShouldOptimize_timeThreshold()83     public void testShouldOptimize_timeThreshold() {
84         GetOptimizeInfoResultProto optimizeInfo =
85                 GetOptimizeInfoResultProto.newBuilder()
86                         .setTimeSinceLastOptimizeMs(
87                                 mAppSearchConfig.getCachedTimeOptimizeThresholdMs())
88                         .setEstimatedOptimizableBytes(
89                                 mAppSearchConfig.getCachedBytesOptimizeThreshold() - 1)
90                         .setOptimizableDocs(
91                                 mAppSearchConfig.getCachedDocCountOptimizeThreshold() - 1)
92                         .setStatus(StatusProto.newBuilder().setCode(StatusProto.Code.OK).build())
93                         .build();
94         assertThat(mServiceOptimizeStrategy.shouldOptimize(optimizeInfo)).isTrue();
95     }
96 
97     @Test
testShouldOptimize_docCountThreshold()98     public void testShouldOptimize_docCountThreshold() {
99         GetOptimizeInfoResultProto optimizeInfo =
100                 GetOptimizeInfoResultProto.newBuilder()
101                         .setTimeSinceLastOptimizeMs(
102                                 mAppSearchConfig.getCachedTimeOptimizeThresholdMs() - 1)
103                         .setEstimatedOptimizableBytes(
104                                 mAppSearchConfig.getCachedBytesOptimizeThreshold() - 1)
105                         .setOptimizableDocs(mAppSearchConfig.getCachedDocCountOptimizeThreshold())
106                         .setStatus(StatusProto.newBuilder().setCode(StatusProto.Code.OK).build())
107                         .build();
108         assertThat(mServiceOptimizeStrategy.shouldOptimize(optimizeInfo)).isTrue();
109     }
110 
111     @Test
testShouldNotOptimize_underMinTimeThreshold()112     public void testShouldNotOptimize_underMinTimeThreshold() {
113         GetOptimizeInfoResultProto optimizeInfo =
114                 GetOptimizeInfoResultProto.newBuilder()
115                         .setTimeSinceLastOptimizeMs(
116                                 mAppSearchConfig.getCachedMinTimeOptimizeThresholdMs() - 1)
117                         .setEstimatedOptimizableBytes(
118                                 mAppSearchConfig.getCachedBytesOptimizeThreshold())
119                         .setOptimizableDocs(mAppSearchConfig.getCachedDocCountOptimizeThreshold())
120                         .setStatus(StatusProto.newBuilder().setCode(StatusProto.Code.OK).build())
121                         .build();
122         assertThat(mServiceOptimizeStrategy.shouldOptimize(optimizeInfo)).isFalse();
123     }
124 
125     // TODO (b/385020106): figure out how to make the default 0 timeSinceLastOptimize work
126     //  with a higher threshold and return 4 hours when
127     //  Flags.enable_four_hour_min_optimize_threshold is true and reenable
128     @Ignore
129     @Test
130     @RequiresFlagsEnabled(Flags.FLAG_ENABLE_FOUR_HOUR_MIN_TIME_OPTIMIZE_THRESHOLD)
testShouldNotOptimize_underFourHourMinTimeThreshold()131     public void testShouldNotOptimize_underFourHourMinTimeThreshold() {
132         GetOptimizeInfoResultProto optimizeInfo =
133                 GetOptimizeInfoResultProto.newBuilder()
134                         .setTimeSinceLastOptimizeMs(TimeUnit.HOURS.toMillis(4) - 1)
135                         .setEstimatedOptimizableBytes(
136                                 mAppSearchConfig.getCachedBytesOptimizeThreshold())
137                         .setOptimizableDocs(mAppSearchConfig.getCachedDocCountOptimizeThreshold())
138                         .setStatus(StatusProto.newBuilder().setCode(StatusProto.Code.OK).build())
139                         .build();
140         assertThat(mServiceOptimizeStrategy.shouldOptimize(optimizeInfo)).isFalse();
141     }
142 }
143