From b9b85ce0cd8126e63445d13f94fd3a7732f39c83 Mon Sep 17 00:00:00 2001 From: Felipe Leme Date: Thu, 2 Jun 2016 09:08:49 -0700 Subject: [PATCH] Add flakyness check when a valid connection is expected. BUG: 29082308 Change-Id: Iadb9a0bd7fbd307d799af7a7a5dabc0ed000bc6d --- ...stractRestrictBackgroundNetworkTestCase.java | 17 ++++++++++++----- .../net/hostside/app2/MyBroadcastReceiver.java | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java index ba383a88b7..3125dfa50b 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java @@ -269,8 +269,6 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation private void assertNetworkAccess(boolean expectAvailable) throws Exception { final Intent intent = new Intent(ACTION_CHECK_NETWORK); - // When the network info state change, it's possible the app still get the previous value, - // so we need to retry a couple times. final int maxTries = 5; String resultData = null; for (int i = 1; i <= maxTries; i++) { @@ -287,8 +285,14 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation final String networkInfo = parts[4]; if (expectAvailable) { - assertTrue("should be connected: " + connectionCheckDetails - + " (network info: " + networkInfo + ")", connected); + if (!connected) { + // Since it's establishing a connection to an external site, it could be flaky. + Log.w(TAG, "Failed to connect to an external site on attempt #" + i + + " (error: " + connectionCheckDetails + ", NetworkInfo: " + networkInfo + + "); sleeping " + NETWORK_TIMEOUT_MS + "ms before trying again"); + SystemClock.sleep(NETWORK_TIMEOUT_MS); + continue; + } if (state != State.CONNECTED) { Log.d(TAG, "State (" + state + ") not set to CONNECTED on attempt #" + i + "; sleeping 1s before trying again"); @@ -303,6 +307,8 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation assertFalse("should not be connected: " + connectionCheckDetails + " (network info: " + networkInfo + ")", connected); if (state != State.DISCONNECTED) { + // When the network info state change, it's possible the app still get the + // previous value, so we need to retry a couple times. Log.d(TAG, "State (" + state + ") not set to DISCONNECTED on attempt #" + i + "; sleeping 1s before trying again"); SystemClock.sleep(SECOND_IN_MS); @@ -313,7 +319,8 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation } } } - fail("Invalid state after " + maxTries + " attempts. Last data: " + resultData); + fail("Invalid state for expectAvailable=" + expectAvailable + " after " + maxTries + + " attempts. Last data: " + resultData); } protected String executeShellCommand(String command) throws Exception { diff --git a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java index 96e9d2bab4..0eff6abdf8 100644 --- a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java +++ b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java @@ -174,7 +174,7 @@ public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void run() { // TODO: connect to a hostside server instead - final String address = "http://example.com"; + final String address = "http://google.com"; final NetworkInfo networkInfo = cm.getActiveNetworkInfo(); Log.d(TAG, "Running checkNetworkStatus() on thread " + Thread.currentThread().getName() + " for UID " + getUid(context)