De-flake RavenwoodServicesTest
Flag: EXEMPT host test change only
Fix: 376755462
Test: atest RavenwoodServicesTest
Change-Id: I28096aee40a9f6d8c23eea8c11a59f3fe8c5b121
Merged-in: I28096aee40a9f6d8c23eea8c11a59f3fe8c5b121
diff --git a/ravenwood/tests/services-test/test/com/android/ravenwoodtest/servicestest/RavenwoodServicesTest.java b/ravenwood/tests/services-test/test/com/android/ravenwoodtest/servicestest/RavenwoodServicesTest.java
index 8ce15f0..52bf92e 100644
--- a/ravenwood/tests/services-test/test/com/android/ravenwoodtest/servicestest/RavenwoodServicesTest.java
+++ b/ravenwood/tests/services-test/test/com/android/ravenwoodtest/servicestest/RavenwoodServicesTest.java
@@ -19,18 +19,23 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
import android.content.Context;
import android.hardware.SerialManager;
import android.hardware.SerialManagerInternal;
import android.platform.test.annotations.DisabledOnRavenwood;
-import android.platform.test.ravenwood.RavenwoodRule;
+import android.platform.test.ravenwood.RavenwoodConfig;
+import android.platform.test.ravenwood.RavenwoodConfig.Config;
import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.platform.app.InstrumentationRegistry;
import com.android.server.LocalServices;
-import org.junit.Rule;
+import com.google.common.collect.Lists;
+
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -38,18 +43,25 @@
public class RavenwoodServicesTest {
private static final String TEST_VIRTUAL_PORT = "virtual:example";
- @Rule
- public final RavenwoodRule mRavenwood = new RavenwoodRule.Builder()
+ @Config
+ public static final RavenwoodConfig sRavenwood = new RavenwoodConfig.Builder()
.setProcessSystem()
.setServicesRequired(SerialManager.class)
.build();
+ private Context mContext;
+
+ @Before
+ public void setUp() {
+ mContext = InstrumentationRegistry.getInstrumentation().getContext();
+ }
+
@Test
public void testDefined() {
final SerialManager fromName = (SerialManager)
- mRavenwood.getContext().getSystemService(Context.SERIAL_SERVICE);
+ mContext.getSystemService(Context.SERIAL_SERVICE);
final SerialManager fromClass =
- mRavenwood.getContext().getSystemService(SerialManager.class);
+ mContext.getSystemService(SerialManager.class);
assertNotNull(fromName);
assertNotNull(fromClass);
assertEquals(fromName, fromClass);
@@ -63,9 +75,9 @@
// Verify that we can obtain a manager, and talk to the backend service, and that no
// serial ports are configured by default
final SerialManager service = (SerialManager)
- mRavenwood.getContext().getSystemService(Context.SERIAL_SERVICE);
+ mContext.getSystemService(Context.SERIAL_SERVICE);
final String[] ports = service.getSerialPorts();
- final String[] refPorts = mRavenwood.getContext().getResources().getStringArray(
+ final String[] refPorts = mContext.getResources().getStringArray(
com.android.internal.R.array.config_serialPorts);
assertArrayEquals(refPorts, ports);
}
@@ -73,7 +85,7 @@
@Test
public void testDriven() {
final SerialManager service = (SerialManager)
- mRavenwood.getContext().getSystemService(Context.SERIAL_SERVICE);
+ mContext.getSystemService(Context.SERIAL_SERVICE);
final SerialManagerInternal internal = LocalServices.getService(
SerialManagerInternal.class);
@@ -81,8 +93,17 @@
throw new UnsupportedOperationException(
"Needs socketpair() to offer accurate emulation");
});
- final String[] ports = service.getSerialPorts();
- assertEquals(1, ports.length);
- assertEquals(TEST_VIRTUAL_PORT, ports[0]);
+ try {
+ final String[] ports = service.getSerialPorts();
+ for (var port : ports) {
+ if (TEST_VIRTUAL_PORT.equals(port)) {
+ return; // Pass
+ }
+ }
+ fail("Virtual port " + TEST_VIRTUAL_PORT + " not found. Actual="
+ + Lists.newArrayList(ports));
+ } finally {
+ internal.removeVirtualSerialPortForTest(TEST_VIRTUAL_PORT);
+ }
}
}