Add flakyness check when a valid connection is expected.

BUG: 29082308

Change-Id: Iadb9a0bd7fbd307d799af7a7a5dabc0ed000bc6d
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 ba383a8..3125dfa 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 @@
     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 @@
             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 @@
                 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 @@
                 }
             }
         }
-        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 96e9d2b..0eff6ab 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 @@
             @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)