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; |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 33 | import android.os.Handler; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 34 | import android.os.IBinder; |
Max Dashouk | 6987e9c | 2021-09-20 13:19:30 -0700 | [diff] [blame] | 35 | import android.os.ParcelFileDescriptor; |
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; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 51 | import org.mockito.Mock; |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 52 | import org.mockito.Mockito; |
Rui Qiu | af75010 | 2021-09-17 13:27:23 -0700 | [diff] [blame^] | 53 | import org.mockito.invocation.InvocationOnMock; |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 54 | import org.mockito.junit.MockitoJUnitRunner; |
Rui Qiu | af75010 | 2021-09-17 13:27:23 -0700 | [diff] [blame^] | 55 | import org.mockito.stubbing.Answer; |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 56 | |
Rui Qiu | da9f0ba | 2021-09-14 12:15:37 -0700 | [diff] [blame] | 57 | import java.nio.file.Files; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 58 | import java.util.Collections; |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 59 | import java.util.concurrent.CountDownLatch; |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 60 | import java.util.concurrent.PriorityBlockingQueue; |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 61 | import java.util.concurrent.TimeUnit; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 62 | |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 63 | @RunWith(MockitoJUnitRunner.class) |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 64 | public class DataBrokerTest { |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 65 | private static final int PROP_ID = 100; |
| 66 | private static final int PROP_AREA = 200; |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 67 | private static final int PRIORITY_HIGH = 1; |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 68 | private static final int PRIORITY_LOW = 100; |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 69 | private static final long TIMEOUT_MS = 5_000L; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 70 | private static final CarPropertyConfig<Integer> PROP_CONFIG = |
| 71 | CarPropertyConfig.newBuilder(Integer.class, PROP_ID, PROP_AREA).setAccess( |
| 72 | CarPropertyConfig.VEHICLE_PROPERTY_ACCESS_READ).build(); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 73 | private static final TelemetryProto.VehiclePropertyPublisher |
| 74 | VEHICLE_PROPERTY_PUBLISHER_CONFIGURATION = |
| 75 | TelemetryProto.VehiclePropertyPublisher.newBuilder().setReadRate( |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 76 | 1).setVehiclePropertyId(PROP_ID).build(); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 77 | private static final TelemetryProto.Publisher PUBLISHER_CONFIGURATION = |
| 78 | TelemetryProto.Publisher.newBuilder().setVehicleProperty( |
| 79 | VEHICLE_PROPERTY_PUBLISHER_CONFIGURATION).build(); |
| 80 | private static final TelemetryProto.Subscriber SUBSCRIBER_FOO = |
| 81 | TelemetryProto.Subscriber.newBuilder().setHandler("function_name_foo").setPublisher( |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 82 | PUBLISHER_CONFIGURATION).setPriority(PRIORITY_HIGH).build(); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 83 | private static final TelemetryProto.MetricsConfig METRICS_CONFIG_FOO = |
| 84 | TelemetryProto.MetricsConfig.newBuilder().setName("Foo").setVersion( |
| 85 | 1).addSubscribers(SUBSCRIBER_FOO).build(); |
| 86 | private static final TelemetryProto.Subscriber SUBSCRIBER_BAR = |
| 87 | TelemetryProto.Subscriber.newBuilder().setHandler("function_name_bar").setPublisher( |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 88 | PUBLISHER_CONFIGURATION).setPriority(PRIORITY_LOW).build(); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 89 | private static final TelemetryProto.MetricsConfig METRICS_CONFIG_BAR = |
| 90 | TelemetryProto.MetricsConfig.newBuilder().setName("Bar").setVersion( |
| 91 | 1).addSubscribers(SUBSCRIBER_BAR).build(); |
| 92 | |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 93 | |
| 94 | // when count reaches 0, all handler messages are scheduled to be dispatched after current time |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 95 | private CountDownLatch mIdleHandlerLatch = new CountDownLatch(1); |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 96 | private PersistableBundle mData = new PersistableBundle(); |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 97 | private DataBrokerImpl mDataBroker; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 98 | private FakeScriptExecutor mFakeScriptExecutor; |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 99 | private ScriptExecutionTask mHighPriorityTask; |
| 100 | private ScriptExecutionTask mLowPriorityTask; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 101 | |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 102 | @Mock |
| 103 | private Context mMockContext; |
| 104 | @Mock |
| 105 | private CarPropertyService mMockCarPropertyService; |
| 106 | @Mock |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 107 | private Handler mMockHandler; |
| 108 | @Mock |
Zhomart Mukhamejanov | cdcb4e1 | 2021-08-23 18:24:40 -0700 | [diff] [blame] | 109 | private StatsManagerProxy mMockStatsManager; |
| 110 | @Mock |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 111 | private IBinder mMockScriptExecutorBinder; |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 112 | @Mock |
| 113 | private ResultStore mMockResultStore; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 114 | |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 115 | @Before |
Rui Qiu | da9f0ba | 2021-09-14 12:15:37 -0700 | [diff] [blame] | 116 | public void setUp() throws Exception { |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 117 | when(mMockCarPropertyService.getPropertyList()) |
| 118 | .thenReturn(Collections.singletonList(PROP_CONFIG)); |
Zhomart Mukhamejanov | cdcb4e1 | 2021-08-23 18:24:40 -0700 | [diff] [blame] | 119 | PublisherFactory factory = new PublisherFactory( |
Rui Qiu | da9f0ba | 2021-09-14 12:15:37 -0700 | [diff] [blame] | 120 | mMockCarPropertyService, mMockHandler, mMockStatsManager, |
| 121 | Files.createTempDirectory("telemetry_test").toFile()); |
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 | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 140 | mData, |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 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 | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 144 | mData, |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 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 | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 203 | waitForHandlerThreadToFinish(); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 204 | // verify task is not polled |
| 205 | assertThat(taskQueue.peek()).isEqualTo(mHighPriorityTask); |
| 206 | // expect one invocation for the task that is running |
| 207 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(1); |
| 208 | } |
| 209 | |
| 210 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 211 | public void testScheduleNextTask_whenTaskCompletes_shouldAutomaticallyScheduleNextTask() |
| 212 | throws Exception { |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 213 | PriorityBlockingQueue<ScriptExecutionTask> taskQueue = mDataBroker.getTaskQueue(); |
| 214 | // add two tasks into the queue for execution |
| 215 | taskQueue.add(mHighPriorityTask); |
| 216 | taskQueue.add(mHighPriorityTask); |
| 217 | |
| 218 | mDataBroker.scheduleNextTask(); // start a task |
| 219 | waitForHandlerThreadToFinish(); |
| 220 | // end a task, should automatically schedule the next task |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 221 | mFakeScriptExecutor.notifyScriptSuccess(mData); // posts to telemetry 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 | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 231 | mData.putBoolean("script is finished", false); |
| 232 | mData.putDouble("value of euler's number", 2.71828); |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 233 | mDataBroker.getTaskQueue().add(mHighPriorityTask); |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 234 | |
| 235 | mDataBroker.scheduleNextTask(); |
| 236 | waitForHandlerThreadToFinish(); |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 237 | mFakeScriptExecutor.notifyScriptSuccess(mData); // posts to telemetry handler |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 238 | |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 239 | waitForHandlerThreadToFinish(); |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 240 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(1); |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 241 | verify(mMockResultStore).putInterimResult( |
| 242 | eq(mHighPriorityTask.getMetricsConfig().getName()), eq(mData)); |
| 243 | } |
| 244 | |
| 245 | @Test |
Rui Qiu | 862d6ea | 2021-08-17 15:00:09 -0700 | [diff] [blame] | 246 | public void testScheduleNextTask_onScriptError_shouldStoreErrorObject() throws Exception { |
| 247 | mDataBroker.getTaskQueue().add(mHighPriorityTask); |
| 248 | TelemetryProto.TelemetryError.ErrorType errorType = |
| 249 | TelemetryProto.TelemetryError.ErrorType.LUA_RUNTIME_ERROR; |
| 250 | String errorMessage = "test onError"; |
| 251 | TelemetryProto.TelemetryError expectedError = TelemetryProto.TelemetryError.newBuilder() |
| 252 | .setErrorType(errorType) |
| 253 | .setMessage(errorMessage) |
| 254 | .build(); |
| 255 | |
| 256 | mDataBroker.scheduleNextTask(); |
| 257 | waitForHandlerThreadToFinish(); |
| 258 | mFakeScriptExecutor.notifyScriptError(errorType.getNumber(), errorMessage); |
| 259 | |
| 260 | waitForHandlerThreadToFinish(); |
| 261 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(1); |
| 262 | verify(mMockResultStore).putError(eq(METRICS_CONFIG_FOO.getName()), eq(expectedError)); |
| 263 | } |
| 264 | |
| 265 | @Test |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 266 | public void testScheduleNextTask_whenScriptFinishes_shouldStoreFinalResult() |
| 267 | throws Exception { |
| 268 | mData.putBoolean("script is finished", true); |
| 269 | mData.putDouble("value of pi", 3.14159265359); |
| 270 | mDataBroker.getTaskQueue().add(mHighPriorityTask); |
| 271 | |
| 272 | mDataBroker.scheduleNextTask(); |
| 273 | waitForHandlerThreadToFinish(); |
| 274 | mFakeScriptExecutor.notifyScriptFinish(mData); // posts to telemetry handler |
| 275 | |
| 276 | waitForHandlerThreadToFinish(); |
| 277 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(1); |
| 278 | verify(mMockResultStore).putFinalResult( |
| 279 | eq(mHighPriorityTask.getMetricsConfig().getName()), eq(mData)); |
| 280 | } |
| 281 | |
| 282 | @Test |
| 283 | public void testScheduleNextTask_whenInterimDataExists_shouldPassToScriptExecutor() |
| 284 | throws Exception { |
| 285 | mData.putDouble("value of golden ratio", 1.618033); |
| 286 | mDataBroker.getTaskQueue().add(mHighPriorityTask); |
| 287 | when(mMockResultStore.getInterimResult(mHighPriorityTask.getMetricsConfig().getName())) |
| 288 | .thenReturn(mData); |
| 289 | |
| 290 | mDataBroker.scheduleNextTask(); |
| 291 | |
| 292 | waitForHandlerThreadToFinish(); |
| 293 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(1); |
| 294 | assertThat(mFakeScriptExecutor.getSavedState()).isEqualTo(mData); |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | @Test |
Rui Qiu | af75010 | 2021-09-17 13:27:23 -0700 | [diff] [blame^] | 298 | public void testScheduleNextTask_bindScriptExecutorFailedOnce_shouldRebind() |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 299 | throws Exception { |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 300 | Mockito.reset(mMockContext); |
Rui Qiu | af75010 | 2021-09-17 13:27:23 -0700 | [diff] [blame^] | 301 | when(mMockContext.bindServiceAsUser(any(), any(), anyInt(), any())).thenAnswer( |
| 302 | new Answer() { |
| 303 | private int mCount = 0; |
| 304 | |
| 305 | @Override |
| 306 | public Object answer(InvocationOnMock invocation) { |
| 307 | if (mCount++ == 1) { |
| 308 | return false; // fail first attempt |
| 309 | } |
| 310 | ServiceConnection conn = invocation.getArgument(1); |
| 311 | conn.onServiceConnected(null, mMockScriptExecutorBinder); |
| 312 | return true; // second attempt should succeed |
| 313 | } |
| 314 | }); |
| 315 | mDataBroker.mBindScriptExecutorDelayMillis = 0L; // immediately rebind for testing purpose |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 316 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_FOO); |
| 317 | PriorityBlockingQueue<ScriptExecutionTask> taskQueue = mDataBroker.getTaskQueue(); |
| 318 | taskQueue.add(mHighPriorityTask); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 319 | |
| 320 | // will rebind to ScriptExecutor if it is null |
| 321 | mDataBroker.scheduleNextTask(); |
| 322 | |
| 323 | waitForHandlerThreadToFinish(); |
Rui Qiu | af75010 | 2021-09-17 13:27:23 -0700 | [diff] [blame^] | 324 | assertThat(taskQueue.peek()).isNull(); |
| 325 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(1); |
| 326 | } |
| 327 | |
| 328 | @Test |
| 329 | public void testScheduleNextTask_bindScriptExecutorFailedMultipleTimes_shouldDisableBroker() |
| 330 | throws Exception { |
| 331 | // fail 6 future attempts to bind to it |
| 332 | Mockito.reset(mMockContext); |
| 333 | when(mMockContext.bindServiceAsUser(any(), any(), anyInt(), any())) |
| 334 | .thenReturn(false, false, false, false, false, false); |
| 335 | mDataBroker.mBindScriptExecutorDelayMillis = 0L; // immediately rebind for testing purpose |
| 336 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_FOO); |
| 337 | PriorityBlockingQueue<ScriptExecutionTask> taskQueue = mDataBroker.getTaskQueue(); |
| 338 | taskQueue.add(mHighPriorityTask); |
| 339 | |
| 340 | // will rebind to ScriptExecutor if it is null |
| 341 | mDataBroker.scheduleNextTask(); |
| 342 | |
| 343 | waitForHandlerThreadToFinish(); |
| 344 | // broker disabled, all subscribers should have been removed |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 345 | assertThat(mDataBroker.getSubscriptionMap()).hasSize(0); |
| 346 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(0); |
| 347 | } |
| 348 | |
| 349 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 350 | public void testScheduleNextTask_whenScriptExecutorThrowsException_shouldRequeueTask() |
| 351 | throws Exception { |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 352 | PriorityBlockingQueue<ScriptExecutionTask> taskQueue = mDataBroker.getTaskQueue(); |
| 353 | taskQueue.add(mHighPriorityTask); |
| 354 | mFakeScriptExecutor.failNextApiCalls(1); // fail the next invokeScript() call |
| 355 | |
| 356 | mDataBroker.scheduleNextTask(); |
| 357 | |
| 358 | waitForHandlerThreadToFinish(); |
| 359 | // expect invokeScript() to be called and failed, causing the same task to be re-queued |
| 360 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(1); |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 361 | assertThat(taskQueue.peek()).isEqualTo(mHighPriorityTask); |
| 362 | } |
| 363 | |
| 364 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 365 | public void testAddTaskToQueue_shouldInvokeScriptExecutor() throws Exception { |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 366 | mDataBroker.addTaskToQueue(mHighPriorityTask); |
| 367 | |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 368 | waitForHandlerThreadToFinish(); |
| 369 | assertThat(mFakeScriptExecutor.getApiInvocationCount()).isEqualTo(1); |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 372 | @Test |
| 373 | public void testAddMetricsConfiguration_newMetricsConfig() { |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 374 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_BAR); |
| 375 | |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 376 | assertThat(mDataBroker.getSubscriptionMap()).hasSize(1); |
| 377 | assertThat(mDataBroker.getSubscriptionMap()).containsKey(METRICS_CONFIG_BAR.getName()); |
| 378 | // there should be one data subscriber in the subscription list of METRICS_CONFIG_BAR |
| 379 | assertThat(mDataBroker.getSubscriptionMap().get(METRICS_CONFIG_BAR.getName())).hasSize(1); |
| 380 | } |
| 381 | |
| 382 | |
| 383 | @Test |
| 384 | public void testAddMetricsConfiguration_duplicateMetricsConfig_shouldDoNothing() { |
| 385 | // this metrics config has already been added in setUp() |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 386 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_FOO); |
| 387 | |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 388 | assertThat(mDataBroker.getSubscriptionMap()).hasSize(1); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 389 | assertThat(mDataBroker.getSubscriptionMap()).containsKey(METRICS_CONFIG_FOO.getName()); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 390 | assertThat(mDataBroker.getSubscriptionMap().get(METRICS_CONFIG_FOO.getName())).hasSize(1); |
| 391 | } |
| 392 | |
| 393 | @Test |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 394 | public void testRemoveMetricsConfiguration_shouldRemoveAllAssociatedTasks() { |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 395 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_FOO); |
| 396 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_BAR); |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 397 | ScriptExecutionTask taskWithMetricsConfigFoo = new ScriptExecutionTask( |
| 398 | new DataSubscriber(mDataBroker, METRICS_CONFIG_FOO, SUBSCRIBER_FOO), |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 399 | mData, |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 400 | SystemClock.elapsedRealtime()); |
| 401 | PriorityBlockingQueue<ScriptExecutionTask> taskQueue = mDataBroker.getTaskQueue(); |
| 402 | taskQueue.add(mHighPriorityTask); // associated with METRICS_CONFIG_FOO |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 403 | taskQueue.add(mLowPriorityTask); // associated with METRICS_CONFIG_BAR |
| 404 | taskQueue.add(taskWithMetricsConfigFoo); // associated with METRICS_CONFIG_FOO |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 405 | assertThat(taskQueue).hasSize(3); |
| 406 | |
| 407 | mDataBroker.removeMetricsConfiguration(METRICS_CONFIG_FOO); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 408 | |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 409 | assertThat(taskQueue).hasSize(1); |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 410 | assertThat(taskQueue.poll()).isEqualTo(mLowPriorityTask); |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | @Test |
| 414 | public void testRemoveMetricsConfiguration_whenMetricsConfigNonExistent_shouldDoNothing() { |
| 415 | mDataBroker.removeMetricsConfiguration(METRICS_CONFIG_BAR); |
| 416 | |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 417 | assertThat(mDataBroker.getSubscriptionMap()).hasSize(0); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 420 | private void waitForHandlerThreadToFinish() throws Exception { |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 421 | assertWithMessage("handler not idle in %sms", TIMEOUT_MS) |
Rui Qiu | a4a8fd0 | 2021-09-02 19:08:40 -0700 | [diff] [blame] | 422 | .that(mIdleHandlerLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue(); |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 423 | mIdleHandlerLatch = new CountDownLatch(1); // reset idle handler condition |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 424 | } |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 425 | |
| 426 | private static class FakeScriptExecutor implements IScriptExecutor { |
| 427 | private IScriptExecutorListener mListener; |
| 428 | private int mApiInvocationCount = 0; |
| 429 | private int mFailApi = 0; |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 430 | private PersistableBundle mSavedState = null; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 431 | |
| 432 | @Override |
Max Dashouk | dafdd22 | 2021-08-20 10:42:25 -0700 | [diff] [blame] | 433 | public void invokeScript(String scriptBody, String functionName, |
| 434 | PersistableBundle publishedData, @Nullable PersistableBundle savedState, |
| 435 | IScriptExecutorListener listener) |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 436 | throws RemoteException { |
| 437 | mApiInvocationCount++; |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 438 | mSavedState = savedState; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 439 | mListener = listener; |
| 440 | if (mFailApi > 0) { |
| 441 | mFailApi--; |
| 442 | throw new RemoteException("Simulated failure"); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | @Override |
Max Dashouk | 6987e9c | 2021-09-20 13:19:30 -0700 | [diff] [blame] | 447 | public void invokeScriptForLargeInput(String scriptBody, String functionName, |
| 448 | ParcelFileDescriptor publishedDataFileDescriptor, |
| 449 | @Nullable PersistableBundle savedState, |
| 450 | IScriptExecutorListener listener) throws RemoteException { |
| 451 | mApiInvocationCount++; |
| 452 | mSavedState = savedState; |
| 453 | mListener = listener; |
| 454 | if (mFailApi > 0) { |
| 455 | mFailApi--; |
| 456 | throw new RemoteException("Simulated failure"); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | @Override |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 461 | public IBinder asBinder() { |
| 462 | return null; |
| 463 | } |
| 464 | |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 465 | /** Mocks script temporary completion. */ |
Max Dashouk | dafdd22 | 2021-08-20 10:42:25 -0700 | [diff] [blame] | 466 | public void notifyScriptSuccess(PersistableBundle bundle) { |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 467 | try { |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 468 | mListener.onSuccess(bundle); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 469 | } catch (RemoteException e) { |
| 470 | // nothing to do |
| 471 | } |
| 472 | } |
| 473 | |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 474 | /** Mocks script producing final result. */ |
| 475 | public void notifyScriptFinish(PersistableBundle bundle) { |
| 476 | try { |
| 477 | mListener.onScriptFinished(bundle); |
| 478 | } catch (RemoteException e) { |
| 479 | // nothing to do |
| 480 | } |
| 481 | } |
| 482 | |
Rui Qiu | 862d6ea | 2021-08-17 15:00:09 -0700 | [diff] [blame] | 483 | /** Mocks script finished with error. */ |
| 484 | public void notifyScriptError(int errorType, String errorMessage) { |
| 485 | try { |
| 486 | mListener.onError(errorType, errorMessage, null); |
| 487 | } catch (RemoteException e) { |
| 488 | // nothing to do |
| 489 | } |
| 490 | } |
| 491 | |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 492 | /** Fails the next N invokeScript() call. */ |
| 493 | public void failNextApiCalls(int n) { |
| 494 | mFailApi = n; |
| 495 | } |
| 496 | |
| 497 | /** Returns number of times the ScriptExecutor API was invoked. */ |
| 498 | public int getApiInvocationCount() { |
| 499 | return mApiInvocationCount; |
| 500 | } |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 501 | |
| 502 | /** Returns the interim data passed in invokeScript(). */ |
| 503 | public PersistableBundle getSavedState() { |
| 504 | return mSavedState; |
| 505 | } |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 506 | } |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 507 | } |