Specify which callback is unexpected in tests
Tests may be flaky due to the usage for assertNoCallback().
The method expects no any callback received. Based on the usage,
tests expect to not to receive certain callback, such as
onAvailable(). The network may update its linkproperties during
the test and trigger onLinkPropertiesChanged(). These callbacks
are ignorable in the tests. They should not fail the tests.
Replace the assertNoCallback to new assertNoCallbackThat with
callback type specified to deflake tests.
Bug: 192239030
Test: atest android.net.cts.ConnectivityManagerTest\
--iterations 20
Change-Id: I1643c1ff15215c07e174dbcb664cfac2a38d5840
diff --git a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java
index a2ad645..fb231ee 100644
--- a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java
+++ b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java
@@ -2099,6 +2099,10 @@
public void onBlockedStatusChanged(Network network, int blockedReasons) {
getHistory().add(new CallbackEntry.BlockedStatusInt(network, blockedReasons));
}
+ private void assertNoBlockedStatusCallback() {
+ super.assertNoCallbackThat(NO_CALLBACK_TIMEOUT_MS,
+ c -> c instanceof CallbackEntry.BlockedStatus);
+ }
}
private void setRequireVpnForUids(boolean requireVpn, Collection<Range<Integer>> ranges)
@@ -2135,24 +2139,24 @@
setRequireVpnForUids(true, List.of(myUidRange));
myUidCallback.expectBlockedStatusCallback(defaultNetwork, BLOCKED_REASON_LOCKDOWN_VPN);
- otherUidCallback.assertNoCallback(NO_CALLBACK_TIMEOUT_MS);
+ otherUidCallback.assertNoBlockedStatusCallback();
setRequireVpnForUids(true, List.of(myUidRange, otherUidRange));
- myUidCallback.assertNoCallback(NO_CALLBACK_TIMEOUT_MS);
+ myUidCallback.assertNoBlockedStatusCallback();
otherUidCallback.expectBlockedStatusCallback(defaultNetwork, BLOCKED_REASON_LOCKDOWN_VPN);
// setRequireVpnForUids does no deduplication or refcounting. Removing myUidRange does not
// unblock myUid because it was added to the blocked ranges twice.
setRequireVpnForUids(false, List.of(myUidRange));
- myUidCallback.assertNoCallback(NO_CALLBACK_TIMEOUT_MS);
- otherUidCallback.assertNoCallback(NO_CALLBACK_TIMEOUT_MS);
+ myUidCallback.assertNoBlockedStatusCallback();
+ otherUidCallback.assertNoBlockedStatusCallback();
setRequireVpnForUids(false, List.of(myUidRange, otherUidRange));
myUidCallback.expectBlockedStatusCallback(defaultNetwork, BLOCKED_REASON_NONE);
otherUidCallback.expectBlockedStatusCallback(defaultNetwork, BLOCKED_REASON_NONE);
- myUidCallback.assertNoCallback(NO_CALLBACK_TIMEOUT_MS);
- otherUidCallback.assertNoCallback(NO_CALLBACK_TIMEOUT_MS);
+ myUidCallback.assertNoBlockedStatusCallback();
+ otherUidCallback.assertNoBlockedStatusCallback();
}
@Test
@@ -2637,8 +2641,9 @@
// Default network should be updated to validated cellular network.
defaultCb.eventuallyExpect(CallbackEntry.AVAILABLE, NETWORK_CALLBACK_TIMEOUT_MS,
entry -> cellNetwork.equals(entry.getNetwork()));
- // No update on wifi callback.
- wifiCb.assertNoCallback();
+ // No callback except LinkPropertiesChanged which may be triggered randomly from network
+ wifiCb.assertNoCallbackThat(NO_CALLBACK_TIMEOUT_MS,
+ c -> !(c instanceof CallbackEntry.LinkPropertiesChanged));
} finally {
mCm.unregisterNetworkCallback(wifiCb);
mCm.unregisterNetworkCallback(defaultCb);