Index: components/cronet/android/test/javatests/src/org/chromium/net/QuicTest.java |
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/QuicTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/QuicTest.java |
index 0e220e26967e58ec4471839703eafea128ac1cc0..636387419ff803ea89f77924f5d23adb24d247ae 100644 |
--- a/components/cronet/android/test/javatests/src/org/chromium/net/QuicTest.java |
+++ b/components/cronet/android/test/javatests/src/org/chromium/net/QuicTest.java |
@@ -4,8 +4,10 @@ |
package org.chromium.net; |
+import android.os.ConditionVariable; |
import android.test.suitebuilder.annotation.LargeTest; |
import android.test.suitebuilder.annotation.SmallTest; |
+import android.util.SparseIntArray; |
import org.chromium.base.Log; |
import org.chromium.base.annotations.SuppressFBWarnings; |
@@ -18,6 +20,7 @@ import java.io.FileInputStream; |
import java.io.FileNotFoundException; |
import java.io.IOException; |
import java.util.HashMap; |
+import java.util.concurrent.Executors; |
/** |
* Tests making requests using QUIC. |
@@ -35,7 +38,8 @@ public class QuicTest extends CronetTestBase { |
QuicTestServer.startQuicTestServer(getContext()); |
mBuilder = new CronetEngine.Builder(getContext()); |
- mBuilder.enableQUIC(true); |
+ mBuilder.enableQUIC(true).enableNetworkQualityEstimator( |
+ true, Executors.newSingleThreadExecutor()); |
mBuilder.addQuicHint(QuicTestServer.getServerHost(), QuicTestServer.getServerPort(), |
QuicTestServer.getServerPort()); |
@@ -64,6 +68,49 @@ public class QuicTest extends CronetTestBase { |
super.tearDown(); |
} |
+ static class TestNetworkQualityListener |
+ implements NetworkQualityRttListener, NetworkQualityThroughputListener { |
+ // Lock to ensure that observation counts can be updated and read by different threads. |
+ private final Object mLock = new Object(); |
bengr
2016/06/07 20:53:16
Consider renaming as mObservationLock
tbansal1
2016/06/07 21:37:17
Done.
|
+ private final ConditionVariable mWaitForThroughput; |
+ private int mThroughputObservationCount; |
+ private SparseIntArray mRttObservationCountBySource = new SparseIntArray(); |
+ |
+ TestNetworkQualityListener(ConditionVariable waitForThroughput) { |
+ mWaitForThroughput = waitForThroughput; |
+ } |
+ |
+ @Override |
+ public void onRttObservation(int rttMs, long when, int source) { |
+ synchronized (mLock) { |
+ mRttObservationCountBySource.put( |
+ source, mRttObservationCountBySource.get(source) + 1); |
+ } |
+ } |
+ |
+ @Override |
+ public void onThroughputObservation(int throughputKbps, long when, int source) { |
+ synchronized (mLock) { |
+ if (mWaitForThroughput != null) { |
+ mWaitForThroughput.open(); |
+ } |
+ mThroughputObservationCount++; |
+ } |
+ } |
+ |
+ public int rttObservationCount(int source) { |
+ synchronized (mLock) { |
+ return mRttObservationCountBySource.get(source); |
+ } |
+ } |
+ |
+ public int throughputObservationCount() { |
+ synchronized (mLock) { |
+ return mThroughputObservationCount; |
+ } |
+ } |
+ } |
+ |
@SmallTest |
@Feature({"Cronet"}) |
@SuppressWarnings("deprecation") |
@@ -170,4 +217,52 @@ public class QuicTest extends CronetTestBase { |
fileInputStream.close(); |
return new String(data, "UTF-8").contains(content); |
} |
+ |
+ @LargeTest |
+ @Feature({"Cronet"}) |
+ @OnlyRunNativeCronet |
+ @SuppressWarnings("deprecation") |
+ public void testRealTimeNetworkQualityObservationsWithQuic() throws Exception { |
+ mTestFramework = startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mBuilder); |
+ registerHostResolver(mTestFramework); |
+ String quicURL = QuicTestServer.getServerURL() + "/simple.txt"; |
bengr
2016/06/07 20:53:16
quicUrl? Shouldn't getServerURL be getServerUrl? I
tbansal1
2016/06/07 21:37:17
May be, may be not :) I am not sure about Java sty
|
+ |
+ ConditionVariable waitForThroughput = new ConditionVariable(); |
+ TestNetworkQualityListener networkQualityListener = |
+ new TestNetworkQualityListener(waitForThroughput); |
+ mTestFramework.mCronetEngine.configureNetworkQualityEstimatorForTesting(true, true); |
+ mTestFramework.mCronetEngine.addRttListener(networkQualityListener); |
+ mTestFramework.mCronetEngine.addThroughputListener(networkQualityListener); |
+ TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
+ |
+ // Although the native stack races QUIC and SPDY for the first request, |
+ // since there is no http server running on the corresponding TCP port, |
+ // QUIC will always succeed with a 200 (see |
+ // net::HttpStreamFactoryImpl::Request::OnStreamFailed). |
+ UrlRequest.Builder requestBuilder = new UrlRequest.Builder( |
+ quicURL, callback, callback.getExecutor(), mTestFramework.mCronetEngine); |
+ requestBuilder.build().start(); |
+ callback.blockForDone(); |
+ |
+ assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
+ String expectedContent = "This is a simple text file served by QUIC.\n"; |
+ assertEquals(expectedContent, callback.mResponseAsString); |
+ assertEquals("quic/1+spdy/3", callback.mResponseInfo.getNegotiatedProtocol()); |
+ |
+ // Throughput observation is posted to the network quality estimator on the network thread |
+ // after the UrlRequest is completed. The observations are then eventually posted to |
+ // throughput listeners on the executor provided to network quality. |
+ waitForThroughput.block(); |
+ assertTrue(networkQualityListener.throughputObservationCount() > 0); |
+ |
+ // Check RTT observation count after throughput observation has been received. This ensures |
+ // that executor has finished posting the RTT observation to the RTT listeners. |
+ // NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST |
+ assertTrue(networkQualityListener.rttObservationCount(0) > 0); |
+ |
+ // NETWORK_QUALITY_OBSERVATION_SOURCE_QUIC |
+ assertTrue(networkQualityListener.rttObservationCount(2) > 0); |
+ |
+ mTestFramework.mCronetEngine.shutdown(); |
+ } |
} |