From ddfb2382e0f81c4aeb93124f111a24e771e3c03a Mon Sep 17 00:00:00 2001 From: Junyu Lai Date: Tue, 2 May 2023 10:28:31 +0800 Subject: [PATCH] Fix flaky testUidTagStateDetails This includes: 1. Specify a read timeout: At present, the test only specifies a connect timeout and terminates if the timeout is exceeded. However, if the connection takes a long time to establish, there may be very little time left for the test to perform a read operation. 2. Read only the initial bytes: Since the exact amount of data is not important for the test, it is preferable to read only a small amount of data at the beginning rather than reading the entire content. Test: atest CtsNetTestCases:android.net.cts.NetworkStatsManagerTest \ --rerun-until-failure 100 Fix: 274835045 Change-Id: If53a8db7ab6c5df462757d80b563de89531c3c3f --- .../android/net/cts/NetworkStatsManagerTest.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/tests/cts/net/src/android/net/cts/NetworkStatsManagerTest.java b/tests/cts/net/src/android/net/cts/NetworkStatsManagerTest.java index f86c5cd3b4..d8a0b075ee 100644 --- a/tests/cts/net/src/android/net/cts/NetworkStatsManagerTest.java +++ b/tests/cts/net/src/android/net/cts/NetworkStatsManagerTest.java @@ -210,7 +210,6 @@ public class NetworkStatsManagerTest { private long mStartTime; private long mEndTime; - private long mBytesRead; private String mWriteSettingsMode; private String mUsageStatsMode; @@ -229,6 +228,7 @@ public class NetworkStatsManagerTest { TrafficStats.setThreadStatsTag(NETWORK_TAG); urlc = (HttpURLConnection) network.openConnection(url); urlc.setConnectTimeout(TIMEOUT_MILLIS); + urlc.setReadTimeout(TIMEOUT_MILLIS); urlc.setUseCaches(false); // Disable compression so we generate enough traffic that assertWithinPercentage will // not be affected by the small amount of traffic (5-10kB) sent by the test harness. @@ -236,11 +236,10 @@ public class NetworkStatsManagerTest { urlc.connect(); boolean ping = urlc.getResponseCode() == 200; if (ping) { - in = new InputStreamReader( - (InputStream) urlc.getContent()); - - mBytesRead = 0; - while (in.read() != -1) ++mBytesRead; + 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(); } } catch (Exception e) { Log.i(LOG_TAG, "Badness during exercising remote server: " + e); @@ -379,7 +378,7 @@ public class NetworkStatsManagerTest { .build(), callback); synchronized (this) { try { - wait((int) (TIMEOUT_MILLIS * 1.2)); + wait((int) (TIMEOUT_MILLIS * 2.4)); } catch (InterruptedException e) { } } @@ -394,7 +393,7 @@ public class NetworkStatsManagerTest { assertFalse(mNetworkInterfacesToTest[networkTypeIndex].getSystemFeature() + " is a reported system feature, " + "however no corresponding connected network interface was found or the attempt " - + "to connect has timed out (timeout = " + TIMEOUT_MILLIS + "ms)." + + "to connect and read has timed out (timeout = " + (TIMEOUT_MILLIS * 2) + "ms)." + mNetworkInterfacesToTest[networkTypeIndex].getErrorMessage(), hasFeature); return false; }