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

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

Issue 1868433003: [Cronet] Leak NetLog instance used by CronetUrlRequestContextAdapter (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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 static org.chromium.base.CollectionUtil.newHashSet; 7 import static org.chromium.base.CollectionUtil.newHashSet;
8 8
9 import android.content.Context; 9 import android.content.Context;
10 import android.content.ContextWrapper; 10 import android.content.ContextWrapper;
(...skipping 27 matching lines...) Expand all
38 public class CronetUrlRequestContextTest extends CronetTestBase { 38 public class CronetUrlRequestContextTest extends CronetTestBase {
39 // URLs used for tests. 39 // URLs used for tests.
40 private static final String MOCK_CRONET_TEST_FAILED_URL = 40 private static final String MOCK_CRONET_TEST_FAILED_URL =
41 "http://mock.failed.request/-2"; 41 "http://mock.failed.request/-2";
42 private static final String MOCK_CRONET_TEST_SUCCESS_URL = 42 private static final String MOCK_CRONET_TEST_SUCCESS_URL =
43 "http://mock.http/success.txt"; 43 "http://mock.http/success.txt";
44 44
45 private EmbeddedTestServer mTestServer; 45 private EmbeddedTestServer mTestServer;
46 private String mUrl; 46 private String mUrl;
47 private String mUrl404; 47 private String mUrl404;
48 private String mUrl500;
48 CronetTestFramework mTestFramework; 49 CronetTestFramework mTestFramework;
49 50
50 @Override 51 @Override
51 protected void setUp() throws Exception { 52 protected void setUp() throws Exception {
52 super.setUp(); 53 super.setUp();
53 mTestServer = EmbeddedTestServer.createAndStartDefaultServer(getContext( )); 54 mTestServer = EmbeddedTestServer.createAndStartDefaultServer(getContext( ));
54 mUrl = mTestServer.getURL("/echo?status=200"); 55 mUrl = mTestServer.getURL("/echo?status=200");
55 mUrl404 = mTestServer.getURL("/echo?status=404"); 56 mUrl404 = mTestServer.getURL("/echo?status=404");
57 mUrl500 = mTestServer.getURL("/echo?status=500");
56 } 58 }
57 59
58 @Override 60 @Override
59 protected void tearDown() throws Exception { 61 protected void tearDown() throws Exception {
60 mTestServer.stopAndDestroyServer(); 62 mTestServer.stopAndDestroyServer();
61 super.tearDown(); 63 super.tearDown();
62 } 64 }
63 65
64 static class RequestThread extends Thread { 66 static class RequestThread extends Thread {
65 public TestUrlRequestCallback mCallback; 67 public TestUrlRequestCallback mCallback;
(...skipping 547 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 cronetEngine.stopNetLog(); 615 cronetEngine.stopNetLog();
614 assertTrue(file.exists()); 616 assertTrue(file.exists());
615 assertTrue(file.length() != 0); 617 assertTrue(file.length() != 0);
616 assertFalse(hasBytesInNetLog(file)); 618 assertFalse(hasBytesInNetLog(file));
617 assertTrue(file.delete()); 619 assertTrue(file.delete());
618 assertTrue(!file.exists()); 620 assertTrue(!file.exists());
619 } 621 }
620 622
621 @SmallTest 623 @SmallTest
622 @Feature({"Cronet"}) 624 @Feature({"Cronet"})
625 @OnlyRunNativeCronet
626 // Tests that NetLog contains events emitted by all live CronetEngines.
627 public void testNetLogContainEventsFromAllLiveEngines() throws Exception {
628 Context context = getContext();
629 File directory = new File(PathUtils.getDataDirectory(context));
630 File file1 = File.createTempFile("cronet1", "json", directory);
631 File file2 = File.createTempFile("cronet2", "json", directory);
632 CronetEngine cronetEngine1 = new CronetUrlRequestContext(
633 new CronetEngine.Builder(context).setLibraryName("cronet_tests") );
634 CronetEngine cronetEngine2 = new CronetUrlRequestContext(
635 new CronetEngine.Builder(context).setLibraryName("cronet_tests") );
636
637 cronetEngine1.startNetLogToFile(file1.getPath(), false);
638 cronetEngine2.startNetLogToFile(file2.getPath(), false);
639
640 // Warm CronetEngine and make sure both CronetUrlRequestContexts are
641 // initialized before testing the logs.
642 makeRequestAndCheckStatus(cronetEngine1, mUrl, 200);
643 makeRequestAndCheckStatus(cronetEngine2, mUrl, 200);
644
645 // Use cronetEngine1 to make a request to mUrl404.
646 makeRequestAndCheckStatus(cronetEngine1, mUrl404, 404);
647
648 // Use cronetEngine2 to make a request to mUrl500.
649 makeRequestAndCheckStatus(cronetEngine2, mUrl500, 500);
650
651 cronetEngine1.stopNetLog();
652 cronetEngine2.stopNetLog();
653 assertTrue(file1.exists());
654 assertTrue(file2.exists());
655 // Make sure both files contain the two requests made separately using
656 // different engines.
657 assertTrue(containsStringInNetLog(file1, mUrl404));
658 assertTrue(containsStringInNetLog(file1, mUrl500));
659 assertTrue(containsStringInNetLog(file2, mUrl404));
660 assertTrue(containsStringInNetLog(file2, mUrl500));
661 assertTrue(file1.delete());
662 assertTrue(file2.delete());
663 }
664
665 @SmallTest
666 @Feature({"Cronet"})
667 @OnlyRunNativeCronet
668 // Tests that if CronetEngine is shut down when reading from disk cache,
669 // there isn't a crash. See crbug.com/486120.
670 public void testShutDownEngineWhenReadingFromDiskCache() throws Exception {
671 enableCache(CronetEngine.Builder.HTTP_CACHE_DISK);
672 String url = NativeTestServer.getFileURL("/cacheable.txt");
673 // Make a request to a cacheable resource.
674 checkRequestCaching(url, false);
675
676 // Shut down the server.
677 NativeTestServer.shutdownNativeTestServer();
678 class CancelUrlRequestCallback extends TestUrlRequestCallback {
679 @Override
680 public void onResponseStarted(UrlRequest request, UrlResponseInfo in fo) {
681 super.onResponseStarted(request, info);
682 request.cancel();
683 // Shut down CronetEngine immediately after request is destroyed .
684 mTestFramework.mCronetEngine.shutdown();
685 }
686
687 @Override
688 public void onSucceeded(UrlRequest request, UrlResponseInfo info) {
689 // onSucceeded must not happen, because the request is canceled
mmenke 2016/04/07 21:37:32 nit: "must not" -> "will not", since we're more e
xunjieli 2016/04/07 21:41:20 Right. That makes sense. Done.
690 // after sending first read and the executor is single threaded.
691 throw new RuntimeException("Unexpected");
692 }
693
694 @Override
695 public void onFailed(
696 UrlRequest request, UrlResponseInfo info, UrlRequestExceptio n error) {
697 throw new RuntimeException("Unexpected");
698 }
699 }
700 CancelUrlRequestCallback callback = new CancelUrlRequestCallback();
701 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(
702 url, callback, callback.getExecutor(), mTestFramework.mCronetEng ine);
703 urlRequestBuilder.build().start();
704 callback.blockForDone();
705 assertEquals(200, callback.mResponseInfo.getHttpStatusCode());
706 assertTrue(callback.mResponseInfo.wasCached());
707 assertTrue(callback.mOnCanceledCalled);
708 }
709
710 @SmallTest
711 @Feature({"Cronet"})
712 @OnlyRunNativeCronet
623 public void testNetLogAfterShutdown() throws Exception { 713 public void testNetLogAfterShutdown() throws Exception {
624 mTestFramework = startCronetTestFramework(); 714 mTestFramework = startCronetTestFramework();
625 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 715 TestUrlRequestCallback callback = new TestUrlRequestCallback();
626 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( 716 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(
627 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); 717 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine);
628 urlRequestBuilder.build().start(); 718 urlRequestBuilder.build().start();
629 callback.blockForDone(); 719 callback.blockForDone();
630 mTestFramework.mCronetEngine.shutdown(); 720 mTestFramework.mCronetEngine.shutdown();
631 721
632 File directory = new File(PathUtils.getDataDirectory(getContext())); 722 File directory = new File(PathUtils.getDataDirectory(getContext()));
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 callback.blockForDone(); 802 callback.blockForDone();
713 cronetEngine.stopNetLog(); 803 cronetEngine.stopNetLog();
714 assertTrue(file.exists()); 804 assertTrue(file.exists());
715 assertTrue(file.length() != 0); 805 assertTrue(file.length() != 0);
716 assertTrue(hasBytesInNetLog(file)); 806 assertTrue(hasBytesInNetLog(file));
717 assertTrue(file.delete()); 807 assertTrue(file.delete());
718 assertTrue(!file.exists()); 808 assertTrue(!file.exists());
719 } 809 }
720 810
721 private boolean hasBytesInNetLog(File logFile) throws Exception { 811 private boolean hasBytesInNetLog(File logFile) throws Exception {
812 return containsStringInNetLog(logFile, "\"hex_encoded_bytes\"");
813 }
814
815 private boolean containsStringInNetLog(File logFile, String content) throws Exception {
722 BufferedReader logReader = new BufferedReader(new FileReader(logFile)); 816 BufferedReader logReader = new BufferedReader(new FileReader(logFile));
723 try { 817 try {
724 String logLine; 818 String logLine;
725 while ((logLine = logReader.readLine()) != null) { 819 while ((logLine = logReader.readLine()) != null) {
726 if (logLine.contains("\"hex_encoded_bytes\"")) { 820 if (logLine.contains(content)) {
727 return true; 821 return true;
728 } 822 }
729 } 823 }
730 return false; 824 return false;
731 } finally { 825 } finally {
732 logReader.close(); 826 logReader.close();
733 } 827 }
734 } 828 }
735 829
830 /**
831 * Helper method to make a request to {@code url}, wait for it to
832 * complete, and check that the status code is the same as {@code expectedSt atusCode}.
833 */
834 private void makeRequestAndCheckStatus(
835 CronetEngine engine, String url, int expectedStatusCode) {
836 TestUrlRequestCallback callback = new TestUrlRequestCallback();
837 UrlRequest request =
838 new UrlRequest.Builder(url, callback, callback.getExecutor(), en gine).build();
839 request.start();
840 callback.blockForDone();
841 assertEquals(expectedStatusCode, callback.mResponseInfo.getHttpStatusCod e());
842 }
843
736 private void enableCache(int cacheType) throws Exception { 844 private void enableCache(int cacheType) throws Exception {
737 String cacheTypeString = ""; 845 String cacheTypeString = "";
738 if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK) { 846 if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK) {
739 cacheTypeString = CronetTestFramework.CACHE_DISK; 847 cacheTypeString = CronetTestFramework.CACHE_DISK;
740 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP) { 848 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP) {
741 cacheTypeString = CronetTestFramework.CACHE_DISK_NO_HTTP; 849 cacheTypeString = CronetTestFramework.CACHE_DISK_NO_HTTP;
742 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_IN_MEMORY) { 850 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_IN_MEMORY) {
743 cacheTypeString = CronetTestFramework.CACHE_IN_MEMORY; 851 cacheTypeString = CronetTestFramework.CACHE_IN_MEMORY;
744 } 852 }
745 String[] commandLineArgs = {CronetTestFramework.CACHE_KEY, cacheTypeStri ng}; 853 String[] commandLineArgs = {CronetTestFramework.CACHE_KEY, cacheTypeStri ng};
746 mTestFramework = startCronetTestFrameworkWithUrlAndCommandLineArgs(null, commandLineArgs); 854 mTestFramework = startCronetTestFrameworkWithUrlAndCommandLineArgs(null, commandLineArgs);
747 assertTrue(NativeTestServer.startNativeTestServer(getContext())); 855 assertTrue(NativeTestServer.startNativeTestServer(getContext()));
748 } 856 }
749 857
750 private void checkRequestCaching(String url, boolean expectCached) { 858 private void checkRequestCaching(String url, boolean expectCached) {
751 checkRequestCaching(url, expectCached, false); 859 checkRequestCaching(url, expectCached, false);
752 } 860 }
753 861
754 private void checkRequestCaching(String url, boolean expectCached, 862 private void checkRequestCaching(String url, boolean expectCached,
755 boolean disableCache) { 863 boolean disableCache) {
756 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 864 TestUrlRequestCallback callback = new TestUrlRequestCallback();
757 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( 865 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(
758 url, callback, callback.getExecutor(), mTestFramework.mCronetEng ine); 866 url, callback, callback.getExecutor(), mTestFramework.mCronetEng ine);
759 if (disableCache) { 867 if (disableCache) {
760 urlRequestBuilder.disableCache(); 868 urlRequestBuilder.disableCache();
761 } 869 }
762 urlRequestBuilder.build().start(); 870 urlRequestBuilder.build().start();
763 callback.blockForDone(); 871 callback.blockForDone();
764 assertEquals(expectCached, callback.mResponseInfo.wasCached()); 872 assertEquals(expectCached, callback.mResponseInfo.wasCached());
873 assertEquals("this is a cacheable file\n", callback.mResponseAsString);
765 } 874 }
766 875
767 @SmallTest 876 @SmallTest
768 @Feature({"Cronet"}) 877 @Feature({"Cronet"})
769 @OnlyRunNativeCronet 878 @OnlyRunNativeCronet
770 public void testEnableHttpCacheDisabled() throws Exception { 879 public void testEnableHttpCacheDisabled() throws Exception {
771 enableCache(CronetEngine.Builder.HTTP_CACHE_DISABLED); 880 enableCache(CronetEngine.Builder.HTTP_CACHE_DISABLED);
772 String url = NativeTestServer.getFileURL("/cacheable.txt"); 881 String url = NativeTestServer.getFileURL("/cacheable.txt");
773 checkRequestCaching(url, false); 882 checkRequestCaching(url, false);
774 checkRequestCaching(url, false); 883 checkRequestCaching(url, false);
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
1013 try { 1122 try {
1014 // ensureInitialized() calls native code to check the version right after library load 1123 // ensureInitialized() calls native code to check the version right after library load
1015 // and will error with the message below if library loading was skip ped 1124 // and will error with the message below if library loading was skip ped
1016 CronetLibraryLoader.ensureInitialized(getContext(), builder); 1125 CronetLibraryLoader.ensureInitialized(getContext(), builder);
1017 fail("Native library should not be loaded"); 1126 fail("Native library should not be loaded");
1018 } catch (UnsatisfiedLinkError e) { 1127 } catch (UnsatisfiedLinkError e) {
1019 assertTrue(loader.wasCalled()); 1128 assertTrue(loader.wasCalled());
1020 } 1129 }
1021 } 1130 }
1022 } 1131 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698