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