Merge changes Ic20ea5a4,I1b32d9e0 into main

* changes:
  Wait in loop
  Read all response to generate enough traffic while testing
This commit is contained in:
Junyu Lai
2023-07-11 07:07:29 +00:00
committed by Gerrit Code Review

View File

@@ -82,9 +82,9 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.UnknownHostException; import java.net.UnknownHostException;
@@ -220,7 +220,7 @@ public class NetworkStatsManagerTest {
} else { } else {
Log.w(LOG_TAG, "Network: " + networkInfo.toString()); Log.w(LOG_TAG, "Network: " + networkInfo.toString());
} }
InputStreamReader in = null; BufferedInputStream in = null;
HttpURLConnection urlc = null; HttpURLConnection urlc = null;
String originalKeepAlive = System.getProperty("http.keepAlive"); String originalKeepAlive = System.getProperty("http.keepAlive");
System.setProperty("http.keepAlive", "false"); System.setProperty("http.keepAlive", "false");
@@ -236,10 +236,10 @@ public class NetworkStatsManagerTest {
urlc.connect(); urlc.connect();
boolean ping = urlc.getResponseCode() == 200; boolean ping = urlc.getResponseCode() == 200;
if (ping) { if (ping) {
in = new InputStreamReader((InputStream) urlc.getContent()); in = new BufferedInputStream((InputStream) urlc.getContent());
// Since the test doesn't really care about the precise amount of data, instead while (in.read() != -1) {
// of reading all contents, just read few bytes at the beginning. // Comments to suppress lint error.
in.read(); }
} }
} catch (Exception e) { } catch (Exception e) {
Log.i(LOG_TAG, "Badness during exercising remote server: " + e); Log.i(LOG_TAG, "Badness during exercising remote server: " + e);
@@ -377,9 +377,14 @@ public class NetworkStatsManagerTest {
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.build(), callback); .build(), callback);
synchronized (this) { synchronized (this) {
try { long now = System.currentTimeMillis();
wait((int) (TIMEOUT_MILLIS * 2.4)); final long deadline = (long) (now + TIMEOUT_MILLIS * 2.4);
} catch (InterruptedException e) { while (!callback.success && now < deadline) {
try {
wait(deadline - now);
} catch (InterruptedException e) {
}
now = System.currentTimeMillis();
} }
} }
if (callback.success) { if (callback.success) {