From 52723ecd91654b61a5b6ba2c5f85df6dd58cf627 Mon Sep 17 00:00:00 2001 From: Chalard Jean Date: Fri, 9 Oct 2020 04:53:54 +0000 Subject: [PATCH] Fix a way for this test to flake. While tests are hopefully cleaning up after themselves, there is no guarantee that there is a currently available default network milliseconds after any given test. Some tests need to disconnect to check something, or to change a property of the wifi network that they have to revert at the end for example. Or, a test may fail leaving the device without a default network. To make sure the state is correctly cleaned up, have tearDown make sure the device is connected to a working Internet connection before the test ends, so that the next test can be sure to find established connectivity immediately. It's possible the device needs a few hundred milliseconds to re-establish connectivity, so this patch gives a grace of up to 30 seconds (the default waiting timer for TestCallback) for connectivity to be restored at the end of any test. Bug: 161767594 and others Test: ConnectivityManagerTest Original change: https://android-review.googlesource.com/c/platform/cts/+/1436794 Change-Id: I2318a6d1a6d6ac4b142fc998f0c5efbe93b68707 (cherry picked from commit ff47147c3a881a11f03f87c75bc2a7cf68f85857) --- .../src/android/net/cts/ConnectivityManagerTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java index 3880664827..109034fd92 100644 --- a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java +++ b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java @@ -210,6 +210,16 @@ public class ConnectivityManagerTest { if (mCtsNetUtils.cellConnectAttempted()) { mCtsNetUtils.disconnectFromCell(); } + + // All tests in this class require a working Internet connection as they start. Make + // sure there is still one as they end that's ready to use for the next test to use. + final TestNetworkCallback callback = new TestNetworkCallback(); + mCm.registerDefaultNetworkCallback(callback); + try { + assertNotNull("Couldn't restore Internet connectivity", callback.waitForAvailable()); + } finally { + mCm.unregisterNetworkCallback(callback); + } } /**