From 19a0339971db2560a5c71cac4afd0579e6cb8d23 Mon Sep 17 00:00:00 2001 From: Paul Hu Date: Fri, 15 Apr 2022 16:23:15 +0800 Subject: [PATCH] Fix flaky test: testSetOemNetworkPreferenceForTestPref - waitForAvailable is checking the NetworkCapabilities that has target transport. But calling getNetworkCapabilities in available callback is not correct because it should not use sync call in async call. Instead, it should wait for capabilities callback to check the transport. - The other callbacks (LP or NC changes) would receive before LOST callback. Thus, use eventuallyExpect to replace expectCallback. Bug: 264769746 Test: atest android.net.cts.ConnectivityManagerTest --iteration Change-Id: Iaad7a74717c81f3e6236d21981f8a5449b5a6f25 --- .../net/cts/ConnectivityManagerTest.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java index 774176f439..48ec9c60eb 100644 --- a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java +++ b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java @@ -2176,15 +2176,12 @@ public class ConnectivityManagerTest { c -> c instanceof CallbackEntry.Available); } - private void waitForAvailable( + private void waitForTransport( @NonNull final TestableNetworkCallback cb, final int expectedTransport) { - cb.eventuallyExpect( - CallbackEntry.AVAILABLE, NETWORK_CALLBACK_TIMEOUT_MS, - entry -> { - final NetworkCapabilities nc = mCm.getNetworkCapabilities(entry.getNetwork()); - return nc.hasTransport(expectedTransport); - } - ); + cb.eventuallyExpect(CallbackEntry.NETWORK_CAPS_UPDATED, + NETWORK_CALLBACK_TIMEOUT_MS, + entry -> ((CallbackEntry.CapabilitiesChanged) entry).getCaps() + .hasTransport(expectedTransport)); } private void waitForAvailable( @@ -2672,7 +2669,8 @@ public class ConnectivityManagerTest { // Validate that an unmetered network is used over other networks. waitForAvailable(defaultCallback, wifiNetwork); - waitForAvailable(systemDefaultCallback, wifiNetwork); + systemDefaultCallback.eventuallyExpect(CallbackEntry.AVAILABLE, + NETWORK_CALLBACK_TIMEOUT_MS, cb -> wifiNetwork.equals(cb.getNetwork())); // Validate that when setting unmetered to metered, unmetered is lost and replaced by // the network with the TEST transport. Also wait for validation here, in case there @@ -2684,11 +2682,14 @@ public class ConnectivityManagerTest { // callbacks may be received. Eventually, metered Wi-Fi should be the final available // callback in any case therefore confirm its receipt before continuing to assure the // system is in the expected state. - waitForAvailable(systemDefaultCallback, TRANSPORT_WIFI); + waitForTransport(systemDefaultCallback, TRANSPORT_WIFI); }, /* cleanup */ () -> { - // Validate that removing the test network will fallback to the default network. + // Validate that removing the test network will fallback to the default network. runWithShellPermissionIdentity(tnt::teardown); - defaultCallback.expect(CallbackEntry.LOST, tnt, NETWORK_CALLBACK_TIMEOUT_MS); + // The other callbacks (LP or NC changes) would receive before LOST callback. Use + // eventuallyExpect to check callback for avoiding test flake. + defaultCallback.eventuallyExpect(CallbackEntry.LOST, NETWORK_CALLBACK_TIMEOUT_MS, + lost -> tnt.getNetwork().equals(lost.getNetwork())); waitForAvailable(defaultCallback); }, /* cleanup */ () -> { setWifiMeteredStatusAndWait(ssid, oldMeteredValue, false /* waitForValidation */);