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; |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 30 | import android.car.AbstractExtendedMockitoCarServiceTestCase; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 31 | import android.car.hardware.CarPropertyConfig; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 32 | import android.content.Context; |
| 33 | import android.content.ServiceConnection; |
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; |
Max Dashouk | 6987e9c | 2021-09-20 13:19:30 -0700 | [diff] [blame] | 36 | import android.os.ParcelFileDescriptor; |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 37 | import android.os.PersistableBundle; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 38 | import android.os.RemoteException; |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 39 | import android.os.SystemClock; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 40 | |
| 41 | import com.android.car.CarPropertyService; |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 42 | import com.android.car.telemetry.ResultStore; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 43 | import com.android.car.telemetry.TelemetryProto; |
| 44 | import com.android.car.telemetry.publisher.PublisherFactory; |
Zhomart Mukhamejanov | cdcb4e1 | 2021-08-23 18:24:40 -0700 | [diff] [blame] | 45 | import com.android.car.telemetry.publisher.StatsManagerProxy; |
Max Dashouk | 9912ac3 | 2021-08-13 17:54:51 +0000 | [diff] [blame] | 46 | import com.android.car.telemetry.scriptexecutorinterface.IScriptExecutor; |
| 47 | import com.android.car.telemetry.scriptexecutorinterface.IScriptExecutorListener; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 48 | |
| 49 | import org.junit.Before; |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 50 | import org.junit.Test; |
| 51 | import org.junit.runner.RunWith; |
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 | af75010 | 2021-09-17 13:27:23 -0700 | [diff] [blame] | 54 | import org.mockito.invocation.InvocationOnMock; |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 55 | import org.mockito.junit.MockitoJUnitRunner; |
Rui Qiu | af75010 | 2021-09-17 13:27:23 -0700 | [diff] [blame] | 56 | import org.mockito.stubbing.Answer; |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 57 | |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 58 | import java.io.IOException; |
| 59 | import java.io.InputStream; |
Rui Qiu | da9f0ba | 2021-09-14 12:15:37 -0700 | [diff] [blame] | 60 | import java.nio.file.Files; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 61 | import java.util.Collections; |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 62 | import java.util.Objects; |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 63 | import java.util.concurrent.CountDownLatch; |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 64 | import java.util.concurrent.PriorityBlockingQueue; |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 65 | import java.util.concurrent.TimeUnit; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 66 | |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 67 | @RunWith(MockitoJUnitRunner.class) |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 68 | public class DataBrokerTest extends AbstractExtendedMockitoCarServiceTestCase { |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 69 | private static final int PROP_ID = 100; |
| 70 | private static final int PROP_AREA = 200; |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 71 | private static final int PRIORITY_HIGH = 1; |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 72 | private static final int PRIORITY_LOW = 100; |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 73 | private static final long TIMEOUT_MS = 5_000L; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 74 | private static final CarPropertyConfig<Integer> PROP_CONFIG = |
| 75 | CarPropertyConfig.newBuilder(Integer.class, PROP_ID, PROP_AREA).setAccess( |
| 76 | CarPropertyConfig.VEHICLE_PROPERTY_ACCESS_READ).build(); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 77 | private static final TelemetryProto.VehiclePropertyPublisher |
| 78 | VEHICLE_PROPERTY_PUBLISHER_CONFIGURATION = |
| 79 | TelemetryProto.VehiclePropertyPublisher.newBuilder().setReadRate( |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 80 | 1).setVehiclePropertyId(PROP_ID).build(); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 81 | private static final TelemetryProto.Publisher PUBLISHER_CONFIGURATION = |
| 82 | TelemetryProto.Publisher.newBuilder().setVehicleProperty( |
| 83 | VEHICLE_PROPERTY_PUBLISHER_CONFIGURATION).build(); |
| 84 | private static final TelemetryProto.Subscriber SUBSCRIBER_FOO = |
| 85 | TelemetryProto.Subscriber.newBuilder().setHandler("function_name_foo").setPublisher( |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 86 | PUBLISHER_CONFIGURATION).setPriority(PRIORITY_HIGH).build(); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 87 | private static final TelemetryProto.MetricsConfig METRICS_CONFIG_FOO = |
| 88 | TelemetryProto.MetricsConfig.newBuilder().setName("Foo").setVersion( |
| 89 | 1).addSubscribers(SUBSCRIBER_FOO).build(); |
| 90 | private static final TelemetryProto.Subscriber SUBSCRIBER_BAR = |
| 91 | TelemetryProto.Subscriber.newBuilder().setHandler("function_name_bar").setPublisher( |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 92 | PUBLISHER_CONFIGURATION).setPriority(PRIORITY_LOW).build(); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 93 | private static final TelemetryProto.MetricsConfig METRICS_CONFIG_BAR = |
| 94 | TelemetryProto.MetricsConfig.newBuilder().setName("Bar").setVersion( |
| 95 | 1).addSubscribers(SUBSCRIBER_BAR).build(); |
| 96 | |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 97 | |
| 98 | // 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] | 99 | private CountDownLatch mIdleHandlerLatch = new CountDownLatch(1); |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 100 | private PersistableBundle mData = new PersistableBundle(); |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 101 | private DataBrokerImpl mDataBroker; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 102 | private FakeScriptExecutor mFakeScriptExecutor; |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 103 | private ScriptExecutionTask mHighPriorityTask; |
| 104 | private ScriptExecutionTask mLowPriorityTask; |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 105 | |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 106 | @Mock |
| 107 | private Context mMockContext; |
| 108 | @Mock |
| 109 | private CarPropertyService mMockCarPropertyService; |
| 110 | @Mock |
Rui Qiu | aa499ce | 2021-09-29 15:47:06 -0700 | [diff] [blame^] | 111 | private DataBroker.ScriptFinishedCallback mMockScriptFinishedCallback; |
| 112 | @Mock |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 113 | private Handler mMockHandler; |
| 114 | @Mock |
Zhomart Mukhamejanov | cdcb4e1 | 2021-08-23 18:24:40 -0700 | [diff] [blame] | 115 | private StatsManagerProxy mMockStatsManager; |
| 116 | @Mock |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 117 | private IBinder mMockScriptExecutorBinder; |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 118 | @Mock |
| 119 | private ResultStore mMockResultStore; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 120 | |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 121 | @Before |
Rui Qiu | da9f0ba | 2021-09-14 12:15:37 -0700 | [diff] [blame] | 122 | public void setUp() throws Exception { |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 123 | when(mMockCarPropertyService.getPropertyList()) |
| 124 | .thenReturn(Collections.singletonList(PROP_CONFIG)); |
Zhomart Mukhamejanov | cdcb4e1 | 2021-08-23 18:24:40 -0700 | [diff] [blame] | 125 | PublisherFactory factory = new PublisherFactory( |
Rui Qiu | da9f0ba | 2021-09-14 12:15:37 -0700 | [diff] [blame] | 126 | mMockCarPropertyService, mMockHandler, mMockStatsManager, |
| 127 | Files.createTempDirectory("telemetry_test").toFile()); |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 128 | mDataBroker = new DataBrokerImpl(mMockContext, factory, mMockResultStore); |
Rui Qiu | aa499ce | 2021-09-29 15:47:06 -0700 | [diff] [blame^] | 129 | mDataBroker.setOnScriptFinishedCallback(mMockScriptFinishedCallback); |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 130 | // add IdleHandler to get notified when all messages and posts are handled |
| 131 | mDataBroker.getTelemetryHandler().getLooper().getQueue().addIdleHandler(() -> { |
| 132 | mIdleHandlerLatch.countDown(); |
| 133 | return true; |
| 134 | }); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 135 | |
| 136 | mFakeScriptExecutor = new FakeScriptExecutor(); |
| 137 | when(mMockScriptExecutorBinder.queryLocalInterface(anyString())) |
| 138 | .thenReturn(mFakeScriptExecutor); |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 139 | when(mMockContext.bindServiceAsUser(any(), any(), anyInt(), any())).thenAnswer(i -> { |
| 140 | ServiceConnection conn = i.getArgument(1); |
| 141 | conn.onServiceConnected(null, mMockScriptExecutorBinder); |
| 142 | return true; |
| 143 | }); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 144 | |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 145 | mHighPriorityTask = new ScriptExecutionTask( |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 146 | new DataSubscriber(mDataBroker, METRICS_CONFIG_FOO, SUBSCRIBER_FOO), |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 147 | mData, |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 148 | SystemClock.elapsedRealtime()); |
| 149 | mLowPriorityTask = new ScriptExecutionTask( |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 150 | new DataSubscriber(mDataBroker, METRICS_CONFIG_BAR, SUBSCRIBER_BAR), |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 151 | mData, |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 152 | SystemClock.elapsedRealtime()); |
| 153 | } |
| 154 | |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 155 | @Override |
| 156 | protected void onSessionBuilder(CustomMockitoSessionBuilder builder) { |
| 157 | builder.spyStatic(ParcelFileDescriptor.class); |
| 158 | } |
| 159 | |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 160 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 161 | public void testSetTaskExecutionPriority_whenNoTask_shouldNotInvokeScriptExecutor() |
| 162 | throws Exception { |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 163 | mDataBroker.setTaskExecutionPriority(PRIORITY_HIGH); |
| 164 | |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 165 | waitForTelemetryThreadToFinish(); |
| 166 | assertThat(mFakeScriptExecutor.getInvokeScriptCount()).isEqualTo(0); |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 170 | public void testSetTaskExecutionPriority_whenNextTaskPriorityLow_shouldNotRunTask() |
| 171 | throws Exception { |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 172 | mDataBroker.getTaskQueue().add(mLowPriorityTask); |
| 173 | |
| 174 | mDataBroker.setTaskExecutionPriority(PRIORITY_HIGH); |
| 175 | |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 176 | waitForTelemetryThreadToFinish(); |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 177 | // task is not polled |
| 178 | assertThat(mDataBroker.getTaskQueue().peek()).isEqualTo(mLowPriorityTask); |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 179 | assertThat(mFakeScriptExecutor.getInvokeScriptCount()).isEqualTo(0); |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 183 | public void testSetTaskExecutionPriority_whenNextTaskPriorityHigh_shouldInvokeScriptExecutor() |
| 184 | throws Exception { |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 185 | mDataBroker.getTaskQueue().add(mHighPriorityTask); |
| 186 | |
| 187 | mDataBroker.setTaskExecutionPriority(PRIORITY_HIGH); |
| 188 | |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 189 | waitForTelemetryThreadToFinish(); |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 190 | // task is polled and run |
| 191 | assertThat(mDataBroker.getTaskQueue().peek()).isNull(); |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 192 | assertThat(mFakeScriptExecutor.getInvokeScriptCount()).isEqualTo(1); |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 196 | public void testScheduleNextTask_whenNoTask_shouldNotInvokeScriptExecutor() throws Exception { |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 197 | mDataBroker.scheduleNextTask(); |
| 198 | |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 199 | waitForTelemetryThreadToFinish(); |
| 200 | assertThat(mFakeScriptExecutor.getInvokeScriptCount()).isEqualTo(0); |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 204 | public void testScheduleNextTask_whenTaskInProgress_shouldNotInvokeScriptExecutorAgain() |
| 205 | throws Exception { |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 206 | PriorityBlockingQueue<ScriptExecutionTask> taskQueue = mDataBroker.getTaskQueue(); |
| 207 | taskQueue.add(mHighPriorityTask); |
| 208 | mDataBroker.scheduleNextTask(); // start a task |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 209 | waitForTelemetryThreadToFinish(); |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 210 | assertThat(taskQueue.peek()).isNull(); // assert that task is polled and running |
| 211 | taskQueue.add(mHighPriorityTask); // add another task into the queue |
| 212 | |
| 213 | mDataBroker.scheduleNextTask(); // schedule next task while the last task is in progress |
| 214 | |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 215 | waitForTelemetryThreadToFinish(); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 216 | // verify task is not polled |
| 217 | assertThat(taskQueue.peek()).isEqualTo(mHighPriorityTask); |
| 218 | // expect one invocation for the task that is running |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 219 | assertThat(mFakeScriptExecutor.getInvokeScriptCount()).isEqualTo(1); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 223 | public void testScheduleNextTask_whenTaskCompletes_shouldAutomaticallyScheduleNextTask() |
| 224 | throws Exception { |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 225 | PriorityBlockingQueue<ScriptExecutionTask> taskQueue = mDataBroker.getTaskQueue(); |
| 226 | // add two tasks into the queue for execution |
| 227 | taskQueue.add(mHighPriorityTask); |
| 228 | taskQueue.add(mHighPriorityTask); |
| 229 | |
| 230 | mDataBroker.scheduleNextTask(); // start a task |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 231 | waitForTelemetryThreadToFinish(); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 232 | // end a task, should automatically schedule the next task |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 233 | mFakeScriptExecutor.notifyScriptSuccess(mData); // posts to telemetry handler |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 234 | |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 235 | waitForTelemetryThreadToFinish(); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 236 | // verify queue is empty, both tasks are polled and executed |
| 237 | assertThat(taskQueue.peek()).isNull(); |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 238 | assertThat(mFakeScriptExecutor.getInvokeScriptCount()).isEqualTo(2); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 242 | public void testScheduleNextTask_onScriptSuccess_shouldStoreInterimResult() throws Exception { |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 243 | mData.putBoolean("script is finished", false); |
| 244 | mData.putDouble("value of euler's number", 2.71828); |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 245 | mDataBroker.getTaskQueue().add(mHighPriorityTask); |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 246 | |
| 247 | mDataBroker.scheduleNextTask(); |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 248 | waitForTelemetryThreadToFinish(); |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 249 | mFakeScriptExecutor.notifyScriptSuccess(mData); // posts to telemetry handler |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 250 | |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 251 | waitForTelemetryThreadToFinish(); |
| 252 | assertThat(mFakeScriptExecutor.getInvokeScriptCount()).isEqualTo(1); |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 253 | verify(mMockResultStore).putInterimResult( |
| 254 | eq(mHighPriorityTask.getMetricsConfig().getName()), eq(mData)); |
| 255 | } |
| 256 | |
| 257 | @Test |
Rui Qiu | 862d6ea | 2021-08-17 15:00:09 -0700 | [diff] [blame] | 258 | public void testScheduleNextTask_onScriptError_shouldStoreErrorObject() throws Exception { |
| 259 | mDataBroker.getTaskQueue().add(mHighPriorityTask); |
| 260 | TelemetryProto.TelemetryError.ErrorType errorType = |
| 261 | TelemetryProto.TelemetryError.ErrorType.LUA_RUNTIME_ERROR; |
| 262 | String errorMessage = "test onError"; |
| 263 | TelemetryProto.TelemetryError expectedError = TelemetryProto.TelemetryError.newBuilder() |
| 264 | .setErrorType(errorType) |
| 265 | .setMessage(errorMessage) |
| 266 | .build(); |
| 267 | |
| 268 | mDataBroker.scheduleNextTask(); |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 269 | waitForTelemetryThreadToFinish(); |
Rui Qiu | 862d6ea | 2021-08-17 15:00:09 -0700 | [diff] [blame] | 270 | mFakeScriptExecutor.notifyScriptError(errorType.getNumber(), errorMessage); |
| 271 | |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 272 | waitForTelemetryThreadToFinish(); |
| 273 | assertThat(mFakeScriptExecutor.getInvokeScriptCount()).isEqualTo(1); |
Rui Qiu | 862d6ea | 2021-08-17 15:00:09 -0700 | [diff] [blame] | 274 | verify(mMockResultStore).putError(eq(METRICS_CONFIG_FOO.getName()), eq(expectedError)); |
| 275 | } |
| 276 | |
| 277 | @Test |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 278 | public void testScheduleNextTask_whenScriptFinishes_shouldStoreFinalResult() |
| 279 | throws Exception { |
| 280 | mData.putBoolean("script is finished", true); |
| 281 | mData.putDouble("value of pi", 3.14159265359); |
| 282 | mDataBroker.getTaskQueue().add(mHighPriorityTask); |
| 283 | |
| 284 | mDataBroker.scheduleNextTask(); |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 285 | waitForTelemetryThreadToFinish(); |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 286 | mFakeScriptExecutor.notifyScriptFinish(mData); // posts to telemetry handler |
| 287 | |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 288 | waitForTelemetryThreadToFinish(); |
| 289 | assertThat(mFakeScriptExecutor.getInvokeScriptCount()).isEqualTo(1); |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 290 | verify(mMockResultStore).putFinalResult( |
| 291 | eq(mHighPriorityTask.getMetricsConfig().getName()), eq(mData)); |
Rui Qiu | aa499ce | 2021-09-29 15:47:06 -0700 | [diff] [blame^] | 292 | verify(mMockScriptFinishedCallback).onScriptFinished(eq(METRICS_CONFIG_FOO.getName())); |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | @Test |
| 296 | public void testScheduleNextTask_whenInterimDataExists_shouldPassToScriptExecutor() |
| 297 | throws Exception { |
| 298 | mData.putDouble("value of golden ratio", 1.618033); |
| 299 | mDataBroker.getTaskQueue().add(mHighPriorityTask); |
| 300 | when(mMockResultStore.getInterimResult(mHighPriorityTask.getMetricsConfig().getName())) |
| 301 | .thenReturn(mData); |
| 302 | |
| 303 | mDataBroker.scheduleNextTask(); |
| 304 | |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 305 | waitForTelemetryThreadToFinish(); |
| 306 | assertThat(mFakeScriptExecutor.getInvokeScriptCount()).isEqualTo(1); |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 307 | assertThat(mFakeScriptExecutor.getSavedState()).isEqualTo(mData); |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | @Test |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 311 | public void testScheduleNextTask_largeInput_shouldPipeData() throws Exception { |
| 312 | mData.putBooleanArray("1 MB Array", new boolean [1024 * 1024]); |
| 313 | mDataBroker.getTaskQueue().add(mHighPriorityTask); |
| 314 | |
| 315 | mDataBroker.scheduleNextTask(); |
| 316 | |
| 317 | waitForTelemetryThreadToFinish(); |
| 318 | assertThat(mFakeScriptExecutor.getInvokeScriptForLargeInputCount()).isEqualTo(1); |
| 319 | } |
| 320 | |
| 321 | @Test |
| 322 | public void testScheduleNextTask_largeInputPipeIOException_shouldIgnoreCurrentTask() |
| 323 | throws Exception { |
| 324 | mData.putBooleanArray("1 MB Array", new boolean [1024 * 1024]); |
| 325 | PriorityBlockingQueue<ScriptExecutionTask> taskQueue = mDataBroker.getTaskQueue(); |
| 326 | taskQueue.add(mHighPriorityTask); // invokeScriptForLargeInput() path |
| 327 | taskQueue.add(new ScriptExecutionTask( |
| 328 | new DataSubscriber(mDataBroker, METRICS_CONFIG_FOO, SUBSCRIBER_FOO), |
| 329 | new PersistableBundle(), |
| 330 | SystemClock.elapsedRealtime())); // invokeScript() path |
| 331 | ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe(); |
| 332 | when(ParcelFileDescriptor.createPipe()).thenReturn(fds); |
| 333 | fds[1].close(); // cause IO Exception in invokeScriptForLargeInput() path |
| 334 | |
| 335 | mDataBroker.scheduleNextTask(); |
| 336 | |
| 337 | waitForTelemetryThreadToFinish(); |
| 338 | assertThat(mFakeScriptExecutor.getInvokeScriptForLargeInputCount()).isEqualTo(1); |
| 339 | assertThat(mFakeScriptExecutor.getInvokeScriptCount()).isEqualTo(1); |
| 340 | assertThat(taskQueue).isEmpty(); |
| 341 | } |
| 342 | |
| 343 | @Test |
Rui Qiu | af75010 | 2021-09-17 13:27:23 -0700 | [diff] [blame] | 344 | public void testScheduleNextTask_bindScriptExecutorFailedOnce_shouldRebind() |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 345 | throws Exception { |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 346 | Mockito.reset(mMockContext); |
Rui Qiu | af75010 | 2021-09-17 13:27:23 -0700 | [diff] [blame] | 347 | when(mMockContext.bindServiceAsUser(any(), any(), anyInt(), any())).thenAnswer( |
| 348 | new Answer() { |
| 349 | private int mCount = 0; |
| 350 | |
| 351 | @Override |
| 352 | public Object answer(InvocationOnMock invocation) { |
| 353 | if (mCount++ == 1) { |
| 354 | return false; // fail first attempt |
| 355 | } |
| 356 | ServiceConnection conn = invocation.getArgument(1); |
| 357 | conn.onServiceConnected(null, mMockScriptExecutorBinder); |
| 358 | return true; // second attempt should succeed |
| 359 | } |
| 360 | }); |
| 361 | mDataBroker.mBindScriptExecutorDelayMillis = 0L; // immediately rebind for testing purpose |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 362 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_FOO); |
| 363 | PriorityBlockingQueue<ScriptExecutionTask> taskQueue = mDataBroker.getTaskQueue(); |
| 364 | taskQueue.add(mHighPriorityTask); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 365 | |
| 366 | // will rebind to ScriptExecutor if it is null |
| 367 | mDataBroker.scheduleNextTask(); |
| 368 | |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 369 | waitForTelemetryThreadToFinish(); |
Rui Qiu | af75010 | 2021-09-17 13:27:23 -0700 | [diff] [blame] | 370 | assertThat(taskQueue.peek()).isNull(); |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 371 | assertThat(mFakeScriptExecutor.getInvokeScriptCount()).isEqualTo(1); |
Rui Qiu | af75010 | 2021-09-17 13:27:23 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | @Test |
| 375 | public void testScheduleNextTask_bindScriptExecutorFailedMultipleTimes_shouldDisableBroker() |
| 376 | throws Exception { |
| 377 | // fail 6 future attempts to bind to it |
| 378 | Mockito.reset(mMockContext); |
| 379 | when(mMockContext.bindServiceAsUser(any(), any(), anyInt(), any())) |
| 380 | .thenReturn(false, false, false, false, false, false); |
| 381 | mDataBroker.mBindScriptExecutorDelayMillis = 0L; // immediately rebind for testing purpose |
| 382 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_FOO); |
| 383 | PriorityBlockingQueue<ScriptExecutionTask> taskQueue = mDataBroker.getTaskQueue(); |
| 384 | taskQueue.add(mHighPriorityTask); |
| 385 | |
| 386 | // will rebind to ScriptExecutor if it is null |
| 387 | mDataBroker.scheduleNextTask(); |
| 388 | |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 389 | waitForTelemetryThreadToFinish(); |
Rui Qiu | af75010 | 2021-09-17 13:27:23 -0700 | [diff] [blame] | 390 | // broker disabled, all subscribers should have been removed |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 391 | assertThat(mDataBroker.getSubscriptionMap()).hasSize(0); |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 392 | assertThat(mFakeScriptExecutor.getInvokeScriptCount()).isEqualTo(0); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | @Test |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 396 | public void testScheduleNextTask_whenScriptExecutorThrowsException_shouldResetAndTryAgain() |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 397 | throws Exception { |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 398 | PriorityBlockingQueue<ScriptExecutionTask> taskQueue = mDataBroker.getTaskQueue(); |
| 399 | taskQueue.add(mHighPriorityTask); |
| 400 | mFakeScriptExecutor.failNextApiCalls(1); // fail the next invokeScript() call |
| 401 | |
| 402 | mDataBroker.scheduleNextTask(); |
| 403 | |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 404 | waitForTelemetryThreadToFinish(); |
| 405 | // invokeScript() failed, task is re-queued and re-run |
| 406 | assertThat(mFakeScriptExecutor.getInvokeScriptCount()).isEqualTo(2); |
| 407 | assertThat(taskQueue).isEmpty(); |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | @Test |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 411 | public void testAddTaskToQueue_shouldInvokeScriptExecutor() throws Exception { |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 412 | mDataBroker.addTaskToQueue(mHighPriorityTask); |
| 413 | |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 414 | waitForTelemetryThreadToFinish(); |
| 415 | assertThat(mFakeScriptExecutor.getInvokeScriptCount()).isEqualTo(1); |
Zhomart Mukhamejanov | 44570cb | 2021-06-07 10:23:13 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 418 | @Test |
| 419 | public void testAddMetricsConfiguration_newMetricsConfig() { |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 420 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_BAR); |
| 421 | |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 422 | assertThat(mDataBroker.getSubscriptionMap()).hasSize(1); |
| 423 | assertThat(mDataBroker.getSubscriptionMap()).containsKey(METRICS_CONFIG_BAR.getName()); |
| 424 | // there should be one data subscriber in the subscription list of METRICS_CONFIG_BAR |
| 425 | assertThat(mDataBroker.getSubscriptionMap().get(METRICS_CONFIG_BAR.getName())).hasSize(1); |
| 426 | } |
| 427 | |
| 428 | |
| 429 | @Test |
| 430 | public void testAddMetricsConfiguration_duplicateMetricsConfig_shouldDoNothing() { |
| 431 | // this metrics config has already been added in setUp() |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 432 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_FOO); |
| 433 | |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 434 | assertThat(mDataBroker.getSubscriptionMap()).hasSize(1); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 435 | assertThat(mDataBroker.getSubscriptionMap()).containsKey(METRICS_CONFIG_FOO.getName()); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 436 | assertThat(mDataBroker.getSubscriptionMap().get(METRICS_CONFIG_FOO.getName())).hasSize(1); |
| 437 | } |
| 438 | |
| 439 | @Test |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 440 | public void testRemoveMetricsConfiguration_shouldRemoveAllAssociatedTasks() { |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 441 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_FOO); |
| 442 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_BAR); |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 443 | ScriptExecutionTask taskWithMetricsConfigFoo = new ScriptExecutionTask( |
| 444 | new DataSubscriber(mDataBroker, METRICS_CONFIG_FOO, SUBSCRIBER_FOO), |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 445 | mData, |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 446 | SystemClock.elapsedRealtime()); |
| 447 | PriorityBlockingQueue<ScriptExecutionTask> taskQueue = mDataBroker.getTaskQueue(); |
| 448 | taskQueue.add(mHighPriorityTask); // associated with METRICS_CONFIG_FOO |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 449 | taskQueue.add(mLowPriorityTask); // associated with METRICS_CONFIG_BAR |
| 450 | taskQueue.add(taskWithMetricsConfigFoo); // associated with METRICS_CONFIG_FOO |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 451 | assertThat(taskQueue).hasSize(3); |
| 452 | |
Rui Qiu | 9f80c7c | 2021-09-28 15:15:36 -0700 | [diff] [blame] | 453 | mDataBroker.removeMetricsConfiguration(METRICS_CONFIG_FOO.getName()); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 454 | |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 455 | assertThat(taskQueue).hasSize(1); |
Rui Qiu | d95f95d | 2021-08-17 12:29:33 -0700 | [diff] [blame] | 456 | assertThat(taskQueue.poll()).isEqualTo(mLowPriorityTask); |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | @Test |
| 460 | public void testRemoveMetricsConfiguration_whenMetricsConfigNonExistent_shouldDoNothing() { |
Rui Qiu | 9f80c7c | 2021-09-28 15:15:36 -0700 | [diff] [blame] | 461 | mDataBroker.removeMetricsConfiguration(METRICS_CONFIG_BAR.getName()); |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 462 | |
Rui Qiu | f14c6a2 | 2021-07-14 10:39:16 -0700 | [diff] [blame] | 463 | assertThat(mDataBroker.getSubscriptionMap()).hasSize(0); |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Rui Qiu | 9f80c7c | 2021-09-28 15:15:36 -0700 | [diff] [blame] | 466 | @Test |
| 467 | public void testRemoveAllMetricsConfigurations_shouldRemoveTasksAndClearSubscriptionMap() { |
| 468 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_FOO); |
| 469 | mDataBroker.addMetricsConfiguration(METRICS_CONFIG_BAR); |
| 470 | PriorityBlockingQueue<ScriptExecutionTask> taskQueue = mDataBroker.getTaskQueue(); |
| 471 | taskQueue.add(mHighPriorityTask); // associated with METRICS_CONFIG_FOO |
| 472 | taskQueue.add(mLowPriorityTask); // associated with METRICS_CONFIG_BAR |
| 473 | |
| 474 | mDataBroker.removeAllMetricsConfigurations(); |
| 475 | |
| 476 | assertThat(taskQueue).isEmpty(); |
| 477 | assertThat(mDataBroker.getSubscriptionMap()).isEmpty(); |
| 478 | } |
| 479 | |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 480 | private void waitForTelemetryThreadToFinish() throws Exception { |
Rui Qiu | 033041e | 2021-06-21 14:59:11 -0700 | [diff] [blame] | 481 | assertWithMessage("handler not idle in %sms", TIMEOUT_MS) |
Rui Qiu | a4a8fd0 | 2021-09-02 19:08:40 -0700 | [diff] [blame] | 482 | .that(mIdleHandlerLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue(); |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 483 | mIdleHandlerLatch = new CountDownLatch(1); // reset idle handler condition |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 484 | } |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 485 | |
| 486 | private static class FakeScriptExecutor implements IScriptExecutor { |
| 487 | private IScriptExecutorListener mListener; |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 488 | private int mInvokeScriptCount = 0; |
| 489 | private int mInvokeScriptForLargeInputCount = 0; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 490 | private int mFailApi = 0; |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 491 | private PersistableBundle mSavedState = null; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 492 | |
| 493 | @Override |
Max Dashouk | dafdd22 | 2021-08-20 10:42:25 -0700 | [diff] [blame] | 494 | public void invokeScript(String scriptBody, String functionName, |
| 495 | PersistableBundle publishedData, @Nullable PersistableBundle savedState, |
| 496 | IScriptExecutorListener listener) |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 497 | throws RemoteException { |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 498 | mInvokeScriptCount++; |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 499 | mSavedState = savedState; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 500 | mListener = listener; |
| 501 | if (mFailApi > 0) { |
| 502 | mFailApi--; |
| 503 | throw new RemoteException("Simulated failure"); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | @Override |
Max Dashouk | 6987e9c | 2021-09-20 13:19:30 -0700 | [diff] [blame] | 508 | public void invokeScriptForLargeInput(String scriptBody, String functionName, |
| 509 | ParcelFileDescriptor publishedDataFileDescriptor, |
| 510 | @Nullable PersistableBundle savedState, |
| 511 | IScriptExecutorListener listener) throws RemoteException { |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 512 | mInvokeScriptForLargeInputCount++; |
Max Dashouk | 6987e9c | 2021-09-20 13:19:30 -0700 | [diff] [blame] | 513 | mSavedState = savedState; |
| 514 | mListener = listener; |
| 515 | if (mFailApi > 0) { |
| 516 | mFailApi--; |
| 517 | throw new RemoteException("Simulated failure"); |
| 518 | } |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 519 | // Since DataBrokerImpl and FakeScriptExecutor are in the same process, they do not |
| 520 | // use real IPC and share the fd. When DataBroker closes the fd, it affects |
| 521 | // FakeScriptExecutor. Therefore FakeScriptExecutor must dup the fd before it is |
| 522 | // closed by DataBroker |
| 523 | ParcelFileDescriptor dup = null; |
| 524 | try { |
| 525 | dup = publishedDataFileDescriptor.dup(); |
| 526 | } catch (IOException e) { } |
| 527 | final ParcelFileDescriptor fd = Objects.requireNonNull(dup); |
| 528 | // to prevent deadlock, read and write must happen on separate threads |
| 529 | Handler.getMain().post(() -> { |
| 530 | try (InputStream input = new ParcelFileDescriptor.AutoCloseInputStream(fd)) { |
| 531 | PersistableBundle.readFromStream(input); |
| 532 | } catch (IOException e) { } |
| 533 | }); |
Max Dashouk | 6987e9c | 2021-09-20 13:19:30 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | @Override |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 537 | public IBinder asBinder() { |
| 538 | return null; |
| 539 | } |
| 540 | |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 541 | /** Mocks script temporary completion. */ |
Max Dashouk | dafdd22 | 2021-08-20 10:42:25 -0700 | [diff] [blame] | 542 | public void notifyScriptSuccess(PersistableBundle bundle) { |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 543 | try { |
Rui Qiu | aba0208 | 2021-08-16 16:24:00 -0700 | [diff] [blame] | 544 | mListener.onSuccess(bundle); |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 545 | } catch (RemoteException e) { |
| 546 | // nothing to do |
| 547 | } |
| 548 | } |
| 549 | |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 550 | /** Mocks script producing final result. */ |
| 551 | public void notifyScriptFinish(PersistableBundle bundle) { |
| 552 | try { |
| 553 | mListener.onScriptFinished(bundle); |
| 554 | } catch (RemoteException e) { |
| 555 | // nothing to do |
| 556 | } |
| 557 | } |
| 558 | |
Rui Qiu | 862d6ea | 2021-08-17 15:00:09 -0700 | [diff] [blame] | 559 | /** Mocks script finished with error. */ |
| 560 | public void notifyScriptError(int errorType, String errorMessage) { |
| 561 | try { |
| 562 | mListener.onError(errorType, errorMessage, null); |
| 563 | } catch (RemoteException e) { |
| 564 | // nothing to do |
| 565 | } |
| 566 | } |
| 567 | |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 568 | /** Fails the next N invokeScript() call. */ |
| 569 | public void failNextApiCalls(int n) { |
| 570 | mFailApi = n; |
| 571 | } |
| 572 | |
Rui Qiu | f4aa2e5 | 2021-09-27 16:30:00 -0700 | [diff] [blame] | 573 | /** Returns number of times invokeScript() was called. */ |
| 574 | public int getInvokeScriptCount() { |
| 575 | return mInvokeScriptCount; |
| 576 | } |
| 577 | |
| 578 | /** Returns number of times invokeScriptForLargeInput() was called. */ |
| 579 | public int getInvokeScriptForLargeInputCount() { |
| 580 | return mInvokeScriptForLargeInputCount; |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 581 | } |
Rui Qiu | ea6a494 | 2021-09-07 15:05:34 -0700 | [diff] [blame] | 582 | |
| 583 | /** Returns the interim data passed in invokeScript(). */ |
| 584 | public PersistableBundle getSavedState() { |
| 585 | return mSavedState; |
| 586 | } |
Rui Qiu | a86ab60 | 2021-07-02 10:40:36 -0700 | [diff] [blame] | 587 | } |
Rui Qiu | 337cb24 | 2021-06-03 11:26:16 -0700 | [diff] [blame] | 588 | } |