diff options
author | Maciej Żenczykowski <maze@google.com> | 2019-10-10 18:58:03 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2019-10-10 18:58:03 +0000 |
commit | e6e22b4b26890dba7f3a74bf40a1b2640e25f580 (patch) | |
tree | 47ebe70d226269a4427f0b22ac517ff139f50b65 | |
parent | ddd173d98cc1d87c850dc5d35ab46d673d14bfaf (diff) | |
parent | 749440800e38fc57c046e97e123f36ead1a9c696 (diff) | |
download | cts-ndk-sysroot-r21.tar.gz |
Merge "Wait until the test host can be resolved before doing network traffic."ndk-sysroot-r21
-rw-r--r-- | tests/tests/app.usage/src/android/app/usage/cts/NetworkUsageStatsTest.java | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/tests/tests/app.usage/src/android/app/usage/cts/NetworkUsageStatsTest.java b/tests/tests/app.usage/src/android/app/usage/cts/NetworkUsageStatsTest.java index 6b03e91d338..60448206abc 100644 --- a/tests/tests/app.usage/src/android/app/usage/cts/NetworkUsageStatsTest.java +++ b/tests/tests/app.usage/src/android/app/usage/cts/NetworkUsageStatsTest.java @@ -32,6 +32,7 @@ import android.os.HandlerThread; import android.os.ParcelFileDescriptor; import android.os.Process; import android.os.RemoteException; +import android.os.SystemClock; import android.platform.test.annotations.AppModeFull; import android.telephony.TelephonyManager; import android.test.InstrumentationTestCase; @@ -45,6 +46,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; +import java.net.UnknownHostException; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Scanner; @@ -74,7 +76,8 @@ public class NetworkUsageStatsTest extends InstrumentationTestCase { private static final int TIMEOUT_MILLIS = 15000; private static final String CHECK_CONNECTIVITY_URL = "http://www.265.com/"; - private static final String CHECK_CALLBACK_URL = CHECK_CONNECTIVITY_URL; + private static final int HOST_RESOLUTION_RETRIES = 4; + private static final int HOST_RESOLUTION_INTERVAL_MS = 500; private static final int NETWORK_TAG = 0xf00d; private static final long THRESHOLD_BYTES = 2 * 1024 * 1024; // 2 MB @@ -281,11 +284,27 @@ public class NetworkUsageStatsTest extends InstrumentationTestCase { isDefault = false; } + // The test host only has IPv4. So on a dual-stack network where IPv6 connects before IPv4, + // we need to wait until IPv4 is available or the test will spuriously fail. + private void waitForHostResolution(Network network) { + for (int i = 0; i < HOST_RESOLUTION_RETRIES; i++) { + try { + network.getAllByName(mUrl.getHost()); + return; + } catch (UnknownHostException e) { + SystemClock.sleep(HOST_RESOLUTION_INTERVAL_MS); + } + } + fail(String.format("%s could not be resolved on network %s (%d attempts %dms apart)", + mUrl.getHost(), network, HOST_RESOLUTION_RETRIES, HOST_RESOLUTION_INTERVAL_MS)); + } + @Override public void onAvailable(Network network) { try { mStartTime = System.currentTimeMillis() - mTolerance; isDefault = network.equals(mCm.getActiveNetwork()); + waitForHostResolution(network); exerciseRemoteHost(network, mUrl); mEndTime = System.currentTimeMillis() + mTolerance; success = true; |