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