From 6c80c3806d283b30fb25396214d70daa8f2bd6d3 Mon Sep 17 00:00:00 2001 From: Chiachang Wang Date: Fri, 16 Jul 2021 15:28:09 +0800 Subject: [PATCH] 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 --- .../net/cts/ConnectivityManagerTest.java | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java index a2ad645163..fb231ee56c 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 class ConnectivityManagerTest { 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> ranges) @@ -2135,24 +2139,24 @@ public class ConnectivityManagerTest { 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 @@ public class ConnectivityManagerTest { // 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);