Merge "Add flakyness check when a valid connection is expected." into nyc-dev

am: 8ee60199fd

* commit '8ee60199fd55b1ceefbb6c98a36a3d688c892f3f':
  Add flakyness check when a valid connection is expected.

Change-Id: I5dcd03e34fc6ada0e8f49429f2a25f3cdb10e476
This commit is contained in:
Felipe Leme
2016-06-02 17:45:30 +00:00
committed by android-build-merger
2 changed files with 13 additions and 6 deletions

View File

@@ -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 {

View File

@@ -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)