Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.car.telemetry.databroker; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 20 | import static com.google.common.truth.Truth.assertWithMessage; |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 21 | |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 22 | import static org.mockito.ArgumentMatchers.any; |
| 23 | import static org.mockito.ArgumentMatchers.anyInt; |
| 24 | import static org.mockito.ArgumentMatchers.anyString; |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 25 | import static org.mockito.ArgumentMatchers.eq; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 26 | import static org.mockito.Mockito.verify; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 27 | import static org.mockito.Mockito.when; |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 28 | |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 29 | import android.annotation.Nullable; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 30 | import android.car.hardware.CarPropertyConfig; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 31 | import android.content.Context; |
| 32 | import android.content.ServiceConnection; |
Zhomart Mukhamejanov | cdcb4e1 | 2021-08-23 18:24:40 -0700 | [diff] [blame] | 33 | import android.content.SharedPreferences; |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 34 | import android.os.Handler; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 35 | import android.os.IBinder; |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 36 | import android.os.PersistableBundle; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 37 | import android.os.RemoteException; |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 38 | import android.os.SystemClock; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 39 | |
| 40 | import com.android.car.CarPropertyService; |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 41 | import com.android.car.telemetry.ResultStore; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 42 | import com.android.car.telemetry.TelemetryProto; |
| 43 | import com.android.car.telemetry.publisher.PublisherFactory; |
Zhomart Mukhamejanov | cdcb4e1 | 2021-08-23 18:24:40 -0700 | [diff] [blame] | 44 | import com.android.car.telemetry.publisher.StatsManagerProxy; |
Max Dashouk | 9912ac3 | 2021-08-13 17:54:51 +0000 | [diff] [blame] | 45 | import com.android.car.telemetry.scriptexecutorinterface.IScriptExecutor; |
| 46 | import com.android.car.telemetry.scriptexecutorinterface.IScriptExecutorListener; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 47 | |
| 48 | import org.junit.Before; |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 49 | import org.junit.Test; |
| 50 | import org.junit.runner.RunWith; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 51 | import org.mockito.ArgumentCaptor; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 52 | import org.mockito.Mock; |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 53 | import org.mockito.Mockito; |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 54 | import org.mockito.junit.MockitoJUnitRunner; |
| 55 | |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 56 | import java.util.Collections; |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 57 | import java.util.concurrent.CountDownLatch; |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 58 | import java.util.concurrent.PriorityBlockingQueue; |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 59 | import java.util.concurrent.TimeUnit; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 60 | |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 61 | @RunWith(MockitoJUnitRunner.class) |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 62 | public class DataBrokerTest { |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 63 | private static final int PROP_ID = 100; |
| 64 | private static final int PROP_AREA = 200; |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 65 | private static final int PRIORITY_HIGH = 1; |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 66 | private static final int PRIORITY_LOW = 100; |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 67 | private static final long TIMEOUT_MS = 5_000L; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 68 | private static final CarPropertyConfig<Integer> PROP_CONFIG = |
| 69 | CarPropertyConfig.newBuilder(Integer.class, PROP_ID, PROP_AREA).setAccess( |
| 70 | CarPropertyConfig.VEHICLE_PROPERTY_ACCESS_READ).build(); |
Max Dashouk | dafdd22 | 2021-08-20 10:42:25 -0700 | [diff] [blame] | 71 | private static final PersistableBundle DATA = new PersistableBundle(); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 72 | private static final TelemetryProto.VehiclePropertyPublisher |
| 73 | VEHICLE_PROPERTY_PUBLISHER_CONFIGURATION = |
| 74 | TelemetryProto.VehiclePropertyPublisher.newBuilder().setReadRate( |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 75 | 1).setVehiclePropertyId(PROP_ID).build(); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 76 | private static final TelemetryProto.Publisher PUBLISHER_CONFIGURATION = |
| 77 | TelemetryProto.Publisher.newBuilder().setVehicleProperty( |
| 78 | VEHICLE_PROPERTY_PUBLISHER_CONFIGURATION).build(); |
| 79 | private static final TelemetryProto.Subscriber SUBSCRIBER_FOO = |
| 80 | TelemetryProto.Subscriber.newBuilder().setHandler("function_name_foo").setPublisher( |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 81 | PUBLISHER_CONFIGURATION).setPriority(PRIORITY_HIGH).build(); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 82 | private static final TelemetryProto.MetricsConfig METRICS_CONFIG_FOO = |
| 83 | TelemetryProto.MetricsConfig.newBuilder().setName("Foo").setVersion( |
| 84 | 1).addSubscribers(SUBSCRIBER_FOO).build(); |
| 85 | private static final TelemetryProto.Subscriber SUBSCRIBER_BAR = |
| 86 | TelemetryProto.Subscriber.newBuilder().setHandler("function_name_bar").setPublisher( |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 87 | PUBLISHER_CONFIGURATION).setPriority(PRIORITY_LOW).build(); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 88 | private static final TelemetryProto.MetricsConfig METRICS_CONFIG_BAR = |
| 89 | TelemetryProto.MetricsConfig.newBuilder().setName("Bar").setVersion( |
| 90 | 1).addSubscribers(SUBSCRIBER_BAR).build(); |
| 91 | |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 92 | // when count reaches 0, all messages are handled, so assertions won't have race condition |
| 93 | private CountDownLatch mIdleHandlerLatch = new CountDownLatch(1); |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 94 | private DataBrokerImpl mDataBroker; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 95 | private FakeScriptExecutor mFakeScriptExecutor; |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 96 | private ScriptExecutionTask mHighPriorityTask; |
| 97 | private ScriptExecutionTask mLowPriorityTask; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 98 | |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 99 | @Mock |
| 100 | private Context mMockContext; |
| 101 | @Mock |
| 102 | private CarPropertyService mMockCarPropertyService; |
| 103 | @Mock |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 104 | private Handler mMockHandler; |
| 105 | @Mock |
Zhomart Mukhamejanov | cdcb4e1 | 2021-08-23 18:24:40 -0700 | [diff] [blame] | 106 | private StatsManagerProxy mMockStatsManager; |
| 107 | @Mock |
| 108 | private SharedPreferences mMockSharedPreferences; |
| 109 | @Mock |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 110 | private IBinder mMockScriptExecutorBinder; |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 111 | @Mock |
| 112 | private ResultStore mMockResultStore; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 113 | |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 114 | @Before |
| 115 | public void setUp() { |
| 116 | when(mMockCarPropertyService.getPropertyList()) |
| 117 | .thenReturn(Collections.singletonList(PROP_CONFIG)); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 118 | // bind service should return true, otherwise broker is disabled |
| 119 | when(mMockContext.bindServiceAsUser(any(), any(), anyInt(), any())).thenReturn(true); |
Zhomart Mukhamejanov | cdcb4e1 | 2021-08-23 18:24:40 -0700 | [diff] [blame] | 120 | PublisherFactory factory = new PublisherFactory( |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 121 | mMockCarPropertyService, mMockHandler, mMockStatsManager, mMockSharedPreferences); |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 122 | mDataBroker = new DataBrokerImpl(mMockContext, factory, mMockResultStore); |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 123 | // add IdleHandler to get notified when all messages and posts are handled |
| 124 | mDataBroker.getTelemetryHandler().getLooper().getQueue().addIdleHandler(() -> { |
| 125 | mIdleHandlerLatch.countDown(); |
| 126 | return true; |
| 127 | }); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 128 | |
| 129 | mFakeScriptExecutor = new FakeScriptExecutor(); |
| 130 | when(mMockScriptExecutorBinder.queryLocalInterface(anyString())) |
| 131 | .thenReturn(mFakeScriptExecutor); |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 132 | when(mMockContext.bindServiceAsUser(any(), any(), anyInt(), any())).thenAnswer(i -> { |
| 133 | ServiceConnection conn = i.getArgument(1); |
| 134 | conn.onServiceConnected(null, mMockScriptExecutorBinder); |
| 135 | return true; |
| 136 | }); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 137 | |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 138 | mHighPriorityTask = new ScriptExecutionTask( |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 139 | new DataSubscriber(mDataBroker, METRICS_CONFIG_FOO, SUBSCRIBER_FOO), |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 140 | DATA, |
| 141 | SystemClock.elapsedRealtime()); |
| 142 | mLowPriorityTask = new ScriptExecutionTask( |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 143 | new DataSubscriber(mDataBroker, METRICS_CONFIG_BAR, SUBSCRIBER_BAR), |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 144 | DATA, |
| 145 | SystemClock.elapsedRealtime()); |
| 146 | } |
| 147 | |
| 148 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 149 | public void testSetTaskExecutionPriority_whenNoTask_shouldNotInvokeScriptExecutor() |
| 150 | throws Exception { |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 151 | mDataBroker.setTaskExecutionPriority(PRIORITY_HIGH); |
| 152 | |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 153 | waitForHandlerThreadToFinish(); |
| 154 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(0); |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 158 | public void testSetTaskExecutionPriority_whenNextTaskPriorityLow_shouldNotRunTask() |
| 159 | throws Exception { |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 160 | mDataBroker.getTaskQueue().add(mLowPriorityTask); |
| 161 | |
| 162 | mDataBroker.setTaskExecutionPriority(PRIORITY_HIGH); |
| 163 | |
| 164 | waitForHandlerThreadToFinish(); |
| 165 | // task is not polled |
| 166 | assertThat(mDataBroker.getTaskQueue().peek()).isEqualTo(mLowPriorityTask); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 167 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(0); |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 171 | public void testSetTaskExecutionPriority_whenNextTaskPriorityHigh_shouldInvokeScriptExecutor() |
| 172 | throws Exception { |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 173 | mDataBroker.getTaskQueue().add(mHighPriorityTask); |
| 174 | |
| 175 | mDataBroker.setTaskExecutionPriority(PRIORITY_HIGH); |
| 176 | |
| 177 | waitForHandlerThreadToFinish(); |
| 178 | // task is polled and run |
| 179 | assertThat(mDataBroker.getTaskQueue().peek()).isNull(); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 180 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(1); |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 184 | public void testScheduleNextTask_whenNoTask_shouldNotInvokeScriptExecutor() throws Exception { |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 185 | mDataBroker.scheduleNextTask(); |
| 186 | |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 187 | waitForHandlerThreadToFinish(); |
| 188 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(0); |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 192 | public void testScheduleNextTask_whenTaskInProgress_shouldNotInvokeScriptExecutorAgain() |
| 193 | throws Exception { |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 194 | PriorityBlockingQueue<ScriptExecutionTask> taskQueue = mDataBroker.getTaskQueue(); |
| 195 | taskQueue.add(mHighPriorityTask); |
| 196 | mDataBroker.scheduleNextTask(); // start a task |
| 197 | waitForHandlerThreadToFinish(); |
| 198 | assertThat(taskQueue.peek()).isNull(); // assert that task is polled and running |
| 199 | taskQueue.add(mHighPriorityTask); // add another task into the queue |
| 200 | |
| 201 | mDataBroker.scheduleNextTask(); // schedule next task while the last task is in progress |
| 202 | |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 203 | // verify task is not polled |
| 204 | assertThat(taskQueue.peek()).isEqualTo(mHighPriorityTask); |
| 205 | // expect one invocation for the task that is running |
| 206 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(1); |
| 207 | } |
| 208 | |
| 209 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 210 | public void testScheduleNextTask_whenTaskCompletes_shouldAutomaticallyScheduleNextTask() |
| 211 | throws Exception { |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 212 | PriorityBlockingQueue<ScriptExecutionTask> taskQueue = mDataBroker.getTaskQueue(); |
| 213 | // add two tasks into the queue for execution |
| 214 | taskQueue.add(mHighPriorityTask); |
| 215 | taskQueue.add(mHighPriorityTask); |
| 216 | |
| 217 | mDataBroker.scheduleNextTask(); // start a task |
| 218 | waitForHandlerThreadToFinish(); |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 219 | mIdleHandlerLatch = new CountDownLatch(1); // reset handler idle condition |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 220 | // end a task, should automatically schedule the next task |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 221 | mFakeScriptExecutor.notifyScriptSuccess(DATA); // posts to handler |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 222 | |
| 223 | waitForHandlerThreadToFinish(); |
| 224 | // verify queue is empty, both tasks are polled and executed |
| 225 | assertThat(taskQueue.peek()).isNull(); |
| 226 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(2); |
| 227 | } |
| 228 | |
| 229 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 230 | public void testScheduleNextTask_onScriptSuccess_shouldStoreInterimResult() throws Exception { |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 231 | mDataBroker.getTaskQueue().add(mHighPriorityTask); |
| 232 | ArgumentCaptor<PersistableBundle> persistableBundleCaptor = |
| 233 | ArgumentCaptor.forClass(PersistableBundle.class); |
| 234 | |
| 235 | mDataBroker.scheduleNextTask(); |
| 236 | waitForHandlerThreadToFinish(); |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 237 | mFakeScriptExecutor.notifyScriptSuccess(DATA); |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 238 | |
| 239 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(1); |
| 240 | verify(mMockResultStore).putInterimResult(eq(METRICS_CONFIG_FOO.getName()), |
| 241 | persistableBundleCaptor.capture()); |
| 242 | assertThat(persistableBundleCaptor.getValue().toString()).isEqualTo( |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 243 | DATA.toString()); // expect same persistable bundle |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 247 | public void testScheduleNextTask_whenBindScriptExecutorFailed_shouldDisableBroker() |
| 248 | throws Exception { |
| 249 | // fail all future attempts to bind to it |
| 250 | Mockito.reset(mMockContext); |
| 251 | when(mMockContext.bindServiceAsUser(any(), any(), anyInt(), any())).thenReturn(false); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 252 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_FOO); |
| 253 | PriorityBlockingQueue<ScriptExecutionTask> taskQueue = mDataBroker.getTaskQueue(); |
| 254 | taskQueue.add(mHighPriorityTask); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 255 | |
| 256 | // will rebind to ScriptExecutor if it is null |
| 257 | mDataBroker.scheduleNextTask(); |
| 258 | |
| 259 | waitForHandlerThreadToFinish(); |
| 260 | // all subscribers should have been removed |
| 261 | assertThat(mDataBroker.getSubscriptionMap()).hasSize(0); |
| 262 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(0); |
| 263 | } |
| 264 | |
| 265 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 266 | public void testScheduleNextTask_whenScriptExecutorThrowsException_shouldRequeueTask() |
| 267 | throws Exception { |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 268 | PriorityBlockingQueue<ScriptExecutionTask> taskQueue = mDataBroker.getTaskQueue(); |
| 269 | taskQueue.add(mHighPriorityTask); |
| 270 | mFakeScriptExecutor.failNextApiCalls(1); // fail the next invokeScript() call |
| 271 | |
| 272 | mDataBroker.scheduleNextTask(); |
| 273 | |
| 274 | waitForHandlerThreadToFinish(); |
| 275 | // expect invokeScript() to be called and failed, causing the same task to be re-queued |
| 276 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(1); |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 277 | assertThat(taskQueue.peek()).isEqualTo(mHighPriorityTask); |
| 278 | } |
| 279 | |
| 280 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 281 | public void testAddTaskToQueue_shouldInvokeScriptExecutor() throws Exception { |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 282 | mDataBroker.addTaskToQueue(mHighPriorityTask); |
| 283 | |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 284 | waitForHandlerThreadToFinish(); |
| 285 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(1); |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 288 | @Test |
| 289 | public void testAddMetricsConfiguration_newMetricsConfig() { |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 290 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_BAR); |
| 291 | |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 292 | assertThat(mDataBroker.getSubscriptionMap()).hasSize(1); |
| 293 | assertThat(mDataBroker.getSubscriptionMap()).containsKey(METRICS_CONFIG_BAR.getName()); |
| 294 | // there should be one data subscriber in the subscription list of METRICS_CONFIG_BAR |
| 295 | assertThat(mDataBroker.getSubscriptionMap().get(METRICS_CONFIG_BAR.getName())).hasSize(1); |
| 296 | } |
| 297 | |
| 298 | |
| 299 | @Test |
| 300 | public void testAddMetricsConfiguration_duplicateMetricsConfig_shouldDoNothing() { |
| 301 | // this metrics config has already been added in setUp() |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 302 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_FOO); |
| 303 | |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 304 | assertThat(mDataBroker.getSubscriptionMap()).hasSize(1); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 305 | assertThat(mDataBroker.getSubscriptionMap()).containsKey(METRICS_CONFIG_FOO.getName()); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 306 | assertThat(mDataBroker.getSubscriptionMap().get(METRICS_CONFIG_FOO.getName())).hasSize(1); |
| 307 | } |
| 308 | |
| 309 | @Test |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 310 | public void testRemoveMetricsConfiguration_shouldRemoveAllAssociatedTasks() { |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 311 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_FOO); |
| 312 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_BAR); |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 313 | ScriptExecutionTask taskWithMetricsConfigFoo = new ScriptExecutionTask( |
| 314 | new DataSubscriber(mDataBroker, METRICS_CONFIG_FOO, SUBSCRIBER_FOO), |
| 315 | DATA, |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 316 | SystemClock.elapsedRealtime()); |
| 317 | PriorityBlockingQueue<ScriptExecutionTask> taskQueue = mDataBroker.getTaskQueue(); |
| 318 | taskQueue.add(mHighPriorityTask); // associated with METRICS_CONFIG_FOO |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 319 | taskQueue.add(mLowPriorityTask); // associated with METRICS_CONFIG_BAR |
| 320 | taskQueue.add(taskWithMetricsConfigFoo); // associated with METRICS_CONFIG_FOO |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 321 | assertThat(taskQueue).hasSize(3); |
| 322 | |
| 323 | mDataBroker.removeMetricsConfiguration(METRICS_CONFIG_FOO); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 324 | |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 325 | assertThat(taskQueue).hasSize(1); |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 326 | assertThat(taskQueue.poll()).isEqualTo(mLowPriorityTask); |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | @Test |
| 330 | public void testRemoveMetricsConfiguration_whenMetricsConfigNonExistent_shouldDoNothing() { |
| 331 | mDataBroker.removeMetricsConfiguration(METRICS_CONFIG_BAR); |
| 332 | |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 333 | assertThat(mDataBroker.getSubscriptionMap()).hasSize(0); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 336 | private void waitForHandlerThreadToFinish() throws Exception { |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 337 | assertWithMessage("handler not idle in %sms", TIMEOUT_MS) |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 338 | .that(mIdleHandlerLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 339 | } |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 340 | |
| 341 | private static class FakeScriptExecutor implements IScriptExecutor { |
| 342 | private IScriptExecutorListener mListener; |
| 343 | private int mApiInvocationCount = 0; |
| 344 | private int mFailApi = 0; |
| 345 | |
| 346 | @Override |
Max Dashouk | dafdd22 | 2021-08-20 10:42:25 -0700 | [diff] [blame] | 347 | public void invokeScript(String scriptBody, String functionName, |
| 348 | PersistableBundle publishedData, @Nullable PersistableBundle savedState, |
| 349 | IScriptExecutorListener listener) |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 350 | throws RemoteException { |
| 351 | mApiInvocationCount++; |
| 352 | mListener = listener; |
| 353 | if (mFailApi > 0) { |
| 354 | mFailApi--; |
| 355 | throw new RemoteException("Simulated failure"); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | @Override |
| 360 | public IBinder asBinder() { |
| 361 | return null; |
| 362 | } |
| 363 | |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 364 | /** Mocks script temporary completion. */ |
Max Dashouk | dafdd22 | 2021-08-20 10:42:25 -0700 | [diff] [blame] | 365 | public void notifyScriptSuccess(PersistableBundle bundle) { |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 366 | try { |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 367 | mListener.onSuccess(bundle); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 368 | } catch (RemoteException e) { |
| 369 | // nothing to do |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | /** Fails the next N invokeScript() call. */ |
| 374 | public void failNextApiCalls(int n) { |
| 375 | mFailApi = n; |
| 376 | } |
| 377 | |
| 378 | /** Returns number of times the ScriptExecutor API was invoked. */ |
| 379 | public int getApiInvocationCount() { |
| 380 | return mApiInvocationCount; |
| 381 | } |
| 382 | } |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 383 | } |