Ensure wifi reconnects before waiting for metered
testGetMultipathPreference assumes that wifi will auto-reconnect after
changing its metered status, but this is not necessarily the case if the
BSSID has been blocklisted after it failed validating too many times.
Call ensureWifiConnected to force wifi to reconnect before waiting for
it to become metered.
Bug: 190913510
Test: atest ConnectivityManagerTest
Change-Id: Iafe9837b97e4d654bb0f3962d48a94c1f31bbebc
Merged-In: Iafe9837b97e4d654bb0f3962d48a94c1f31bbebc
diff --git a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java
index 9be7855..a267699 100644
--- a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java
+++ b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java
@@ -1102,10 +1102,10 @@
}
}
- private void waitForActiveNetworkMetered(final int targetTransportType,
+ private Network waitForActiveNetworkMetered(final int targetTransportType,
final boolean requestedMeteredness, final boolean useSystemDefault)
throws Exception {
- final CountDownLatch latch = new CountDownLatch(1);
+ final CompletableFuture<Network> networkFuture = new CompletableFuture<>();
final NetworkCallback networkCallback = new NetworkCallback() {
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities nc) {
@@ -1113,7 +1113,7 @@
final boolean metered = !nc.hasCapability(NET_CAPABILITY_NOT_METERED);
if (metered == requestedMeteredness) {
- latch.countDown();
+ networkFuture.complete(network);
}
}
};
@@ -1133,18 +1133,20 @@
// Changing meteredness on wifi involves reconnecting, which can take several seconds
// (involves re-associating, DHCP...).
- if (!latch.await(NETWORK_CALLBACK_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
- fail("Timed out waiting for active network metered status to change to "
- + requestedMeteredness + " ; network = " + mCm.getActiveNetwork());
- }
+ return networkFuture.get(NETWORK_CALLBACK_TIMEOUT_MS, TimeUnit.MILLISECONDS);
+ } catch (TimeoutException e) {
+ throw new AssertionError("Timed out waiting for active network metered status to "
+ + "change to " + requestedMeteredness + " ; network = "
+ + mCm.getActiveNetwork(), e);
} finally {
mCm.unregisterNetworkCallback(networkCallback);
}
}
- private void setWifiMeteredStatusAndWait(String ssid, boolean isMetered) throws Exception {
+ private Network setWifiMeteredStatusAndWait(String ssid, boolean isMetered) throws Exception {
setWifiMeteredStatus(ssid, Boolean.toString(isMetered) /* metered */);
- waitForActiveNetworkMetered(TRANSPORT_WIFI,
+ mCtsNetUtils.ensureWifiConnected();
+ return waitForActiveNetworkMetered(TRANSPORT_WIFI,
isMetered /* requestedMeteredness */,
true /* useSystemDefault */);
}
@@ -1210,8 +1212,7 @@
Integer.toString(newMeteredPreference));
// Wifi meterness changes from unmetered to metered will disconnect and reconnect since
// R.
- setWifiMeteredStatusAndWait(ssid, true);
- final Network network = mCtsNetUtils.ensureWifiConnected();
+ final Network network = setWifiMeteredStatusAndWait(ssid, true);
assertEquals(ssid, unquoteSSID(mWifiManager.getConnectionInfo().getSSID()));
assertEquals(mCm.getNetworkCapabilities(network).hasCapability(
NET_CAPABILITY_NOT_METERED), false);