Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(388)

Side by Side Diff: components/cronet/android/test/javatests/src/org/chromium/net/CronetUrlRequestContextTest.java

Issue 2283243002: Allow direct executors in cronet. (Closed)
Patch Set: Improve thread checking Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.net; 5 package org.chromium.net;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 import android.content.ContextWrapper; 8 import android.content.ContextWrapper;
9 import android.os.ConditionVariable; 9 import android.os.ConditionVariable;
10 import android.os.Handler; 10 import android.os.Handler;
(...skipping 14 matching lines...) Expand all
25 import java.io.BufferedReader; 25 import java.io.BufferedReader;
26 import java.io.File; 26 import java.io.File;
27 import java.io.FileReader; 27 import java.io.FileReader;
28 import java.net.URL; 28 import java.net.URL;
29 import java.util.Arrays; 29 import java.util.Arrays;
30 import java.util.LinkedList; 30 import java.util.LinkedList;
31 import java.util.NoSuchElementException; 31 import java.util.NoSuchElementException;
32 import java.util.concurrent.Executor; 32 import java.util.concurrent.Executor;
33 import java.util.concurrent.Executors; 33 import java.util.concurrent.Executors;
34 import java.util.concurrent.ThreadFactory; 34 import java.util.concurrent.ThreadFactory;
35 import java.util.concurrent.atomic.AtomicReference;
35 36
36 /** 37 /**
37 * Test CronetEngine. 38 * Test CronetEngine.
38 */ 39 */
39 @JNINamespace("cronet") 40 @JNINamespace("cronet")
40 public class CronetUrlRequestContextTest extends CronetTestBase { 41 public class CronetUrlRequestContextTest extends CronetTestBase {
41 // URLs used for tests. 42 // URLs used for tests.
42 private static final String MOCK_CRONET_TEST_FAILED_URL = 43 private static final String MOCK_CRONET_TEST_FAILED_URL =
43 "http://mock.failed.request/-2"; 44 "http://mock.failed.request/-2";
44 private static final String MOCK_CRONET_TEST_SUCCESS_URL = 45 private static final String MOCK_CRONET_TEST_SUCCESS_URL =
(...skipping 695 matching lines...) Expand 10 before | Expand all | Expand 10 after
740 assertTrue(containsStringInNetLog(eventFile1, mUrl404)); 741 assertTrue(containsStringInNetLog(eventFile1, mUrl404));
741 assertTrue(containsStringInNetLog(eventFile1, mUrl500)); 742 assertTrue(containsStringInNetLog(eventFile1, mUrl500));
742 assertTrue(containsStringInNetLog(eventFile2, mUrl404)); 743 assertTrue(containsStringInNetLog(eventFile2, mUrl404));
743 assertTrue(containsStringInNetLog(eventFile2, mUrl500)); 744 assertTrue(containsStringInNetLog(eventFile2, mUrl500));
744 745
745 FileUtils.recursivelyDeleteFile(netLogDir1); 746 FileUtils.recursivelyDeleteFile(netLogDir1);
746 assertFalse(netLogDir1.exists()); 747 assertFalse(netLogDir1.exists());
747 FileUtils.recursivelyDeleteFile(netLogDir2); 748 FileUtils.recursivelyDeleteFile(netLogDir2);
748 assertFalse(netLogDir2.exists()); 749 assertFalse(netLogDir2.exists());
749 } 750 }
751 @SmallTest
752 @Feature({"Cronet"})
753 @OnlyRunNativeCronet
754 // Tests that if CronetEngine is shut down on the network thread, an appropr iate exception
755 // is thrown.
756 public void testShutDownEngineOnNetworkThread() throws Exception {
757 final CronetTestFramework testFramework =
758 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT TP_CACHE_DISK);
759 String url = NativeTestServer.getFileURL("/cacheable.txt");
760 // Make a request to a cacheable resource.
761 checkRequestCaching(testFramework.mCronetEngine, url, false);
762
763 final AtomicReference<Throwable> thrown = new AtomicReference<>();
764 // Shut down the server.
765 NativeTestServer.shutdownNativeTestServer();
766 class CancelUrlRequestCallback extends TestUrlRequestCallback {
767 @Override
768 public void onResponseStarted(UrlRequest request, UrlResponseInfo in fo) {
769 super.onResponseStarted(request, info);
770 request.cancel();
771 // Shut down CronetEngine immediately after request is destroyed .
772 try {
773 testFramework.mCronetEngine.shutdown();
774 } catch (Exception e) {
775 thrown.set(e);
776 }
777 }
778
779 @Override
780 public void onSucceeded(UrlRequest request, UrlResponseInfo info) {
781 // onSucceeded will not happen, because the request is canceled
782 // after sending first read and the executor is single threaded.
783 throw new RuntimeException("Unexpected");
784 }
785
786 @Override
787 public void onFailed(
788 UrlRequest request, UrlResponseInfo info, UrlRequestExceptio n error) {
789 throw new RuntimeException("Unexpected");
790 }
791 }
792 Executor directExecutor = new Executor() {
793 @Override
794 public void execute(Runnable command) {
795 command.run();
796 }
797 };
798 CancelUrlRequestCallback callback = new CancelUrlRequestCallback();
799 callback.setAllowDirectExecutor(true);
800 UrlRequest.Builder urlRequestBuilder =
801 new UrlRequest.Builder(url, callback, directExecutor, testFramew ork.mCronetEngine);
802 urlRequestBuilder.allowDirectExecutor();
803 urlRequestBuilder.build().start();
804 callback.blockForDone();
805 assertTrue(thrown.get() instanceof RuntimeException);
806 }
750 807
751 @SmallTest 808 @SmallTest
752 @Feature({"Cronet"}) 809 @Feature({"Cronet"})
753 @OnlyRunNativeCronet 810 @OnlyRunNativeCronet
754 // Tests that if CronetEngine is shut down when reading from disk cache, 811 // Tests that if CronetEngine is shut down when reading from disk cache,
755 // there isn't a crash. See crbug.com/486120. 812 // there isn't a crash. See crbug.com/486120.
756 public void testShutDownEngineWhenReadingFromDiskCache() throws Exception { 813 public void testShutDownEngineWhenReadingFromDiskCache() throws Exception {
757 final CronetTestFramework testFramework = 814 final CronetTestFramework testFramework =
758 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT TP_CACHE_DISK); 815 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT TP_CACHE_DISK);
759 String url = NativeTestServer.getFileURL("/cacheable.txt"); 816 String url = NativeTestServer.getFileURL("/cacheable.txt");
(...skipping 649 matching lines...) Expand 10 before | Expand all | Expand 10 after
1409 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 1466 TestUrlRequestCallback callback = new TestUrlRequestCallback();
1410 URL requestUrl = 1467 URL requestUrl =
1411 new URL("http", resolverTestHostname, testUrl.getPort(), testUrl .getFile()); 1468 new URL("http", resolverTestHostname, testUrl.getPort(), testUrl .getFile());
1412 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(requestUrl .toString(), 1469 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(requestUrl .toString(),
1413 callback, callback.getExecutor(), testFramework.mCronetEngine); 1470 callback, callback.getExecutor(), testFramework.mCronetEngine);
1414 urlRequestBuilder.build().start(); 1471 urlRequestBuilder.build().start();
1415 callback.blockForDone(); 1472 callback.blockForDone();
1416 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); 1473 assertEquals(200, callback.mResponseInfo.getHttpStatusCode());
1417 } 1474 }
1418 } 1475 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698