From 8f105009afb58545c0bca6bf512c5b2f03f32a5c Mon Sep 17 00:00:00 2001 From: Kangping Dong Date: Sun, 7 Apr 2024 12:40:46 +0800 Subject: [PATCH] [Thread] wait for Thread address updates on thread-wpan There are timing issues in the current tunInterface_joinedNetwork_otAddressesAddedToTunInterface test that the OT addresses (e.g. Leader ALOC) may be updated after the device attaches while the test doesn't wait for the addresses being propagated to the system_server. This commit fixes this issue by waiting for the addresses check for 1 seconds before timing out. Bug: 333078476 Change-Id: If672aab7535f200bd38b50f1ce691b7c32e4cc75 --- .../net/thread/ThreadIntegrationTest.java | 18 ++++++++++++++---- .../net/thread/utils/IntegrationTestUtils.java | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/thread/tests/integration/src/android/net/thread/ThreadIntegrationTest.java b/thread/tests/integration/src/android/net/thread/ThreadIntegrationTest.java index d29e6577d5..8f8aa5f188 100644 --- a/thread/tests/integration/src/android/net/thread/ThreadIntegrationTest.java +++ b/thread/tests/integration/src/android/net/thread/ThreadIntegrationTest.java @@ -21,6 +21,7 @@ import static android.net.thread.ThreadNetworkController.DEVICE_ROLE_LEADER; import static android.net.thread.ThreadNetworkController.DEVICE_ROLE_STOPPED; import static android.net.thread.utils.IntegrationTestUtils.CALLBACK_TIMEOUT; import static android.net.thread.utils.IntegrationTestUtils.RESTART_JOIN_TIMEOUT; +import static android.net.thread.utils.IntegrationTestUtils.waitFor; import static com.android.compatibility.common.util.SystemUtil.runShellCommand; import static com.android.compatibility.common.util.SystemUtil.runShellCommandOrThrow; @@ -65,6 +66,9 @@ public class ThreadIntegrationTest { // The byte[] buffer size for UDP tests private static final int UDP_BUFFER_SIZE = 1024; + // The maximum time for OT addresses to be propagated to the TUN interface "thread-wpan" + private static final Duration TUN_ADDR_UPDATE_TIMEOUT = Duration.ofSeconds(1); + // A valid Thread Active Operational Dataset generated from OpenThread CLI "dataset init new". private static final byte[] DEFAULT_DATASET_TLVS = base16().decode( @@ -149,16 +153,22 @@ public class ThreadIntegrationTest { assertThat(ifconfig).doesNotContain("inet6 addr"); } + // TODO (b/323300829): add test for removing an OT address @Test public void tunInterface_joinedNetwork_otAddressesAddedToTunInterface() throws Exception { mController.joinAndWait(DEFAULT_DATASET); - String ifconfig = runShellCommand("ifconfig thread-wpan"); List otAddresses = mOtCtl.getAddresses(); assertThat(otAddresses).isNotEmpty(); - for (Inet6Address otAddress : otAddresses) { - assertThat(ifconfig).contains(otAddress.getHostAddress()); - } + // TODO: it's cleaner to have a retry() method to retry failed asserts in given delay so + // that we can write assertThat() in the Predicate + waitFor( + () -> { + String ifconfig = runShellCommand("ifconfig thread-wpan"); + return otAddresses.stream() + .allMatch(addr -> ifconfig.contains(addr.getHostAddress())); + }, + TUN_ADDR_UPDATE_TIMEOUT); } @Test diff --git a/thread/tests/integration/src/android/net/thread/utils/IntegrationTestUtils.java b/thread/tests/integration/src/android/net/thread/utils/IntegrationTestUtils.java index 2237e651b2..3efdf7d829 100644 --- a/thread/tests/integration/src/android/net/thread/utils/IntegrationTestUtils.java +++ b/thread/tests/integration/src/android/net/thread/utils/IntegrationTestUtils.java @@ -85,7 +85,7 @@ public final class IntegrationTestUtils { */ public static void waitFor(Supplier condition, Duration timeout) throws TimeoutException { - final long intervalMills = 1000; + final long intervalMills = 500; final long timeoutMills = timeout.toMillis(); for (long i = 0; i < timeoutMills; i += intervalMills) {