Commit f49eca0d authored by xunjieli's avatar xunjieli Committed by Commit bot

[Cronet] Added a unit test in QuicTest to test async API.

This CL removed the incorrect comment in QuicTest, and
added a second unit test to make sure addQuicHints
works for the new async API.

It made QuicTestServer install test files during start up
to match the new behavior of NativeTestServer, and adds an
assertion to make sure the server has test data.

The CL caches the result returned by
CronetTestActivity#getContextConfig, so we don't
need to go through that path for initializing
mUrlRequestContext, mHttpUrlRequestFactory and
mStreamHandlerFactory, and cleaned up log statements.

Review URL: https://codereview.chromium.org/1138493002

Cr-Commit-Position: refs/heads/master@{#329678}
parent bd11f56b
...@@ -20,8 +20,18 @@ public class QuicTest extends CronetTestBase { ...@@ -20,8 +20,18 @@ public class QuicTest extends CronetTestBase {
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
mActivity = launchCronetTestApp(); // Load library first, since we need the Quic test server's URL.
QuicTestServer.startQuicTestServer(mActivity.getApplicationContext()); System.loadLibrary("cronet_tests");
QuicTestServer.startQuicTestServer(getInstrumentation().getTargetContext());
UrlRequestContextConfig config = new UrlRequestContextConfig();
config.enableQUIC(true);
config.addQuicHint(QuicTestServer.getServerHost(), QuicTestServer.getServerPort(),
QuicTestServer.getServerPort());
config.setExperimentalQuicConnectionOptions("PACE,IW10,FOO,DEADBEEF");
String[] commandLineArgs = {CronetTestActivity.CONFIG_KEY, config.toString(),
CronetTestActivity.CACHE_KEY, CronetTestActivity.CACHE_DISK_NO_HTTP};
mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(null, commandLineArgs);
} }
@Override @Override
...@@ -32,38 +42,44 @@ public class QuicTest extends CronetTestBase { ...@@ -32,38 +42,44 @@ public class QuicTest extends CronetTestBase {
@SmallTest @SmallTest
@Feature({"Cronet"}) @Feature({"Cronet"})
public void testQuicLoadUrl() throws Exception { public void testQuicLoadUrl_LegacyAPI() throws Exception {
HttpUrlRequestFactoryConfig config = new HttpUrlRequestFactoryConfig();
String quicURL = QuicTestServer.getServerURL() + "/simple.txt"; String quicURL = QuicTestServer.getServerURL() + "/simple.txt";
config.enableQUIC(true);
config.setLibraryName("cronet_tests");
config.addQuicHint(QuicTestServer.getServerHost(),
QuicTestServer.getServerPort(), QuicTestServer.getServerPort());
config.setExperimentalQuicConnectionOptions("PACE,IW10,FOO,DEADBEEF");
HttpUrlRequestFactory factory = HttpUrlRequestFactory.createFactory(
mActivity.getApplicationContext(), config);
HashMap<String, String> headers = new HashMap<String, String>(); HashMap<String, String> headers = new HashMap<String, String>();
TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener();
// Quic always races the first request with SPDY, but the second request // Although the native stack races QUIC and SPDY for the first request,
// should go through Quic. // since there is no http server running on the corresponding TCP port,
for (int i = 0; i < 2; i++) { // QUIC will always succeed with a 200 (see
HttpUrlRequest request = // net::HttpStreamFactoryImpl::Request::OnStreamFailed).
factory.createRequest( HttpUrlRequest request = mActivity.mRequestFactory.createRequest(
quicURL, quicURL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener);
HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, request.start();
headers, listener.blockForComplete();
listener);
request.start();
listener.blockForComplete();
if (listener.mHttpStatusCode == 200) break;
}
assertEquals(200, listener.mHttpStatusCode); assertEquals(200, listener.mHttpStatusCode);
assertEquals( assertEquals(
"This is a simple text file served by QUIC.\n", "This is a simple text file served by QUIC.\n",
listener.mResponseAsString); listener.mResponseAsString);
assertEquals("quic/1+spdy/3", listener.mNegotiatedProtocol); assertEquals("quic/1+spdy/3", listener.mNegotiatedProtocol);
} }
@SmallTest
@Feature({"Cronet"})
public void testQuicLoadUrl() throws Exception {
String quicURL = QuicTestServer.getServerURL() + "/simple.txt";
TestUrlRequestListener listener = new TestUrlRequestListener();
// 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 request = mActivity.mUrlRequestContext.createRequest(
quicURL, listener, listener.getExecutor());
request.start();
listener.blockForDone();
assertEquals(200, listener.mResponseInfo.getHttpStatusCode());
assertEquals("This is a simple text file served by QUIC.\n", listener.mResponseAsString);
assertEquals("quic/1+spdy/3", listener.mResponseInfo.getNegotiatedProtocol());
}
} }
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/android/jni_string.h" #include "base/android/jni_string.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/threading/thread.h" #include "base/threading/thread.h"
#include "jni/QuicTestServer_jni.h" #include "jni/QuicTestServer_jni.h"
#include "net/base/ip_endpoint.h" #include "net/base/ip_endpoint.h"
...@@ -30,6 +31,7 @@ void ServeFilesFromDirectory( ...@@ -30,6 +31,7 @@ void ServeFilesFromDirectory(
DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread());
DCHECK(!g_quic_server); DCHECK(!g_quic_server);
base::FilePath file_dir = directory.Append("quic_data"); base::FilePath file_dir = directory.Append("quic_data");
CHECK(base::PathExists(file_dir)) << "Quic data does not exist";
net::tools::QuicInMemoryCache::GetInstance()->InitializeFromDirectory( net::tools::QuicInMemoryCache::GetInstance()->InitializeFromDirectory(
file_dir.value()); file_dir.value());
net::IPAddressNumber ip; net::IPAddressNumber ip;
......
...@@ -8,10 +8,11 @@ import android.app.Activity; ...@@ -8,10 +8,11 @@ import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.util.Log;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.assertTrue;
import org.chromium.base.Log;
import org.chromium.base.PathUtils; import org.chromium.base.PathUtils;
import org.chromium.base.annotations.SuppressFBWarnings; import org.chromium.base.annotations.SuppressFBWarnings;
import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory; import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory;
...@@ -72,6 +73,9 @@ public class CronetTestActivity extends Activity { ...@@ -72,6 +73,9 @@ public class CronetTestActivity extends Activity {
int mHttpStatusCode = 0; int mHttpStatusCode = 0;
// UrlRequestContextConfig used for this activity.
private UrlRequestContextConfig mConfig;
class TestHttpUrlRequestListener implements HttpUrlRequestListener { class TestHttpUrlRequestListener implements HttpUrlRequestListener {
public TestHttpUrlRequestListener() { public TestHttpUrlRequestListener() {
} }
...@@ -92,6 +96,25 @@ public class CronetTestActivity extends Activity { ...@@ -92,6 +96,25 @@ public class CronetTestActivity extends Activity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
prepareTestStorage(); prepareTestStorage();
// Print out extra arguments passed in starting this activity.
Intent intent = getIntent();
Bundle extras = intent.getExtras();
Log.i(TAG, "Cronet extras: " + extras);
if (extras != null) {
String[] commandLine = extras.getStringArray(COMMAND_LINE_ARGS_KEY);
if (commandLine != null) {
assertEquals(0, commandLine.length % 2);
for (int i = 0; i < commandLine.length / 2; i++) {
Log.i(TAG, "Cronet commandLine %s = %s", commandLine[i * 2],
commandLine[i * 2 + 1]);
}
}
}
// Initializes UrlRequestContextConfig from commandLine args.
mConfig = initializeContextConfig();
Log.i(TAG, "Using Config: " + mConfig.toString());
String initString = getCommandLineArg(LIBRARY_INIT_KEY); String initString = getCommandLineArg(LIBRARY_INIT_KEY);
if (LIBRARY_INIT_SKIP.equals(initString)) { if (LIBRARY_INIT_SKIP.equals(initString)) {
return; return;
...@@ -99,7 +122,7 @@ public class CronetTestActivity extends Activity { ...@@ -99,7 +122,7 @@ public class CronetTestActivity extends Activity {
if (LIBRARY_INIT_WRAPPER.equals(initString)) { if (LIBRARY_INIT_WRAPPER.equals(initString)) {
mStreamHandlerFactory = mStreamHandlerFactory =
new CronetURLStreamHandlerFactory(this, getContextConfig()); new CronetURLStreamHandlerFactory(this, mConfig);
} }
mUrlRequestContext = initRequestContext(); mUrlRequestContext = initRequestContext();
...@@ -143,25 +166,17 @@ public class CronetTestActivity extends Activity { ...@@ -143,25 +166,17 @@ public class CronetTestActivity extends Activity {
} }
UrlRequestContextConfig getContextConfig() { UrlRequestContextConfig getContextConfig() {
UrlRequestContextConfig config = new UrlRequestContextConfig(); return mConfig;
}
String cacheString = getCommandLineArg(CACHE_KEY); private UrlRequestContextConfig initializeContextConfig() {
if (CACHE_DISK.equals(cacheString)) { UrlRequestContextConfig config = new UrlRequestContextConfig();
config.setStoragePath(getTestStorage());
config.enableHttpCache(UrlRequestContextConfig.HttpCache.DISK, 1000 * 1024);
} else if (CACHE_DISK_NO_HTTP.equals(cacheString)) {
config.setStoragePath(getTestStorage());
config.enableHttpCache(UrlRequestContextConfig.HttpCache.DISK_NO_HTTP, 1000 * 1024);
} else if (CACHE_IN_MEMORY.equals(cacheString)) {
config.enableHttpCache(UrlRequestContextConfig.HttpCache.IN_MEMORY, 100 * 1024);
}
config.enableSPDY(true).enableQUIC(true); config.enableSPDY(true).enableQUIC(true);
// Override config if it is passed from the launcher. // Override config if it is passed from the launcher.
String configString = getCommandLineArg(CONFIG_KEY); String configString = getCommandLineArg(CONFIG_KEY);
if (configString != null) { if (configString != null) {
try { try {
Log.i(TAG, "Using Config: " + configString);
config = new UrlRequestContextConfig(configString); config = new UrlRequestContextConfig(configString);
} catch (org.json.JSONException e) { } catch (org.json.JSONException e) {
Log.e(TAG, "Invalid Config.", e); Log.e(TAG, "Invalid Config.", e);
...@@ -170,6 +185,17 @@ public class CronetTestActivity extends Activity { ...@@ -170,6 +185,17 @@ public class CronetTestActivity extends Activity {
} }
} }
String cacheString = getCommandLineArg(CACHE_KEY);
if (CACHE_DISK.equals(cacheString)) {
config.setStoragePath(getTestStorage());
config.enableHttpCache(UrlRequestContextConfig.HttpCache.DISK, 1000 * 1024);
} else if (CACHE_DISK_NO_HTTP.equals(cacheString)) {
config.setStoragePath(getTestStorage());
config.enableHttpCache(UrlRequestContextConfig.HttpCache.DISK_NO_HTTP, 1000 * 1024);
} else if (CACHE_IN_MEMORY.equals(cacheString)) {
config.enableHttpCache(UrlRequestContextConfig.HttpCache.IN_MEMORY, 100 * 1024);
}
// Setting this here so it isn't overridden on the command line // Setting this here so it isn't overridden on the command line
config.setLibraryName("cronet_tests"); config.setLibraryName("cronet_tests");
return config; return config;
...@@ -177,12 +203,12 @@ public class CronetTestActivity extends Activity { ...@@ -177,12 +203,12 @@ public class CronetTestActivity extends Activity {
// Helper function to initialize request context. Also used in testing. // Helper function to initialize request context. Also used in testing.
public UrlRequestContext initRequestContext() { public UrlRequestContext initRequestContext() {
return UrlRequestContext.createContext(this, getContextConfig()); return UrlRequestContext.createContext(this, mConfig);
} }
// Helper function to initialize request factory. Also used in testing. // Helper function to initialize request factory. Also used in testing.
public HttpUrlRequestFactory initRequestFactory() { public HttpUrlRequestFactory initRequestFactory() {
return HttpUrlRequestFactory.createFactory(this, getContextConfig()); return HttpUrlRequestFactory.createFactory(this, mConfig);
} }
private static String getUrlFromIntent(Intent intent) { private static String getUrlFromIntent(Intent intent) {
...@@ -192,13 +218,10 @@ public class CronetTestActivity extends Activity { ...@@ -192,13 +218,10 @@ public class CronetTestActivity extends Activity {
private String getCommandLineArg(String key) { private String getCommandLineArg(String key) {
Intent intent = getIntent(); Intent intent = getIntent();
Bundle extras = intent.getExtras(); Bundle extras = intent.getExtras();
Log.i(TAG, "Cronet extras: " + extras);
if (extras != null) { if (extras != null) {
String[] commandLine = extras.getStringArray(COMMAND_LINE_ARGS_KEY); String[] commandLine = extras.getStringArray(COMMAND_LINE_ARGS_KEY);
if (commandLine != null) { if (commandLine != null) {
for (int i = 0; i < commandLine.length; ++i) { for (int i = 0; i < commandLine.length; ++i) {
Log.i(TAG,
"Cronet commandLine[" + i + "]=" + commandLine[i]);
if (commandLine[i].equals(key)) { if (commandLine[i].equals(key)) {
return commandLine[++i]; return commandLine[++i];
} }
......
...@@ -9,6 +9,7 @@ import android.os.ConditionVariable; ...@@ -9,6 +9,7 @@ import android.os.ConditionVariable;
import org.chromium.base.CalledByNative; import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace; import org.chromium.base.JNINamespace;
import org.chromium.base.Log;
/** /**
* Wrapper class to start a Quic test server. * Wrapper class to start a Quic test server.
...@@ -16,8 +17,10 @@ import org.chromium.base.JNINamespace; ...@@ -16,8 +17,10 @@ import org.chromium.base.JNINamespace;
@JNINamespace("cronet") @JNINamespace("cronet")
public final class QuicTestServer { public final class QuicTestServer {
private static final ConditionVariable sBlock = new ConditionVariable(); private static final ConditionVariable sBlock = new ConditionVariable();
private static final String TAG = Log.makeTag("QuicTestServer");
public static void startQuicTestServer(Context context) { public static void startQuicTestServer(Context context) {
TestFilesInstaller.installIfNeeded(context);
nativeStartQuicTestServer(TestFilesInstaller.getInstalledPath(context)); nativeStartQuicTestServer(TestFilesInstaller.getInstalledPath(context));
sBlock.block(); sBlock.block();
} }
...@@ -41,6 +44,7 @@ public final class QuicTestServer { ...@@ -41,6 +44,7 @@ public final class QuicTestServer {
@CalledByNative @CalledByNative
private static void onServerStarted() { private static void onServerStarted() {
Log.i(TAG, "Quic server started.");
sBlock.open(); sBlock.open();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment