From b2d1a83adb94698886ca3b5593faeec5d07c715d Mon Sep 17 00:00:00 2001 From: Junyu Lai Date: Tue, 4 Jul 2023 17:30:52 +0800 Subject: [PATCH 1/2] Read all response to generate enough traffic while testing Currently, testUidTagStateDetails relies on the test URL return enough data (~100k) to ensure untagged traffic is not too much to cause flaky. But aosp/2575590 change it to read only first few bytes to speed up the test which would break this assumption. Thus in this change: 1. Revert partical changes of aosp/2575590 2. Use BufferedInputStream to speed up test instead, which speed up testUidTagStateDetails from 2032ms to 902ms in local trials. Test: atest CtsNetTestCases:NetworkStatsManagerTest Bug: 289112440 Change-Id: I1b32d9e069a7b10664065e138c75c6bfe7928f23 --- .../src/android/net/cts/NetworkStatsManagerTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/cts/net/src/android/net/cts/NetworkStatsManagerTest.java b/tests/cts/net/src/android/net/cts/NetworkStatsManagerTest.java index d8a0b075ee..d70f605d13 100644 --- a/tests/cts/net/src/android/net/cts/NetworkStatsManagerTest.java +++ b/tests/cts/net/src/android/net/cts/NetworkStatsManagerTest.java @@ -82,9 +82,9 @@ import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; +import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.UnknownHostException; @@ -220,7 +220,7 @@ public class NetworkStatsManagerTest { } else { Log.w(LOG_TAG, "Network: " + networkInfo.toString()); } - InputStreamReader in = null; + BufferedInputStream in = null; HttpURLConnection urlc = null; String originalKeepAlive = System.getProperty("http.keepAlive"); System.setProperty("http.keepAlive", "false"); @@ -236,10 +236,10 @@ public class NetworkStatsManagerTest { urlc.connect(); boolean ping = urlc.getResponseCode() == 200; if (ping) { - in = new InputStreamReader((InputStream) urlc.getContent()); - // Since the test doesn't really care about the precise amount of data, instead - // of reading all contents, just read few bytes at the beginning. - in.read(); + in = new BufferedInputStream((InputStream) urlc.getContent()); + while (in.read() != -1) { + // Comments to suppress lint error. + } } } catch (Exception e) { Log.i(LOG_TAG, "Badness during exercising remote server: " + e); From a22daeac98befd06447ef57ea42c64806529dd33 Mon Sep 17 00:00:00 2001 From: Junyu Lai Date: Fri, 7 Jul 2023 15:55:31 +0800 Subject: [PATCH 2/2] Wait in loop Follow-up from commit I1b32d9e069a7b10664065e138c75c6bfe7928f23, the wait() not in a loop could be errorprone if the call to wait() unblocks because of a spurious wakeup. Test: atest CtsNetTestCases:NetworkStatsManagerTest Bug: 289112440 Change-Id: Ic20ea5a42f3125668f80b13285b43146ae929cbc --- .../src/android/net/cts/NetworkStatsManagerTest.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/cts/net/src/android/net/cts/NetworkStatsManagerTest.java b/tests/cts/net/src/android/net/cts/NetworkStatsManagerTest.java index d70f605d13..5f887255ca 100644 --- a/tests/cts/net/src/android/net/cts/NetworkStatsManagerTest.java +++ b/tests/cts/net/src/android/net/cts/NetworkStatsManagerTest.java @@ -377,9 +377,14 @@ public class NetworkStatsManagerTest { .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) .build(), callback); synchronized (this) { - try { - wait((int) (TIMEOUT_MILLIS * 2.4)); - } catch (InterruptedException e) { + long now = System.currentTimeMillis(); + final long deadline = (long) (now + TIMEOUT_MILLIS * 2.4); + while (!callback.success && now < deadline) { + try { + wait(deadline - now); + } catch (InterruptedException e) { + } + now = System.currentTimeMillis(); } } if (callback.success) {