Merge "Always wait for interface to be up in EthernetManagerTest" am: adf8f72856

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/2062850

Change-Id: I4d2ad2c91da0c25ceeaba4a3221348caf244f0b2
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Patrick Rohr
2022-05-20 12:50:08 +00:00
committed by Automerger Merge Worker

View File

@@ -53,10 +53,9 @@ import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import java.net.Inet6Address import java.net.Inet6Address
import java.net.NetworkInterface
import java.util.concurrent.Executor
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertFalse import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull import kotlin.test.assertNull
import kotlin.test.assertTrue import kotlin.test.assertTrue
import kotlin.test.fail import kotlin.test.fail
@@ -76,6 +75,7 @@ class EthernetManagerTest {
private val context by lazy { InstrumentationRegistry.getInstrumentation().context } private val context by lazy { InstrumentationRegistry.getInstrumentation().context }
private val em by lazy { EthernetManagerShimImpl.newInstance(context) } private val em by lazy { EthernetManagerShimImpl.newInstance(context) }
private val ifaceListener = EthernetStateListener()
private val createdIfaces = ArrayList<EthernetTestInterface>() private val createdIfaces = ArrayList<EthernetTestInterface>()
private val addedListeners = ArrayList<EthernetStateListener>() private val addedListeners = ArrayList<EthernetStateListener>()
@@ -154,6 +154,12 @@ class EthernetManagerTest {
return events.poll(TIMEOUT_MS) ?: fail("Did not receive callback after ${TIMEOUT_MS}ms") return events.poll(TIMEOUT_MS) ?: fail("Did not receive callback after ${TIMEOUT_MS}ms")
} }
fun eventuallyExpect(expected: CallbackEntry) = events.poll(TIMEOUT_MS) { it == expected }
fun eventuallyExpect(iface: EthernetTestInterface, state: Int, role: Int) {
assertNotNull(eventuallyExpect(createChangeEvent(iface, state, role)))
}
fun assertNoCallback() { fun assertNoCallback() {
val cb = events.poll(NO_CALLBACK_TIMEOUT_MS) val cb = events.poll(NO_CALLBACK_TIMEOUT_MS)
assertNull(cb, "Expected no callback but got $cb") assertNull(cb, "Expected no callback but got $cb")
@@ -163,6 +169,7 @@ class EthernetManagerTest {
@Before @Before
fun setUp() { fun setUp() {
setIncludeTestInterfaces(true) setIncludeTestInterfaces(true)
addInterfaceStateListener(ifaceListener)
} }
@After @After
@@ -176,18 +183,20 @@ class EthernetManagerTest {
} }
} }
private fun addInterfaceStateListener(executor: Executor, listener: EthernetStateListener) { private fun addInterfaceStateListener(listener: EthernetStateListener) {
runAsShell(CONNECTIVITY_USE_RESTRICTED_NETWORKS) { runAsShell(CONNECTIVITY_USE_RESTRICTED_NETWORKS) {
em.addInterfaceStateListener(executor, listener) em.addInterfaceStateListener(HandlerExecutor(Handler(Looper.getMainLooper())), listener)
} }
addedListeners.add(listener) addedListeners.add(listener)
} }
private fun createInterface(): EthernetTestInterface { private fun createInterface(): EthernetTestInterface {
return EthernetTestInterface( val iface = EthernetTestInterface(
context, context,
Handler(Looper.getMainLooper()) Handler(Looper.getMainLooper())
).also { createdIfaces.add(it) } ).also { createdIfaces.add(it) }
ifaceListener.eventuallyExpect(iface, STATE_LINK_UP, ROLE_CLIENT)
return iface
} }
private fun setIncludeTestInterfaces(value: Boolean) { private fun setIncludeTestInterfaces(value: Boolean) {
@@ -203,12 +212,10 @@ class EthernetManagerTest {
@Test @Test
fun testCallbacks() { fun testCallbacks() {
val executor = HandlerExecutor(Handler(Looper.getMainLooper()))
// If an interface exists when the callback is registered, it is reported on registration. // If an interface exists when the callback is registered, it is reported on registration.
val iface = createInterface() val iface = createInterface()
val listener1 = EthernetStateListener() val listener1 = EthernetStateListener()
addInterfaceStateListener(executor, listener1) addInterfaceStateListener(listener1)
validateListenerOnRegistration(listener1) validateListenerOnRegistration(listener1)
// If an interface appears, existing callbacks see it. // If an interface appears, existing callbacks see it.
@@ -221,18 +228,18 @@ class EthernetManagerTest {
// Register a new listener, it should see state of all existing interfaces immediately. // Register a new listener, it should see state of all existing interfaces immediately.
val listener2 = EthernetStateListener() val listener2 = EthernetStateListener()
addInterfaceStateListener(executor, listener2) addInterfaceStateListener(listener2)
validateListenerOnRegistration(listener2) validateListenerOnRegistration(listener2)
// Removing interfaces first sends link down, then STATE_ABSENT/ROLE_NONE. // Removing interfaces first sends link down, then STATE_ABSENT/ROLE_NONE.
removeInterface(iface) removeInterface(iface)
for (listener in addedListeners) { for (listener in listOf(listener1, listener2)) {
listener.expectCallback(iface, STATE_LINK_DOWN, ROLE_CLIENT) listener.expectCallback(iface, STATE_LINK_DOWN, ROLE_CLIENT)
listener.expectCallback(iface, STATE_ABSENT, ROLE_NONE) listener.expectCallback(iface, STATE_ABSENT, ROLE_NONE)
} }
removeInterface(iface2) removeInterface(iface2)
for (listener in addedListeners) { for (listener in listOf(listener1, listener2)) {
listener.expectCallback(iface2, STATE_LINK_DOWN, ROLE_CLIENT) listener.expectCallback(iface2, STATE_LINK_DOWN, ROLE_CLIENT)
listener.expectCallback(iface2, STATE_ABSENT, ROLE_NONE) listener.expectCallback(iface2, STATE_ABSENT, ROLE_NONE)
listener.assertNoCallback() listener.assertNoCallback()