| Index: components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/CronetUrlRequestContextTest.java
|
| diff --git a/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/CronetUrlRequestContextTest.java b/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/CronetUrlRequestContextTest.java
|
| index 9908acec0b3619f70f0a93570bbc09f4ebddbcf4..a2317c67fab3125c99ebb96f908b57f5e34aa144 100644
|
| --- a/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/CronetUrlRequestContextTest.java
|
| +++ b/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/CronetUrlRequestContextTest.java
|
| @@ -4,6 +4,9 @@
|
|
|
| package org.chromium.cronet_test_apk;
|
|
|
| +import android.os.ConditionVariable;
|
| +import android.os.Handler;
|
| +import android.os.Looper;
|
| import android.test.suitebuilder.annotation.SmallTest;
|
|
|
| import org.chromium.base.PathUtils;
|
| @@ -13,6 +16,7 @@ import org.chromium.cronet_test_apk.TestUrlRequestListener.ResponseStep;
|
| import org.chromium.net.ExtendedResponseInfo;
|
| import org.chromium.net.ResponseInfo;
|
| import org.chromium.net.UrlRequest;
|
| +import org.chromium.net.UrlRequestContext;
|
| import org.chromium.net.UrlRequestContextConfig;
|
| import org.chromium.net.UrlRequestException;
|
|
|
| @@ -26,6 +30,7 @@ import java.util.Arrays;
|
| public class CronetUrlRequestContextTest extends CronetTestBase {
|
| // URLs used for tests.
|
| private static final String TEST_URL = "http://127.0.0.1:8000";
|
| + private static final String URL_404 = "http://127.0.0.1:8000/notfound404";
|
| private static final String MOCK_CRONET_TEST_FAILED_URL =
|
| "http://mock.failed.request/-2";
|
| private static final String MOCK_CRONET_TEST_SUCCESS_URL =
|
| @@ -33,6 +38,28 @@ public class CronetUrlRequestContextTest extends CronetTestBase {
|
|
|
| CronetTestActivity mActivity;
|
|
|
| + static class RequestThread extends Thread {
|
| + public TestUrlRequestListener mListener;
|
| +
|
| + final CronetTestActivity mActivity;
|
| + final String mUrl;
|
| +
|
| + public RequestThread(CronetTestActivity activity, String url) {
|
| + mActivity = activity;
|
| + mUrl = url;
|
| + }
|
| +
|
| + @Override
|
| + public void run() {
|
| + UrlRequestContext requestContext = mActivity.initRequestContext();
|
| + mListener = new TestUrlRequestListener();
|
| + UrlRequest urlRequest =
|
| + requestContext.createRequest(mUrl, mListener, mListener.getExecutor());
|
| + urlRequest.start();
|
| + mListener.blockForDone();
|
| + }
|
| + }
|
| +
|
| /**
|
| * Listener that shutdowns the request context when request has succeeded
|
| * or failed.
|
| @@ -119,6 +146,69 @@ public class CronetUrlRequestContextTest extends CronetTestBase {
|
|
|
| @SmallTest
|
| @Feature({"Cronet"})
|
| + public void testShutdownDuringInit() throws Exception {
|
| + final CronetTestActivity activity = skipFactoryInitInOnCreate();
|
| + final ConditionVariable block = new ConditionVariable(false);
|
| +
|
| + // Post a task to UI thread to block until shutdown is called to test
|
| + // scenario when shutdown is called right after construction before
|
| + // context is fully initialized on the main thread.
|
| + Runnable blockingTask = new Runnable() {
|
| + public void run() {
|
| + try {
|
| + block.block();
|
| + } catch (Exception e) {
|
| + fail("Caught " + e.getMessage());
|
| + }
|
| + }
|
| + };
|
| + new Handler(Looper.getMainLooper()).post(blockingTask);
|
| +
|
| + // Create new request context, but its initialization on the UI thread
|
| + // will be stuck behind blockingTask.
|
| + final UrlRequestContext requestContext = activity.initRequestContext();
|
| + // Unblock the UI thread, so context gets initialized and shutdown on
|
| + // it.
|
| + block.open();
|
| + // Shutdown will wait for init to complete on UI thread.
|
| + requestContext.shutdown();
|
| + // Verify that context is shutdown.
|
| + try {
|
| + requestContext.stopNetLog();
|
| + fail("Should throw an exception.");
|
| + } catch (Exception e) {
|
| + assertEquals("Context is shutdown.", e.getMessage());
|
| + }
|
| + }
|
| +
|
| + @SmallTest
|
| + @Feature({"Cronet"})
|
| + public void testMultipleShutdown() throws Exception {
|
| + mActivity = launchCronetTestApp();
|
| + TestUrlRequestListener listener = new ShutdownTestUrlRequestListener();
|
| + // Block listener when response starts to verify that shutdown fails
|
| + // if there are active requests.
|
| + listener.setFailure(FailureType.BLOCK,
|
| + ResponseStep.ON_RESPONSE_STARTED);
|
| + UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest(
|
| + TEST_URL, listener, listener.getExecutor());
|
| + urlRequest.start();
|
| + listener.openBlockedStep();
|
| + listener.blockForDone();
|
| + try {
|
| + mActivity.mUrlRequestContext.shutdown();
|
| + mActivity.mUrlRequestContext.shutdown();
|
| + mActivity.mUrlRequestContext.shutdown();
|
| + mActivity.mUrlRequestContext.shutdown();
|
| + fail("Should throw an exception");
|
| + } catch (Exception e) {
|
| + assertEquals("Context is already shutdown.",
|
| + e.getMessage());
|
| + }
|
| + }
|
| +
|
| + @SmallTest
|
| + @Feature({"Cronet"})
|
| public void testShutdownAfterError() throws Exception {
|
| mActivity = launchCronetTestApp();
|
| TestUrlRequestListener listener = new ShutdownTestUrlRequestListener();
|
| @@ -218,10 +308,12 @@ public class CronetUrlRequestContextTest extends CronetTestBase {
|
| File directory = new File(PathUtils.getDataDirectory(
|
| getInstrumentation().getTargetContext()));
|
| File file = File.createTempFile("cronet", "json", directory);
|
| - mActivity.mUrlRequestContext.startNetLogToFile(file.getPath());
|
| - mActivity.mUrlRequestContext.stopNetLog();
|
| - assertTrue(file.exists());
|
| - assertTrue(file.length() == 0);
|
| + try {
|
| + mActivity.mUrlRequestContext.startNetLogToFile(file.getPath());
|
| + fail("Should throw an exception.");
|
| + } catch (Exception e) {
|
| + assertEquals("Context is shutdown.", e.getMessage());
|
| + }
|
| assertTrue(file.delete());
|
| assertTrue(!file.exists());
|
| }
|
| @@ -303,7 +395,6 @@ public class CronetUrlRequestContextTest extends CronetTestBase {
|
| urlRequest.start();
|
| listener.blockForDone();
|
| assertEquals(expectCached, listener.mResponseInfo.wasCached());
|
| - assertEquals(200, listener.mResponseInfo.getHttpStatusCode());
|
| }
|
|
|
| @SmallTest
|
| @@ -350,7 +441,7 @@ public class CronetUrlRequestContextTest extends CronetTestBase {
|
|
|
| // TODO(mef): Simple cache uses global thread pool that is not affected by
|
| // shutdown of UrlRequestContext. This test can be flaky unless that thread
|
| - // pool is destroyed and recreated. Enable the test when crbug.com/442321 is fixed.
|
| + // pool is shutdown and recreated. Enable the test when crbug.com/442321 is fixed.
|
| @SmallTest
|
| @Feature({"Cronet"})
|
| public void disabled_testEnableHttpCacheDiskNewContext() throws Exception {
|
| @@ -377,4 +468,72 @@ public class CronetUrlRequestContextTest extends CronetTestBase {
|
| config);
|
| checkRequestCaching(url, true);
|
| }
|
| +
|
| + @SmallTest
|
| + @Feature({"Cronet"})
|
| + public void testInitContextAndStartRequest() {
|
| + CronetTestActivity activity = skipFactoryInitInOnCreate();
|
| +
|
| + // Immediately make a request after initializing the context.
|
| + UrlRequestContext requestContext = activity.initRequestContext();
|
| + TestUrlRequestListener listener = new TestUrlRequestListener();
|
| + UrlRequest urlRequest =
|
| + requestContext.createRequest(TEST_URL, listener, listener.getExecutor());
|
| + urlRequest.start();
|
| + listener.blockForDone();
|
| + assertEquals(200, listener.mResponseInfo.getHttpStatusCode());
|
| + }
|
| +
|
| + @SmallTest
|
| + @Feature({"Cronet"})
|
| + public void testInitContextStartTwoRequests() throws Exception {
|
| + CronetTestActivity activity = skipFactoryInitInOnCreate();
|
| +
|
| + // Make two requests after initializing the context.
|
| + UrlRequestContext requestContext = activity.initRequestContext();
|
| + int[] statusCodes = {0, 0};
|
| + String[] urls = {TEST_URL, URL_404};
|
| + for (int i = 0; i < 2; i++) {
|
| + TestUrlRequestListener listener = new TestUrlRequestListener();
|
| + UrlRequest urlRequest =
|
| + requestContext.createRequest(urls[i], listener, listener.getExecutor());
|
| + urlRequest.start();
|
| + listener.blockForDone();
|
| + statusCodes[i] = listener.mResponseInfo.getHttpStatusCode();
|
| + }
|
| + assertEquals(200, statusCodes[0]);
|
| + assertEquals(404, statusCodes[1]);
|
| + }
|
| +
|
| + @SmallTest
|
| + @Feature({"Cronet"})
|
| + public void testInitTwoContextsSimultaneously() throws Exception {
|
| + final CronetTestActivity activity = skipFactoryInitInOnCreate();
|
| +
|
| + RequestThread thread1 = new RequestThread(activity, TEST_URL);
|
| + RequestThread thread2 = new RequestThread(activity, URL_404);
|
| +
|
| + thread1.start();
|
| + thread2.start();
|
| + thread1.join();
|
| + thread2.join();
|
| + assertEquals(200, thread1.mListener.mResponseInfo.getHttpStatusCode());
|
| + assertEquals(404, thread2.mListener.mResponseInfo.getHttpStatusCode());
|
| + }
|
| +
|
| + @SmallTest
|
| + @Feature({"Cronet"})
|
| + public void testInitTwoContextsInSequence() throws Exception {
|
| + final CronetTestActivity activity = skipFactoryInitInOnCreate();
|
| +
|
| + RequestThread thread1 = new RequestThread(activity, TEST_URL);
|
| + RequestThread thread2 = new RequestThread(activity, URL_404);
|
| +
|
| + thread1.start();
|
| + thread1.join();
|
| + thread2.start();
|
| + thread2.join();
|
| + assertEquals(200, thread1.mListener.mResponseInfo.getHttpStatusCode());
|
| + assertEquals(404, thread2.mListener.mResponseInfo.getHttpStatusCode());
|
| + }
|
| }
|
|
|