Chromium Code Reviews| OLD | NEW |
|---|---|
| 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; |
| 11 import android.os.Looper; | 11 import android.os.Looper; |
| 12 import android.os.Process; | 12 import android.os.Process; |
| 13 import android.support.test.filters.SmallTest; | 13 import android.support.test.filters.SmallTest; |
| 14 | 14 |
| 15 import org.json.JSONObject; | 15 import org.json.JSONObject; |
| 16 | 16 |
| 17 import static org.chromium.net.CronetEngine.Builder.HTTP_CACHE_IN_MEMORY; | 17 import static org.chromium.net.CronetEngine.Builder.HTTP_CACHE_IN_MEMORY; |
| 18 | 18 |
| 19 import org.chromium.base.FileUtils; | 19 import org.chromium.base.FileUtils; |
| 20 import org.chromium.base.PathUtils; | 20 import org.chromium.base.PathUtils; |
| 21 import org.chromium.base.annotations.JNINamespace; | 21 import org.chromium.base.annotations.JNINamespace; |
| 22 import org.chromium.base.test.util.Feature; | 22 import org.chromium.base.test.util.Feature; |
| 23 import org.chromium.net.TestUrlRequestCallback.ResponseStep; | 23 import org.chromium.net.TestUrlRequestCallback.ResponseStep; |
| 24 import org.chromium.net.impl.CronetEngineBase; | |
| 25 import org.chromium.net.impl.CronetEngineBuilderImpl; | 24 import org.chromium.net.impl.CronetEngineBuilderImpl; |
| 26 import org.chromium.net.impl.CronetLibraryLoader; | 25 import org.chromium.net.impl.CronetLibraryLoader; |
| 27 import org.chromium.net.impl.CronetUrlRequestContext; | 26 import org.chromium.net.impl.CronetUrlRequestContext; |
| 28 import org.chromium.net.test.EmbeddedTestServer; | 27 import org.chromium.net.test.EmbeddedTestServer; |
| 29 | 28 |
| 30 import java.io.BufferedReader; | 29 import java.io.BufferedReader; |
| 31 import java.io.File; | 30 import java.io.File; |
| 32 import java.io.FileReader; | 31 import java.io.FileReader; |
| 33 import java.net.URL; | 32 import java.net.URL; |
| 34 import java.nio.ByteBuffer; | 33 import java.nio.ByteBuffer; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 66 mUrl404 = mTestServer.getURL("/echo?status=404"); | 65 mUrl404 = mTestServer.getURL("/echo?status=404"); |
| 67 mUrl500 = mTestServer.getURL("/echo?status=500"); | 66 mUrl500 = mTestServer.getURL("/echo?status=500"); |
| 68 } | 67 } |
| 69 | 68 |
| 70 @Override | 69 @Override |
| 71 protected void tearDown() throws Exception { | 70 protected void tearDown() throws Exception { |
| 72 mTestServer.stopAndDestroyServer(); | 71 mTestServer.stopAndDestroyServer(); |
| 73 super.tearDown(); | 72 super.tearDown(); |
| 74 } | 73 } |
| 75 | 74 |
| 76 static class RequestThread extends Thread { | 75 class RequestThread extends Thread { |
| 77 public TestUrlRequestCallback mCallback; | 76 public TestUrlRequestCallback mCallback; |
| 78 | 77 |
| 79 final CronetTestFramework mTestFramework; | |
| 80 final String mUrl; | 78 final String mUrl; |
| 81 final ConditionVariable mRunBlocker; | 79 final ConditionVariable mRunBlocker; |
| 82 | 80 |
| 83 public RequestThread( | 81 public RequestThread(String url, ConditionVariable runBlocker) { |
| 84 CronetTestFramework testFramework, String url, ConditionVariable runBlocker) { | |
| 85 mTestFramework = testFramework; | |
| 86 mUrl = url; | 82 mUrl = url; |
| 87 mRunBlocker = runBlocker; | 83 mRunBlocker = runBlocker; |
| 88 } | 84 } |
| 89 | 85 |
| 90 @Override | 86 @Override |
| 91 public void run() { | 87 public void run() { |
| 92 mRunBlocker.block(); | 88 mRunBlocker.block(); |
| 93 CronetEngine cronetEngine = mTestFramework.initCronetEngine(); | 89 CronetEngine cronetEngine = new CronetEngine.Builder(getContext()).b uild(); |
| 94 mCallback = new TestUrlRequestCallback(); | 90 mCallback = new TestUrlRequestCallback(); |
| 95 UrlRequest.Builder urlRequestBuilder = | 91 UrlRequest.Builder urlRequestBuilder = |
| 96 cronetEngine.newUrlRequestBuilder(mUrl, mCallback, mCallback .getExecutor()); | 92 cronetEngine.newUrlRequestBuilder(mUrl, mCallback, mCallback .getExecutor()); |
| 97 urlRequestBuilder.build().start(); | 93 urlRequestBuilder.build().start(); |
| 98 mCallback.blockForDone(); | 94 mCallback.blockForDone(); |
| 99 } | 95 } |
| 100 } | 96 } |
| 101 | 97 |
| 102 /** | 98 /** |
| 103 * Callback that shutdowns the request context when request has succeeded | 99 * Callback that shutdowns the request context when request has succeeded |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 136 @SuppressWarnings("deprecation") | 132 @SuppressWarnings("deprecation") |
| 137 public void testConfigUserAgent() throws Exception { | 133 public void testConfigUserAgent() throws Exception { |
| 138 String userAgentName = "User-Agent"; | 134 String userAgentName = "User-Agent"; |
| 139 String userAgentValue = "User-Agent-Value"; | 135 String userAgentValue = "User-Agent-Value"; |
| 140 ExperimentalCronetEngine.Builder cronetEngineBuilder = | 136 ExperimentalCronetEngine.Builder cronetEngineBuilder = |
| 141 new ExperimentalCronetEngine.Builder(getContext()); | 137 new ExperimentalCronetEngine.Builder(getContext()); |
| 142 if (testingJavaImpl()) { | 138 if (testingJavaImpl()) { |
| 143 cronetEngineBuilder = createJavaEngineBuilder(); | 139 cronetEngineBuilder = createJavaEngineBuilder(); |
| 144 } | 140 } |
| 145 cronetEngineBuilder.setUserAgent(userAgentValue); | 141 cronetEngineBuilder.setUserAgent(userAgentValue); |
| 146 final CronetTestFramework testFramework = | 142 final CronetEngine cronetEngine = cronetEngineBuilder.build(); |
| 147 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(mUrl, cron etEngineBuilder); | |
| 148 NativeTestServer.shutdownNativeTestServer(); // startNativeTestServer re turns false if it's | 143 NativeTestServer.shutdownNativeTestServer(); // startNativeTestServer re turns false if it's |
| 149 // already running | 144 // already running |
| 150 assertTrue(NativeTestServer.startNativeTestServer(getContext())); | 145 assertTrue(NativeTestServer.startNativeTestServer(getContext())); |
| 151 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 146 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 152 UrlRequest.Builder urlRequestBuilder = testFramework.mCronetEngine.newUr lRequestBuilder( | 147 UrlRequest.Builder urlRequestBuilder = cronetEngine.newUrlRequestBuilder ( |
| 153 NativeTestServer.getEchoHeaderURL(userAgentName), callback, call back.getExecutor()); | 148 NativeTestServer.getEchoHeaderURL(userAgentName), callback, call back.getExecutor()); |
| 154 urlRequestBuilder.build().start(); | 149 urlRequestBuilder.build().start(); |
| 155 callback.blockForDone(); | 150 callback.blockForDone(); |
| 156 assertEquals(userAgentValue, callback.mResponseAsString); | 151 assertEquals(userAgentValue, callback.mResponseAsString); |
| 157 } | 152 } |
| 158 | 153 |
| 159 @SmallTest | 154 @SmallTest |
| 160 @Feature({"Cronet"}) | 155 @Feature({"Cronet"}) |
| 161 // TODO: Remove the annotation after fixing http://crbug.com/637979 & http:/ /crbug.com/637972 | 156 // TODO: Remove the annotation after fixing http://crbug.com/637979 & http:/ /crbug.com/637972 |
| 162 @OnlyRunNativeCronet | 157 @OnlyRunNativeCronet |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 206 callback.startNextRead(urlRequest); | 201 callback.startNextRead(urlRequest); |
| 207 callback.blockForDone(); | 202 callback.blockForDone(); |
| 208 callback.blockForCallbackToComplete(); | 203 callback.blockForCallbackToComplete(); |
| 209 callback.shutdownExecutor(); | 204 callback.shutdownExecutor(); |
| 210 } | 205 } |
| 211 | 206 |
| 212 @SmallTest | 207 @SmallTest |
| 213 @Feature({"Cronet"}) | 208 @Feature({"Cronet"}) |
| 214 @OnlyRunNativeCronet | 209 @OnlyRunNativeCronet |
| 215 public void testShutdownDuringInit() throws Exception { | 210 public void testShutdownDuringInit() throws Exception { |
| 216 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki pLibraryInit(); | |
| 217 final ConditionVariable block = new ConditionVariable(false); | 211 final ConditionVariable block = new ConditionVariable(false); |
| 218 | 212 |
| 219 // Post a task to main thread to block until shutdown is called to test | 213 // Post a task to main thread to block until shutdown is called to test |
| 220 // scenario when shutdown is called right after construction before | 214 // scenario when shutdown is called right after construction before |
| 221 // context is fully initialized on the main thread. | 215 // context is fully initialized on the main thread. |
| 222 Runnable blockingTask = new Runnable() { | 216 Runnable blockingTask = new Runnable() { |
| 223 @Override | 217 @Override |
| 224 public void run() { | 218 public void run() { |
| 225 try { | 219 try { |
| 226 block.block(); | 220 block.block(); |
| 227 } catch (Exception e) { | 221 } catch (Exception e) { |
| 228 fail("Caught " + e.getMessage()); | 222 fail("Caught " + e.getMessage()); |
| 229 } | 223 } |
| 230 } | 224 } |
| 231 }; | 225 }; |
| 232 // Ensure that test is not running on the main thread. | 226 // Ensure that test is not running on the main thread. |
| 233 assertTrue(Looper.getMainLooper() != Looper.myLooper()); | 227 assertTrue(Looper.getMainLooper() != Looper.myLooper()); |
| 234 new Handler(Looper.getMainLooper()).post(blockingTask); | 228 new Handler(Looper.getMainLooper()).post(blockingTask); |
| 235 | 229 |
| 236 // Create new request context, but its initialization on the main thread | 230 // Create new request context, but its initialization on the main thread |
| 237 // will be stuck behind blockingTask. | 231 // will be stuck behind blockingTask. |
| 238 final CronetUrlRequestContext cronetEngine = | 232 final CronetUrlRequestContext cronetEngine = |
| 239 (CronetUrlRequestContext) testFramework.initCronetEngine(); | 233 (CronetUrlRequestContext) new CronetEngine.Builder(getContext()) .build(); |
| 240 // Unblock the main thread, so context gets initialized and shutdown on | 234 // Unblock the main thread, so context gets initialized and shutdown on |
| 241 // it. | 235 // it. |
| 242 block.open(); | 236 block.open(); |
| 243 // Shutdown will wait for init to complete on main thread. | 237 // Shutdown will wait for init to complete on main thread. |
| 244 cronetEngine.shutdown(); | 238 cronetEngine.shutdown(); |
| 245 // Verify that context is shutdown. | 239 // Verify that context is shutdown. |
| 246 try { | 240 try { |
| 247 cronetEngine.getUrlRequestContextAdapter(); | 241 cronetEngine.getUrlRequestContextAdapter(); |
| 248 fail("Should throw an exception."); | 242 fail("Should throw an exception."); |
| 249 } catch (Exception e) { | 243 } catch (Exception e) { |
| 250 assertEquals("Engine is shut down.", e.getMessage()); | 244 assertEquals("Engine is shut down.", e.getMessage()); |
| 251 } | 245 } |
| 252 } | 246 } |
| 253 | 247 |
| 254 @SmallTest | 248 @SmallTest |
| 255 @Feature({"Cronet"}) | 249 @Feature({"Cronet"}) |
| 256 @OnlyRunNativeCronet | 250 @OnlyRunNativeCronet |
| 257 public void testInitAndShutdownOnMainThread() throws Exception { | 251 public void testInitAndShutdownOnMainThread() throws Exception { |
| 258 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki pLibraryInit(); | |
| 259 final ConditionVariable block = new ConditionVariable(false); | 252 final ConditionVariable block = new ConditionVariable(false); |
| 260 | 253 |
| 261 // Post a task to main thread to init and shutdown on the main thread. | 254 // Post a task to main thread to init and shutdown on the main thread. |
| 262 Runnable blockingTask = new Runnable() { | 255 Runnable blockingTask = new Runnable() { |
| 263 @Override | 256 @Override |
| 264 public void run() { | 257 public void run() { |
| 265 // Create new request context, loading the library. | 258 // Create new request context, loading the library. |
| 266 final CronetUrlRequestContext cronetEngine = | 259 final CronetUrlRequestContext cronetEngine = |
| 267 (CronetUrlRequestContext) testFramework.initCronetEngine (); | 260 (CronetUrlRequestContext) new CronetEngine.Builder(getCo ntext()).build(); |
| 268 // Shutdown right after init. | 261 // Shutdown right after init. |
| 269 cronetEngine.shutdown(); | 262 cronetEngine.shutdown(); |
| 270 // Verify that context is shutdown. | 263 // Verify that context is shutdown. |
| 271 try { | 264 try { |
| 272 cronetEngine.getUrlRequestContextAdapter(); | 265 cronetEngine.getUrlRequestContextAdapter(); |
| 273 fail("Should throw an exception."); | 266 fail("Should throw an exception."); |
| 274 } catch (Exception e) { | 267 } catch (Exception e) { |
| 275 assertEquals("Engine is shut down.", e.getMessage()); | 268 assertEquals("Engine is shut down.", e.getMessage()); |
| 276 } | 269 } |
| 277 block.open(); | 270 block.open(); |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 547 assertFalse(netLogDir1.exists()); | 540 assertFalse(netLogDir1.exists()); |
| 548 FileUtils.recursivelyDeleteFile(netLogDir2); | 541 FileUtils.recursivelyDeleteFile(netLogDir2); |
| 549 assertFalse(netLogDir2.exists()); | 542 assertFalse(netLogDir2.exists()); |
| 550 } | 543 } |
| 551 @SmallTest | 544 @SmallTest |
| 552 @Feature({"Cronet"}) | 545 @Feature({"Cronet"}) |
| 553 @OnlyRunNativeCronet | 546 @OnlyRunNativeCronet |
| 554 // Tests that if CronetEngine is shut down on the network thread, an appropr iate exception | 547 // Tests that if CronetEngine is shut down on the network thread, an appropr iate exception |
| 555 // is thrown. | 548 // is thrown. |
| 556 public void testShutDownEngineOnNetworkThread() throws Exception { | 549 public void testShutDownEngineOnNetworkThread() throws Exception { |
| 557 final CronetTestFramework testFramework = | 550 final CronetEngine cronetEngine = createCronetEngine(CronetEngine.Builde r.HTTP_CACHE_DISK); |
| 558 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT TP_CACHE_DISK); | |
| 559 String url = NativeTestServer.getFileURL("/cacheable.txt"); | 551 String url = NativeTestServer.getFileURL("/cacheable.txt"); |
| 560 // Make a request to a cacheable resource. | 552 // Make a request to a cacheable resource. |
| 561 checkRequestCaching(testFramework.mCronetEngine, url, false); | 553 checkRequestCaching(cronetEngine, url, false); |
| 562 | 554 |
| 563 final AtomicReference<Throwable> thrown = new AtomicReference<>(); | 555 final AtomicReference<Throwable> thrown = new AtomicReference<>(); |
| 564 // Shut down the server. | 556 // Shut down the server. |
| 565 NativeTestServer.shutdownNativeTestServer(); | 557 NativeTestServer.shutdownNativeTestServer(); |
| 566 class CancelUrlRequestCallback extends TestUrlRequestCallback { | 558 class CancelUrlRequestCallback extends TestUrlRequestCallback { |
| 567 @Override | 559 @Override |
| 568 public void onResponseStarted(UrlRequest request, UrlResponseInfo in fo) { | 560 public void onResponseStarted(UrlRequest request, UrlResponseInfo in fo) { |
| 569 super.onResponseStarted(request, info); | 561 super.onResponseStarted(request, info); |
| 570 request.cancel(); | 562 request.cancel(); |
| 571 // Shut down CronetEngine immediately after request is destroyed . | 563 // Shut down CronetEngine immediately after request is destroyed . |
| 572 try { | 564 try { |
| 573 testFramework.mCronetEngine.shutdown(); | 565 cronetEngine.shutdown(); |
| 574 } catch (Exception e) { | 566 } catch (Exception e) { |
| 575 thrown.set(e); | 567 thrown.set(e); |
| 576 } | 568 } |
| 577 } | 569 } |
| 578 | 570 |
| 579 @Override | 571 @Override |
| 580 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { | 572 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { |
| 581 // onSucceeded will not happen, because the request is canceled | 573 // onSucceeded will not happen, because the request is canceled |
| 582 // after sending first read and the executor is single threaded. | 574 // after sending first read and the executor is single threaded. |
| 583 throw new RuntimeException("Unexpected"); | 575 throw new RuntimeException("Unexpected"); |
| 584 } | 576 } |
| 585 | 577 |
| 586 @Override | 578 @Override |
| 587 public void onFailed(UrlRequest request, UrlResponseInfo info, Crone tException error) { | 579 public void onFailed(UrlRequest request, UrlResponseInfo info, Crone tException error) { |
| 588 throw new RuntimeException("Unexpected"); | 580 throw new RuntimeException("Unexpected"); |
| 589 } | 581 } |
| 590 } | 582 } |
| 591 Executor directExecutor = new Executor() { | 583 Executor directExecutor = new Executor() { |
| 592 @Override | 584 @Override |
| 593 public void execute(Runnable command) { | 585 public void execute(Runnable command) { |
| 594 command.run(); | 586 command.run(); |
| 595 } | 587 } |
| 596 }; | 588 }; |
| 597 CancelUrlRequestCallback callback = new CancelUrlRequestCallback(); | 589 CancelUrlRequestCallback callback = new CancelUrlRequestCallback(); |
| 598 callback.setAllowDirectExecutor(true); | 590 callback.setAllowDirectExecutor(true); |
| 599 UrlRequest.Builder urlRequestBuilder = | 591 UrlRequest.Builder urlRequestBuilder = |
| 600 testFramework.mCronetEngine.newUrlRequestBuilder(url, callback, directExecutor); | 592 cronetEngine.newUrlRequestBuilder(url, callback, directExecutor) ; |
| 601 urlRequestBuilder.allowDirectExecutor(); | 593 urlRequestBuilder.allowDirectExecutor(); |
| 602 urlRequestBuilder.build().start(); | 594 urlRequestBuilder.build().start(); |
| 603 callback.blockForDone(); | 595 callback.blockForDone(); |
| 604 assertTrue(thrown.get() instanceof RuntimeException); | 596 assertTrue(thrown.get() instanceof RuntimeException); |
| 605 } | 597 } |
| 606 | 598 |
| 607 @SmallTest | 599 @SmallTest |
| 608 @Feature({"Cronet"}) | 600 @Feature({"Cronet"}) |
| 609 @OnlyRunNativeCronet | 601 @OnlyRunNativeCronet |
| 610 // Tests that if CronetEngine is shut down when reading from disk cache, | 602 // Tests that if CronetEngine is shut down when reading from disk cache, |
| 611 // there isn't a crash. See crbug.com/486120. | 603 // there isn't a crash. See crbug.com/486120. |
| 612 public void testShutDownEngineWhenReadingFromDiskCache() throws Exception { | 604 public void testShutDownEngineWhenReadingFromDiskCache() throws Exception { |
| 613 final CronetTestFramework testFramework = | 605 final CronetEngine cronetEngine = createCronetEngine(CronetEngine.Builde r.HTTP_CACHE_DISK); |
| 614 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT TP_CACHE_DISK); | |
| 615 String url = NativeTestServer.getFileURL("/cacheable.txt"); | 606 String url = NativeTestServer.getFileURL("/cacheable.txt"); |
| 616 // Make a request to a cacheable resource. | 607 // Make a request to a cacheable resource. |
| 617 checkRequestCaching(testFramework.mCronetEngine, url, false); | 608 checkRequestCaching(cronetEngine, url, false); |
| 618 | 609 |
| 619 // Shut down the server. | 610 // Shut down the server. |
| 620 NativeTestServer.shutdownNativeTestServer(); | 611 NativeTestServer.shutdownNativeTestServer(); |
| 621 class CancelUrlRequestCallback extends TestUrlRequestCallback { | 612 class CancelUrlRequestCallback extends TestUrlRequestCallback { |
| 622 @Override | 613 @Override |
| 623 public void onResponseStarted(UrlRequest request, UrlResponseInfo in fo) { | 614 public void onResponseStarted(UrlRequest request, UrlResponseInfo in fo) { |
| 624 super.onResponseStarted(request, info); | 615 super.onResponseStarted(request, info); |
| 625 request.cancel(); | 616 request.cancel(); |
| 626 // Shut down CronetEngine immediately after request is destroyed . | 617 // Shut down CronetEngine immediately after request is destroyed . |
| 627 testFramework.mCronetEngine.shutdown(); | 618 cronetEngine.shutdown(); |
| 628 } | 619 } |
| 629 | 620 |
| 630 @Override | 621 @Override |
| 631 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { | 622 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { |
| 632 // onSucceeded will not happen, because the request is canceled | 623 // onSucceeded will not happen, because the request is canceled |
| 633 // after sending first read and the executor is single threaded. | 624 // after sending first read and the executor is single threaded. |
| 634 throw new RuntimeException("Unexpected"); | 625 throw new RuntimeException("Unexpected"); |
| 635 } | 626 } |
| 636 | 627 |
| 637 @Override | 628 @Override |
| 638 public void onFailed(UrlRequest request, UrlResponseInfo info, Crone tException error) { | 629 public void onFailed(UrlRequest request, UrlResponseInfo info, Crone tException error) { |
| 639 throw new RuntimeException("Unexpected"); | 630 throw new RuntimeException("Unexpected"); |
| 640 } | 631 } |
| 641 } | 632 } |
| 642 CancelUrlRequestCallback callback = new CancelUrlRequestCallback(); | 633 CancelUrlRequestCallback callback = new CancelUrlRequestCallback(); |
| 643 UrlRequest.Builder urlRequestBuilder = testFramework.mCronetEngine.newUr lRequestBuilder( | 634 UrlRequest.Builder urlRequestBuilder = |
| 644 url, callback, callback.getExecutor()); | 635 cronetEngine.newUrlRequestBuilder(url, callback, callback.getExe cutor()); |
| 645 urlRequestBuilder.build().start(); | 636 urlRequestBuilder.build().start(); |
| 646 callback.blockForDone(); | 637 callback.blockForDone(); |
| 647 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 638 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 648 assertTrue(callback.mResponseInfo.wasCached()); | 639 assertTrue(callback.mResponseInfo.wasCached()); |
| 649 assertTrue(callback.mOnCanceledCalled); | 640 assertTrue(callback.mOnCanceledCalled); |
| 650 } | 641 } |
| 651 | 642 |
| 652 @SmallTest | 643 @SmallTest |
| 653 @Feature({"Cronet"}) | 644 @Feature({"Cronet"}) |
| 654 @OnlyRunNativeCronet | 645 @OnlyRunNativeCronet |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 892 private void makeRequestAndCheckStatus( | 883 private void makeRequestAndCheckStatus( |
| 893 CronetEngine engine, String url, int expectedStatusCode) { | 884 CronetEngine engine, String url, int expectedStatusCode) { |
| 894 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 885 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 895 UrlRequest request = | 886 UrlRequest request = |
| 896 engine.newUrlRequestBuilder(url, callback, callback.getExecutor( )).build(); | 887 engine.newUrlRequestBuilder(url, callback, callback.getExecutor( )).build(); |
| 897 request.start(); | 888 request.start(); |
| 898 callback.blockForDone(); | 889 callback.blockForDone(); |
| 899 assertEquals(expectedStatusCode, callback.mResponseInfo.getHttpStatusCod e()); | 890 assertEquals(expectedStatusCode, callback.mResponseInfo.getHttpStatusCod e()); |
| 900 } | 891 } |
| 901 | 892 |
| 902 private CronetTestFramework startCronetTestFrameworkWithCacheEnabled(int cac heType) | 893 private CronetEngine createCronetEngine(int cacheType) { |
|
mgersh
2017/05/23 18:12:13
Since this is now used in a test earlier in the fi
pauljensen
2017/05/25 15:15:15
Done.
| |
| 903 throws Exception { | 894 CronetEngine.Builder builder = new CronetEngine.Builder(getContext()); |
| 904 String cacheTypeString = ""; | |
| 905 if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK) { | 895 if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK) { |
| 906 cacheTypeString = CronetTestFramework.CACHE_DISK; | 896 enableDiskCache(builder); |
|
mgersh
2017/05/23 18:12:13
This way of separating things is a little strange.
pauljensen
2017/05/25 15:15:15
Done.
| |
| 907 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP) { | 897 } else { |
| 908 cacheTypeString = CronetTestFramework.CACHE_DISK_NO_HTTP; | 898 builder.enableHttpCache(cacheType, 100 * 1024); |
| 909 } else if (cacheType == HTTP_CACHE_IN_MEMORY) { | |
| 910 cacheTypeString = CronetTestFramework.CACHE_IN_MEMORY; | |
| 911 } | 899 } |
| 912 String[] commandLineArgs = {CronetTestFramework.CACHE_KEY, cacheTypeStri ng}; | |
| 913 CronetTestFramework testFramework = | |
| 914 startCronetTestFrameworkWithUrlAndCommandLineArgs(null, commandL ineArgs); | |
| 915 assertTrue(NativeTestServer.startNativeTestServer(getContext())); | 900 assertTrue(NativeTestServer.startNativeTestServer(getContext())); |
| 916 return testFramework; | 901 return builder.build(); |
| 917 } | 902 } |
| 918 | 903 |
| 919 private void checkRequestCaching(CronetEngine engine, String url, boolean ex pectCached) { | 904 private void checkRequestCaching(CronetEngine engine, String url, boolean ex pectCached) { |
| 920 checkRequestCaching(engine, url, expectCached, false); | 905 checkRequestCaching(engine, url, expectCached, false); |
| 921 } | 906 } |
| 922 | 907 |
| 923 private void checkRequestCaching( | 908 private void checkRequestCaching( |
| 924 CronetEngine engine, String url, boolean expectCached, boolean disab leCache) { | 909 CronetEngine engine, String url, boolean expectCached, boolean disab leCache) { |
| 925 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 910 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 926 UrlRequest.Builder urlRequestBuilder = | 911 UrlRequest.Builder urlRequestBuilder = |
| 927 engine.newUrlRequestBuilder(url, callback, callback.getExecutor( )); | 912 engine.newUrlRequestBuilder(url, callback, callback.getExecutor( )); |
| 928 if (disableCache) { | 913 if (disableCache) { |
| 929 urlRequestBuilder.disableCache(); | 914 urlRequestBuilder.disableCache(); |
| 930 } | 915 } |
| 931 urlRequestBuilder.build().start(); | 916 urlRequestBuilder.build().start(); |
| 932 callback.blockForDone(); | 917 callback.blockForDone(); |
| 933 assertEquals(expectCached, callback.mResponseInfo.wasCached()); | 918 assertEquals(expectCached, callback.mResponseInfo.wasCached()); |
| 934 assertEquals("this is a cacheable file\n", callback.mResponseAsString); | 919 assertEquals("this is a cacheable file\n", callback.mResponseAsString); |
| 935 } | 920 } |
| 936 | 921 |
| 937 @SmallTest | 922 @SmallTest |
| 938 @Feature({"Cronet"}) | 923 @Feature({"Cronet"}) |
| 939 @OnlyRunNativeCronet | 924 @OnlyRunNativeCronet |
| 940 public void testEnableHttpCacheDisabled() throws Exception { | 925 public void testEnableHttpCacheDisabled() throws Exception { |
| 941 final CronetTestFramework testFramework = | 926 CronetEngine cronetEngine = createCronetEngine(CronetEngine.Builder.HTTP _CACHE_DISABLED); |
| 942 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT TP_CACHE_DISABLED); | |
| 943 String url = NativeTestServer.getFileURL("/cacheable.txt"); | 927 String url = NativeTestServer.getFileURL("/cacheable.txt"); |
| 944 checkRequestCaching(testFramework.mCronetEngine, url, false); | 928 checkRequestCaching(cronetEngine, url, false); |
| 945 checkRequestCaching(testFramework.mCronetEngine, url, false); | 929 checkRequestCaching(cronetEngine, url, false); |
| 946 checkRequestCaching(testFramework.mCronetEngine, url, false); | 930 checkRequestCaching(cronetEngine, url, false); |
| 947 } | 931 } |
| 948 | 932 |
| 949 @SmallTest | 933 @SmallTest |
| 950 @Feature({"Cronet"}) | 934 @Feature({"Cronet"}) |
| 951 public void testEnableHttpCacheInMemory() throws Exception { | 935 public void testEnableHttpCacheInMemory() throws Exception { |
| 952 final CronetTestFramework testFramework = | 936 CronetEngine cronetEngine = createCronetEngine(CronetEngine.Builder.HTTP _CACHE_IN_MEMORY); |
| 953 startCronetTestFrameworkWithCacheEnabled(HTTP_CACHE_IN_MEMORY); | |
| 954 String url = NativeTestServer.getFileURL("/cacheable.txt"); | 937 String url = NativeTestServer.getFileURL("/cacheable.txt"); |
| 955 checkRequestCaching(testFramework.mCronetEngine, url, false); | 938 checkRequestCaching(cronetEngine, url, false); |
| 956 checkRequestCaching(testFramework.mCronetEngine, url, true); | 939 checkRequestCaching(cronetEngine, url, true); |
| 957 NativeTestServer.shutdownNativeTestServer(); | 940 NativeTestServer.shutdownNativeTestServer(); |
| 958 checkRequestCaching(testFramework.mCronetEngine, url, true); | 941 checkRequestCaching(cronetEngine, url, true); |
| 959 } | 942 } |
| 960 | 943 |
| 961 @SmallTest | 944 @SmallTest |
| 962 @Feature({"Cronet"}) | 945 @Feature({"Cronet"}) |
| 963 public void testEnableHttpCacheDisk() throws Exception { | 946 public void testEnableHttpCacheDisk() throws Exception { |
| 964 final CronetTestFramework testFramework = | 947 CronetEngine cronetEngine = createCronetEngine(CronetEngine.Builder.HTTP _CACHE_DISK); |
| 965 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT TP_CACHE_DISK); | |
| 966 String url = NativeTestServer.getFileURL("/cacheable.txt"); | 948 String url = NativeTestServer.getFileURL("/cacheable.txt"); |
| 967 checkRequestCaching(testFramework.mCronetEngine, url, false); | 949 checkRequestCaching(cronetEngine, url, false); |
| 968 checkRequestCaching(testFramework.mCronetEngine, url, true); | 950 checkRequestCaching(cronetEngine, url, true); |
| 969 NativeTestServer.shutdownNativeTestServer(); | 951 NativeTestServer.shutdownNativeTestServer(); |
| 970 checkRequestCaching(testFramework.mCronetEngine, url, true); | 952 checkRequestCaching(cronetEngine, url, true); |
| 971 } | 953 } |
| 972 | 954 |
| 973 @SmallTest | 955 @SmallTest |
| 974 @Feature({"Cronet"}) | 956 @Feature({"Cronet"}) |
| 975 @OnlyRunNativeCronet | 957 @OnlyRunNativeCronet |
| 976 public void testEnableHttpCacheDiskNoHttp() throws Exception { | 958 public void testEnableHttpCacheDiskNoHttp() throws Exception { |
| 977 final CronetTestFramework testFramework = | 959 // TODO(pauljensen): This should be testing HTTP_CACHE_DISK_NO_HTTP. |
| 978 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT TP_CACHE_DISABLED); | 960 CronetEngine cronetEngine = createCronetEngine(CronetEngine.Builder.HTTP _CACHE_DISABLED); |
| 979 String url = NativeTestServer.getFileURL("/cacheable.txt"); | 961 String url = NativeTestServer.getFileURL("/cacheable.txt"); |
| 980 checkRequestCaching(testFramework.mCronetEngine, url, false); | 962 checkRequestCaching(cronetEngine, url, false); |
| 981 checkRequestCaching(testFramework.mCronetEngine, url, false); | 963 checkRequestCaching(cronetEngine, url, false); |
| 982 checkRequestCaching(testFramework.mCronetEngine, url, false); | 964 checkRequestCaching(cronetEngine, url, false); |
| 983 } | 965 } |
| 984 | 966 |
| 985 @SmallTest | 967 @SmallTest |
| 986 @Feature({"Cronet"}) | 968 @Feature({"Cronet"}) |
| 987 public void testDisableCache() throws Exception { | 969 public void testDisableCache() throws Exception { |
| 988 final CronetTestFramework testFramework = | 970 CronetEngine cronetEngine = createCronetEngine(CronetEngine.Builder.HTTP _CACHE_DISK); |
| 989 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT TP_CACHE_DISK); | |
| 990 String url = NativeTestServer.getFileURL("/cacheable.txt"); | 971 String url = NativeTestServer.getFileURL("/cacheable.txt"); |
| 991 | 972 |
| 992 // When cache is disabled, making a request does not write to the cache. | 973 // When cache is disabled, making a request does not write to the cache. |
| 993 checkRequestCaching(testFramework.mCronetEngine, url, false, true /** di sable cache */); | 974 checkRequestCaching(cronetEngine, url, false, true /** disable cache */) ; |
| 994 checkRequestCaching(testFramework.mCronetEngine, url, false); | 975 checkRequestCaching(cronetEngine, url, false); |
| 995 | 976 |
| 996 // When cache is enabled, the second request is cached. | 977 // When cache is enabled, the second request is cached. |
| 997 checkRequestCaching(testFramework.mCronetEngine, url, false, true /** di sable cache */); | 978 checkRequestCaching(cronetEngine, url, false, true /** disable cache */) ; |
| 998 checkRequestCaching(testFramework.mCronetEngine, url, true); | 979 checkRequestCaching(cronetEngine, url, true); |
| 999 | 980 |
| 1000 // Shut down the server, next request should have a cached response. | 981 // Shut down the server, next request should have a cached response. |
| 1001 NativeTestServer.shutdownNativeTestServer(); | 982 NativeTestServer.shutdownNativeTestServer(); |
| 1002 checkRequestCaching(testFramework.mCronetEngine, url, true); | 983 checkRequestCaching(cronetEngine, url, true); |
| 1003 | 984 |
| 1004 // Cache is disabled after server is shut down, request should fail. | 985 // Cache is disabled after server is shut down, request should fail. |
| 1005 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 986 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1006 UrlRequest.Builder urlRequestBuilder = testFramework.mCronetEngine.newUr lRequestBuilder( | 987 UrlRequest.Builder urlRequestBuilder = |
| 1007 url, callback, callback.getExecutor()); | 988 cronetEngine.newUrlRequestBuilder(url, callback, callback.getExe cutor()); |
| 1008 urlRequestBuilder.disableCache(); | 989 urlRequestBuilder.disableCache(); |
| 1009 urlRequestBuilder.build().start(); | 990 urlRequestBuilder.build().start(); |
| 1010 callback.blockForDone(); | 991 callback.blockForDone(); |
| 1011 assertNotNull(callback.mError); | 992 assertNotNull(callback.mError); |
| 1012 assertContains("Exception in CronetUrlRequest: net::ERR_CONNECTION_REFUS ED", | 993 assertContains("Exception in CronetUrlRequest: net::ERR_CONNECTION_REFUS ED", |
| 1013 callback.mError.getMessage()); | 994 callback.mError.getMessage()); |
| 1014 } | 995 } |
| 1015 | 996 |
| 1016 @SmallTest | 997 @SmallTest |
| 1017 @Feature({"Cronet"}) | 998 @Feature({"Cronet"}) |
| 1018 public void testEnableHttpCacheDiskNewEngine() throws Exception { | 999 public void testEnableHttpCacheDiskNewEngine() throws Exception { |
| 1019 final CronetTestFramework testFramework = | 1000 CronetEngine cronetEngine = createCronetEngine(CronetEngine.Builder.HTTP _CACHE_DISK); |
| 1020 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT TP_CACHE_DISK); | |
| 1021 String url = NativeTestServer.getFileURL("/cacheable.txt"); | 1001 String url = NativeTestServer.getFileURL("/cacheable.txt"); |
| 1022 checkRequestCaching(testFramework.mCronetEngine, url, false); | 1002 checkRequestCaching(cronetEngine, url, false); |
| 1023 checkRequestCaching(testFramework.mCronetEngine, url, true); | 1003 checkRequestCaching(cronetEngine, url, true); |
| 1024 NativeTestServer.shutdownNativeTestServer(); | 1004 NativeTestServer.shutdownNativeTestServer(); |
| 1025 checkRequestCaching(testFramework.mCronetEngine, url, true); | 1005 checkRequestCaching(cronetEngine, url, true); |
| 1026 | 1006 |
| 1027 // Shutdown original context and create another that uses the same cache . | 1007 // Shutdown original context and create another that uses the same cache . |
| 1028 testFramework.mCronetEngine.shutdown(); | 1008 cronetEngine.shutdown(); |
| 1029 testFramework.mCronetEngine = | 1009 cronetEngine = enableDiskCache(new CronetEngine.Builder(getContext())).b uild(); |
| 1030 (CronetEngineBase) testFramework.getCronetEngineBuilder().build( ); | 1010 checkRequestCaching(cronetEngine, url, true); |
| 1031 checkRequestCaching(testFramework.mCronetEngine, url, true); | |
| 1032 } | 1011 } |
| 1033 | 1012 |
| 1034 @SmallTest | 1013 @SmallTest |
| 1035 @Feature({"Cronet"}) | 1014 @Feature({"Cronet"}) |
| 1036 public void testInitEngineAndStartRequest() { | 1015 public void testInitEngineAndStartRequest() { |
| 1037 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra ryInit(); | |
| 1038 | |
| 1039 // Immediately make a request after initializing the engine. | 1016 // Immediately make a request after initializing the engine. |
| 1040 CronetEngine cronetEngine = testFramework.initCronetEngine(); | 1017 CronetEngine cronetEngine = new CronetEngine.Builder(getContext()).build (); |
| 1041 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 1018 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1042 UrlRequest.Builder urlRequestBuilder = | 1019 UrlRequest.Builder urlRequestBuilder = |
| 1043 cronetEngine.newUrlRequestBuilder(mUrl, callback, callback.getEx ecutor()); | 1020 cronetEngine.newUrlRequestBuilder(mUrl, callback, callback.getEx ecutor()); |
| 1044 urlRequestBuilder.build().start(); | 1021 urlRequestBuilder.build().start(); |
| 1045 callback.blockForDone(); | 1022 callback.blockForDone(); |
| 1046 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 1023 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 1047 } | 1024 } |
| 1048 | 1025 |
| 1049 @SmallTest | 1026 @SmallTest |
| 1050 @Feature({"Cronet"}) | 1027 @Feature({"Cronet"}) |
| 1051 public void testEmptyGetCertVerifierData() { | 1028 public void testEmptyGetCertVerifierData() { |
| 1052 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra ryInit(); | |
| 1053 | |
| 1054 // Immediately make a request after initializing the engine. | 1029 // Immediately make a request after initializing the engine. |
| 1055 ExperimentalCronetEngine cronetEngine = testFramework.initCronetEngine() ; | 1030 ExperimentalCronetEngine cronetEngine = |
| 1031 new ExperimentalCronetEngine.Builder(getContext()).build(); | |
| 1056 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 1032 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1057 UrlRequest.Builder urlRequestBuilder = | 1033 UrlRequest.Builder urlRequestBuilder = |
| 1058 cronetEngine.newUrlRequestBuilder(mUrl, callback, callback.getEx ecutor()); | 1034 cronetEngine.newUrlRequestBuilder(mUrl, callback, callback.getEx ecutor()); |
| 1059 urlRequestBuilder.build().start(); | 1035 urlRequestBuilder.build().start(); |
| 1060 callback.blockForDone(); | 1036 callback.blockForDone(); |
| 1061 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 1037 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 1062 | 1038 |
| 1063 try { | 1039 try { |
| 1064 cronetEngine.getCertVerifierData(-1); | 1040 cronetEngine.getCertVerifierData(-1); |
| 1065 fail("Should throw an exception"); | 1041 fail("Should throw an exception"); |
| 1066 } catch (Exception e) { | 1042 } catch (Exception e) { |
| 1067 assertEquals("timeout must be a positive value", e.getMessage()); | 1043 assertEquals("timeout must be a positive value", e.getMessage()); |
| 1068 } | 1044 } |
| 1069 // Because mUrl is http, getCertVerifierData() will return empty data. | 1045 // Because mUrl is http, getCertVerifierData() will return empty data. |
| 1070 String data = cronetEngine.getCertVerifierData(100); | 1046 String data = cronetEngine.getCertVerifierData(100); |
| 1071 assertTrue(data.isEmpty()); | 1047 assertTrue(data.isEmpty()); |
| 1072 } | 1048 } |
| 1073 | 1049 |
| 1074 @SmallTest | 1050 @SmallTest |
| 1075 @Feature({"Cronet"}) | 1051 @Feature({"Cronet"}) |
| 1076 public void testInitEngineStartTwoRequests() throws Exception { | 1052 public void testInitEngineStartTwoRequests() throws Exception { |
| 1077 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra ryInit(); | |
| 1078 | |
| 1079 // Make two requests after initializing the context. | 1053 // Make two requests after initializing the context. |
| 1080 CronetEngine cronetEngine = testFramework.initCronetEngine(); | 1054 CronetEngine cronetEngine = new CronetEngine.Builder(getContext()).build (); |
| 1081 int[] statusCodes = {0, 0}; | 1055 int[] statusCodes = {0, 0}; |
| 1082 String[] urls = {mUrl, mUrl404}; | 1056 String[] urls = {mUrl, mUrl404}; |
| 1083 for (int i = 0; i < 2; i++) { | 1057 for (int i = 0; i < 2; i++) { |
| 1084 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 1058 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1085 UrlRequest.Builder urlRequestBuilder = | 1059 UrlRequest.Builder urlRequestBuilder = |
| 1086 cronetEngine.newUrlRequestBuilder(urls[i], callback, callbac k.getExecutor()); | 1060 cronetEngine.newUrlRequestBuilder(urls[i], callback, callbac k.getExecutor()); |
| 1087 urlRequestBuilder.build().start(); | 1061 urlRequestBuilder.build().start(); |
| 1088 callback.blockForDone(); | 1062 callback.blockForDone(); |
| 1089 statusCodes[i] = callback.mResponseInfo.getHttpStatusCode(); | 1063 statusCodes[i] = callback.mResponseInfo.getHttpStatusCode(); |
| 1090 } | 1064 } |
| 1091 assertEquals(200, statusCodes[0]); | 1065 assertEquals(200, statusCodes[0]); |
| 1092 assertEquals(404, statusCodes[1]); | 1066 assertEquals(404, statusCodes[1]); |
| 1093 } | 1067 } |
| 1094 | 1068 |
| 1095 @SmallTest | 1069 @SmallTest |
| 1096 @Feature({"Cronet"}) | 1070 @Feature({"Cronet"}) |
| 1097 public void testInitTwoEnginesSimultaneously() throws Exception { | 1071 public void testInitTwoEnginesSimultaneously() throws Exception { |
| 1098 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki pLibraryInit(); | |
| 1099 | |
| 1100 // Threads will block on runBlocker to ensure simultaneous execution. | 1072 // Threads will block on runBlocker to ensure simultaneous execution. |
| 1101 ConditionVariable runBlocker = new ConditionVariable(false); | 1073 ConditionVariable runBlocker = new ConditionVariable(false); |
| 1102 RequestThread thread1 = new RequestThread(testFramework, mUrl, runBlocke r); | 1074 RequestThread thread1 = new RequestThread(mUrl, runBlocker); |
| 1103 RequestThread thread2 = new RequestThread(testFramework, mUrl404, runBlo cker); | 1075 RequestThread thread2 = new RequestThread(mUrl404, runBlocker); |
| 1104 | 1076 |
| 1105 thread1.start(); | 1077 thread1.start(); |
| 1106 thread2.start(); | 1078 thread2.start(); |
| 1107 runBlocker.open(); | 1079 runBlocker.open(); |
| 1108 thread1.join(); | 1080 thread1.join(); |
| 1109 thread2.join(); | 1081 thread2.join(); |
| 1110 assertEquals(200, thread1.mCallback.mResponseInfo.getHttpStatusCode()); | 1082 assertEquals(200, thread1.mCallback.mResponseInfo.getHttpStatusCode()); |
| 1111 assertEquals(404, thread2.mCallback.mResponseInfo.getHttpStatusCode()); | 1083 assertEquals(404, thread2.mCallback.mResponseInfo.getHttpStatusCode()); |
| 1112 } | 1084 } |
| 1113 | 1085 |
| 1114 @SmallTest | 1086 @SmallTest |
| 1115 @Feature({"Cronet"}) | 1087 @Feature({"Cronet"}) |
| 1116 public void testInitTwoEnginesInSequence() throws Exception { | 1088 public void testInitTwoEnginesInSequence() throws Exception { |
| 1117 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki pLibraryInit(); | |
| 1118 | |
| 1119 ConditionVariable runBlocker = new ConditionVariable(true); | 1089 ConditionVariable runBlocker = new ConditionVariable(true); |
| 1120 RequestThread thread1 = new RequestThread(testFramework, mUrl, runBlocke r); | 1090 RequestThread thread1 = new RequestThread(mUrl, runBlocker); |
| 1121 RequestThread thread2 = new RequestThread(testFramework, mUrl404, runBlo cker); | 1091 RequestThread thread2 = new RequestThread(mUrl404, runBlocker); |
| 1122 | 1092 |
| 1123 thread1.start(); | 1093 thread1.start(); |
| 1124 thread1.join(); | 1094 thread1.join(); |
| 1125 thread2.start(); | 1095 thread2.start(); |
| 1126 thread2.join(); | 1096 thread2.join(); |
| 1127 assertEquals(200, thread1.mCallback.mResponseInfo.getHttpStatusCode()); | 1097 assertEquals(200, thread1.mCallback.mResponseInfo.getHttpStatusCode()); |
| 1128 assertEquals(404, thread2.mCallback.mResponseInfo.getHttpStatusCode()); | 1098 assertEquals(404, thread2.mCallback.mResponseInfo.getHttpStatusCode()); |
| 1129 } | 1099 } |
| 1130 | 1100 |
| 1131 @SmallTest | 1101 @SmallTest |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1260 String resolverTestHostname = "some-weird-hostname"; | 1230 String resolverTestHostname = "some-weird-hostname"; |
| 1261 URL testUrl = new URL(mUrl); | 1231 URL testUrl = new URL(mUrl); |
| 1262 ExperimentalCronetEngine.Builder cronetEngineBuilder = | 1232 ExperimentalCronetEngine.Builder cronetEngineBuilder = |
| 1263 new ExperimentalCronetEngine.Builder(getContext()); | 1233 new ExperimentalCronetEngine.Builder(getContext()); |
| 1264 JSONObject hostResolverRules = new JSONObject().put( | 1234 JSONObject hostResolverRules = new JSONObject().put( |
| 1265 "host_resolver_rules", "MAP " + resolverTestHostname + " " + tes tUrl.getHost()); | 1235 "host_resolver_rules", "MAP " + resolverTestHostname + " " + tes tUrl.getHost()); |
| 1266 JSONObject experimentalOptions = | 1236 JSONObject experimentalOptions = |
| 1267 new JSONObject().put("HostResolverRules", hostResolverRules); | 1237 new JSONObject().put("HostResolverRules", hostResolverRules); |
| 1268 cronetEngineBuilder.setExperimentalOptions(experimentalOptions.toString( )); | 1238 cronetEngineBuilder.setExperimentalOptions(experimentalOptions.toString( )); |
| 1269 | 1239 |
| 1270 final CronetTestFramework testFramework = | 1240 final CronetEngine cronetEngine = cronetEngineBuilder.build(); |
| 1271 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, cron etEngineBuilder); | |
| 1272 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 1241 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1273 URL requestUrl = | 1242 URL requestUrl = |
| 1274 new URL("http", resolverTestHostname, testUrl.getPort(), testUrl .getFile()); | 1243 new URL("http", resolverTestHostname, testUrl.getPort(), testUrl .getFile()); |
| 1275 UrlRequest.Builder urlRequestBuilder = testFramework.mCronetEngine.newUr lRequestBuilder( | 1244 UrlRequest.Builder urlRequestBuilder = cronetEngine.newUrlRequestBuilder ( |
| 1276 requestUrl.toString(), callback, callback.getExecutor()); | 1245 requestUrl.toString(), callback, callback.getExecutor()); |
| 1277 urlRequestBuilder.build().start(); | 1246 urlRequestBuilder.build().start(); |
| 1278 callback.blockForDone(); | 1247 callback.blockForDone(); |
| 1279 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 1248 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 1280 } | 1249 } |
| 1281 | 1250 |
| 1282 /** | 1251 /** |
| 1283 * Runs {@code r} on {@code engine}'s network thread. | 1252 * Runs {@code r} on {@code engine}'s network thread. |
| 1284 */ | 1253 */ |
| 1285 private static void postToNetworkThread(final CronetEngine engine, final Run nable r) { | 1254 private static void postToNetworkThread(final CronetEngine engine, final Run nable r) { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1345 } | 1314 } |
| 1346 // Test that valid thread priority range (-20..19) is working. | 1315 // Test that valid thread priority range (-20..19) is working. |
| 1347 for (int threadPriority = -20; threadPriority < 20; threadPriority++) { | 1316 for (int threadPriority = -20; threadPriority < 20; threadPriority++) { |
| 1348 builder.setThreadPriority(threadPriority); | 1317 builder.setThreadPriority(threadPriority); |
| 1349 CronetEngine engine = builder.build(); | 1318 CronetEngine engine = builder.build(); |
| 1350 assertEquals(threadPriority, getThreadPriority(engine)); | 1319 assertEquals(threadPriority, getThreadPriority(engine)); |
| 1351 engine.shutdown(); | 1320 engine.shutdown(); |
| 1352 } | 1321 } |
| 1353 } | 1322 } |
| 1354 } | 1323 } |
| OLD | NEW |