Fix timeout in CarTelemetryServiceTest
Bug: 205590991
Test: atest CarServiceUnitTest
Change-Id: Ib13e40e64f97e3c7b16d6cde902f6af8fe124ee3
Merged-In: Ib13e40e64f97e3c7b16d6cde902f6af8fe124ee3
(cherry picked from commit f89bfed4db6e82c25398b8f840669567dc13a3e4)
diff --git a/tests/carservice_unit_test/src/com/android/car/telemetry/CarTelemetryServiceTest.java b/tests/carservice_unit_test/src/com/android/car/telemetry/CarTelemetryServiceTest.java
index f6fd0db..17b74c3 100644
--- a/tests/carservice_unit_test/src/com/android/car/telemetry/CarTelemetryServiceTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/telemetry/CarTelemetryServiceTest.java
@@ -17,7 +17,6 @@
package com.android.car.telemetry;
import static com.google.common.truth.Truth.assertThat;
-import static com.google.common.truth.Truth.assertWithMessage;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
@@ -38,6 +37,7 @@
import com.android.car.CarLocalServices;
import com.android.car.CarPropertyService;
+import com.android.car.CarServiceUtils;
import com.android.car.systeminterface.SystemInterface;
import com.android.car.systeminterface.SystemStateInterface;
@@ -53,13 +53,10 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.file.Files;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
@RunWith(MockitoJUnitRunner.class)
@SmallTest
public class CarTelemetryServiceTest {
- private static final long TIMEOUT_MS = 15_000L;
private static final String METRICS_CONFIG_NAME = "my_metrics_config";
private static final MetricsConfigKey KEY_V1 = new MetricsConfigKey(METRICS_CONFIG_NAME, 1);
private static final MetricsConfigKey KEY_V2 = new MetricsConfigKey(METRICS_CONFIG_NAME, 2);
@@ -70,7 +67,6 @@
TelemetryProto.MetricsConfig.newBuilder()
.setName(METRICS_CONFIG_NAME).setVersion(2).setScript("no-op").build();
- private CountDownLatch mIdleHandlerLatch = new CountDownLatch(1);
private CarTelemetryService mService;
private File mTempSystemCarDir;
private Handler mTelemetryHandler;
@@ -115,11 +111,7 @@
mService.setListener(mMockListener);
mTelemetryHandler = mService.getTelemetryHandler();
- mTelemetryHandler.getLooper().getQueue().addIdleHandler(() -> {
- mIdleHandlerLatch.countDown();
- return true;
- });
- waitForHandlerThreadToFinish();
+ CarServiceUtils.runOnLooperSync(mTelemetryHandler.getLooper(), () -> { });
mMetricsConfigStore = mService.getMetricsConfigStore();
mResultStore = mService.getResultStore();
@@ -129,7 +121,7 @@
public void testAddMetricsConfig_newMetricsConfig_shouldSucceed() throws Exception {
mService.addMetricsConfig(KEY_V1, METRICS_CONFIG_V1.toByteArray());
- waitForHandlerThreadToFinish();
+ CarServiceUtils.runOnLooperSync(mTelemetryHandler.getLooper(), () -> { });
verify(mMockListener).onAddMetricsConfigStatus(
eq(KEY_V1), eq(CarTelemetryManager.STATUS_METRICS_CONFIG_SUCCESS));
}
@@ -137,13 +129,13 @@
@Test
public void testAddMetricsConfig_duplicateMetricsConfig_shouldFail() throws Exception {
mService.addMetricsConfig(KEY_V1, METRICS_CONFIG_V1.toByteArray());
- waitForHandlerThreadToFinish();
+ CarServiceUtils.runOnLooperSync(mTelemetryHandler.getLooper(), () -> { });
verify(mMockListener).onAddMetricsConfigStatus(
eq(KEY_V1), eq(CarTelemetryManager.STATUS_METRICS_CONFIG_SUCCESS));
mService.addMetricsConfig(KEY_V1, METRICS_CONFIG_V1.toByteArray());
- waitForHandlerThreadToFinish();
+ CarServiceUtils.runOnLooperSync(mTelemetryHandler.getLooper(), () -> { });
verify(mMockListener).onAddMetricsConfigStatus(
eq(KEY_V1), eq(CarTelemetryManager.STATUS_METRICS_CONFIG_ALREADY_EXISTS));
}
@@ -152,7 +144,7 @@
public void testAddMetricsConfig_invalidMetricsConfig_shouldFail() throws Exception {
mService.addMetricsConfig(KEY_V1, "bad config".getBytes());
- waitForHandlerThreadToFinish();
+ CarServiceUtils.runOnLooperSync(mTelemetryHandler.getLooper(), () -> { });
verify(mMockListener).onAddMetricsConfigStatus(
eq(KEY_V1), eq(CarTelemetryManager.STATUS_METRICS_CONFIG_PARSE_FAILED));
}
@@ -160,13 +152,13 @@
@Test
public void testAddMetricsConfig_olderMetricsConfig_shouldFail() throws Exception {
mService.addMetricsConfig(KEY_V2, METRICS_CONFIG_V2.toByteArray());
- waitForHandlerThreadToFinish();
+ CarServiceUtils.runOnLooperSync(mTelemetryHandler.getLooper(), () -> { });
verify(mMockListener).onAddMetricsConfigStatus(
eq(KEY_V2), eq(CarTelemetryManager.STATUS_METRICS_CONFIG_SUCCESS));
mService.addMetricsConfig(KEY_V1, METRICS_CONFIG_V1.toByteArray());
- waitForHandlerThreadToFinish();
+ CarServiceUtils.runOnLooperSync(mTelemetryHandler.getLooper(), () -> { });
verify(mMockListener).onAddMetricsConfigStatus(
eq(KEY_V1), eq(CarTelemetryManager.STATUS_METRICS_CONFIG_VERSION_TOO_OLD));
}
@@ -179,7 +171,7 @@
mService.addMetricsConfig(KEY_V2, METRICS_CONFIG_V2.toByteArray());
- waitForHandlerThreadToFinish();
+ CarServiceUtils.runOnLooperSync(mTelemetryHandler.getLooper(), () -> { });
verify(mMockListener).onAddMetricsConfigStatus(
eq(KEY_V2), eq(CarTelemetryManager.STATUS_METRICS_CONFIG_SUCCESS));
assertThat(mMetricsConfigStore.getActiveMetricsConfigs())
@@ -193,25 +185,25 @@
mService.addMetricsConfig(wrongKey, METRICS_CONFIG_V1.toByteArray());
- waitForHandlerThreadToFinish();
+ CarServiceUtils.runOnLooperSync(mTelemetryHandler.getLooper(), () -> { });
verify(mMockListener).onAddMetricsConfigStatus(
eq(wrongKey), eq(CarTelemetryManager.STATUS_METRICS_CONFIG_PARSE_FAILED));
}
@Test
- public void testRemoveMetricsConfig_shouldDeleteConfigAndResult() throws Exception {
+ public void testRemoveMetricsConfig_shouldDeleteConfigAndResult() {
mService.addMetricsConfig(KEY_V1, METRICS_CONFIG_V1.toByteArray());
mResultStore.putInterimResult(KEY_V1.getName(), new PersistableBundle());
mService.removeMetricsConfig(KEY_V1);
- waitForHandlerThreadToFinish();
+ CarServiceUtils.runOnLooperSync(mTelemetryHandler.getLooper(), () -> { });
assertThat(mMetricsConfigStore.getActiveMetricsConfigs()).isEmpty();
assertThat(mResultStore.getInterimResult(KEY_V1.getName())).isNull();
}
@Test
- public void testRemoveAllMetricsConfigs_shouldRemoveConfigsAndResults() throws Exception {
+ public void testRemoveAllMetricsConfigs_shouldRemoveConfigsAndResults() {
MetricsConfigKey key = new MetricsConfigKey("test config", 2);
TelemetryProto.MetricsConfig config =
TelemetryProto.MetricsConfig.newBuilder().setName(key.getName()).build();
@@ -222,7 +214,7 @@
mService.removeAllMetricsConfigs();
- waitForHandlerThreadToFinish();
+ CarServiceUtils.runOnLooperSync(mTelemetryHandler.getLooper(), () -> { });
assertThat(mMetricsConfigStore.getActiveMetricsConfigs()).isEmpty();
assertThat(mResultStore.getInterimResult(KEY_V1.getName())).isNull();
assertThat(mResultStore.getFinalResult(key.getName(), /* deleteResult = */ false)).isNull();
@@ -232,7 +224,7 @@
public void testSendFinishedReports_whenNoReport_shouldNotReceiveResponse() throws Exception {
mService.sendFinishedReports(KEY_V1);
- waitForHandlerThreadToFinish();
+ CarServiceUtils.runOnLooperSync(mTelemetryHandler.getLooper(), () -> { });
verify(mMockListener, never()).onResult(any(), any());
verify(mMockListener, never()).onError(any(), any());
}
@@ -245,7 +237,7 @@
mService.sendFinishedReports(KEY_V1);
- waitForHandlerThreadToFinish();
+ CarServiceUtils.runOnLooperSync(mTelemetryHandler.getLooper(), () -> { });
ByteArrayOutputStream bos = new ByteArrayOutputStream();
finalResult.writeToStream(bos);
verify(mMockListener).onResult(eq(KEY_V1), eq(bos.toByteArray()));
@@ -263,14 +255,14 @@
mService.sendFinishedReports(KEY_V1);
- waitForHandlerThreadToFinish();
+ CarServiceUtils.runOnLooperSync(mTelemetryHandler.getLooper(), () -> { });
verify(mMockListener).onError(eq(KEY_V1), eq(error.toByteArray()));
// error should have been deleted
assertThat(mResultStore.getErrorResult(KEY_V1.getName(), false)).isNull();
}
@Test
- public void testSendFinishedReports_whenListenerNotSet_shouldDoNothing() throws Exception {
+ public void testSendFinishedReports_whenListenerNotSet_shouldDoNothing() {
PersistableBundle finalResult = new PersistableBundle();
finalResult.putBoolean("finished", true);
mResultStore.putFinalResult(KEY_V1.getName(), finalResult);
@@ -278,16 +270,9 @@
mService.sendFinishedReports(KEY_V1);
- waitForHandlerThreadToFinish();
+ CarServiceUtils.runOnLooperSync(mTelemetryHandler.getLooper(), () -> { });
// if listener is null, nothing should be done, result should still be in result store
assertThat(mResultStore.getFinalResult(KEY_V1.getName(), false).toString())
.isEqualTo(finalResult.toString());
}
-
- private void waitForHandlerThreadToFinish() throws Exception {
- assertWithMessage("handler not idle in %sms", TIMEOUT_MS)
- .that(mIdleHandlerLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue();
- mIdleHandlerLatch = new CountDownLatch(1); // reset idle handler condition
- mTelemetryHandler.runWithScissors(() -> { }, TIMEOUT_MS);
- }
}