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.StrictMode; | 12 import android.os.StrictMode; |
| 13 import android.test.suitebuilder.annotation.SmallTest; | 13 import android.test.suitebuilder.annotation.SmallTest; |
| 14 | 14 |
| 15 import org.chromium.base.FileUtils; | 15 import org.chromium.base.FileUtils; |
| 16 import org.chromium.base.Log; | |
| 17 import org.chromium.base.PathUtils; | 16 import org.chromium.base.PathUtils; |
| 18 import org.chromium.base.annotations.JNINamespace; | 17 import org.chromium.base.annotations.JNINamespace; |
| 19 import org.chromium.base.test.util.Feature; | 18 import org.chromium.base.test.util.Feature; |
| 20 import org.chromium.net.TestUrlRequestCallback.ResponseStep; | 19 import org.chromium.net.TestUrlRequestCallback.ResponseStep; |
| 21 import org.chromium.net.impl.CronetLibraryLoader; | 20 import org.chromium.net.impl.CronetLibraryLoader; |
| 22 import org.chromium.net.impl.CronetUrlRequestContext; | 21 import org.chromium.net.impl.CronetUrlRequestContext; |
| 23 import org.chromium.net.test.EmbeddedTestServer; | 22 import org.chromium.net.test.EmbeddedTestServer; |
| 24 | 23 |
| 25 import java.io.BufferedReader; | 24 import java.io.BufferedReader; |
| 26 import java.io.File; | 25 import java.io.File; |
| 27 import java.io.FileReader; | 26 import java.io.FileReader; |
| 28 import java.util.Arrays; | 27 import java.util.Arrays; |
| 29 import java.util.LinkedList; | 28 import java.util.LinkedList; |
| 30 import java.util.NoSuchElementException; | 29 import java.util.NoSuchElementException; |
| 31 import java.util.concurrent.Executor; | 30 import java.util.concurrent.Executor; |
| 32 import java.util.concurrent.Executors; | 31 import java.util.concurrent.Executors; |
| 33 import java.util.concurrent.ThreadFactory; | 32 import java.util.concurrent.ThreadFactory; |
| 34 | 33 |
| 35 /** | 34 /** |
| 36 * Test CronetEngine. | 35 * Test CronetEngine. |
| 37 */ | 36 */ |
| 38 @JNINamespace("cronet") | 37 @JNINamespace("cronet") |
| 39 public class CronetUrlRequestContextTest extends CronetTestBase { | 38 public class CronetUrlRequestContextTest extends CronetTestBase { |
| 40 // URLs used for tests. | 39 // URLs used for tests. |
| 41 private static final String MOCK_CRONET_TEST_FAILED_URL = | 40 private static final String MOCK_CRONET_TEST_FAILED_URL = |
| 42 "http://mock.failed.request/-2"; | 41 "http://mock.failed.request/-2"; |
| 43 private static final String MOCK_CRONET_TEST_SUCCESS_URL = | 42 private static final String MOCK_CRONET_TEST_SUCCESS_URL = |
| 44 "http://mock.http/success.txt"; | 43 "http://mock.http/success.txt"; |
| 45 private static final String TAG = "RequestContextTest"; | |
| 46 private static final int MAX_FILE_SIZE = 1000000000; | 44 private static final int MAX_FILE_SIZE = 1000000000; |
| 47 private static final int NUM_EVENT_FILES = 10; | 45 private static final int NUM_EVENT_FILES = 10; |
| 48 | 46 |
| 49 private EmbeddedTestServer mTestServer; | 47 private EmbeddedTestServer mTestServer; |
| 50 private String mUrl; | 48 private String mUrl; |
| 51 private String mUrl404; | 49 private String mUrl404; |
| 52 private String mUrl500; | 50 private String mUrl500; |
| 53 CronetTestFramework mTestFramework; | 51 CronetTestFramework mTestFramework; |
|
pauljensen
2016/08/18 15:05:29
Please remove this variable. It's the root cause
kapishnikov
2016/08/18 16:18:33
Done.
| |
| 54 | 52 |
| 55 // Thread on which network quality listeners should be notified. | 53 // Thread on which network quality listeners should be notified. |
| 56 private Thread mNetworkQualityThread; | 54 private Thread mNetworkQualityThread; |
| 57 | 55 |
| 58 @Override | 56 @Override |
| 59 protected void setUp() throws Exception { | 57 protected void setUp() throws Exception { |
| 60 super.setUp(); | 58 super.setUp(); |
| 61 mTestServer = EmbeddedTestServer.createAndStartDefaultServer(getContext( )); | 59 mTestServer = EmbeddedTestServer.createAndStartDefaultServer(getContext( )); |
| 62 mUrl = mTestServer.getURL("/echo?status=200"); | 60 mUrl = mTestServer.getURL("/echo?status=200"); |
| 63 mUrl404 = mTestServer.getURL("/echo?status=404"); | 61 mUrl404 = mTestServer.getURL("/echo?status=404"); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 93 new UrlRequest.Builder(mUrl, mCallback, mCallback.getExecuto r(), cronetEngine); | 91 new UrlRequest.Builder(mUrl, mCallback, mCallback.getExecuto r(), cronetEngine); |
| 94 urlRequestBuilder.build().start(); | 92 urlRequestBuilder.build().start(); |
| 95 mCallback.blockForDone(); | 93 mCallback.blockForDone(); |
| 96 } | 94 } |
| 97 } | 95 } |
| 98 | 96 |
| 99 /** | 97 /** |
| 100 * Callback that shutdowns the request context when request has succeeded | 98 * Callback that shutdowns the request context when request has succeeded |
| 101 * or failed. | 99 * or failed. |
| 102 */ | 100 */ |
| 103 class ShutdownTestUrlRequestCallback extends TestUrlRequestCallback { | 101 static class ShutdownTestUrlRequestCallback extends TestUrlRequestCallback { |
| 102 private final CronetEngine mCronetEngine; | |
| 103 private ConditionVariable mCallbackCompletionBlock = new ConditionVariab le(); | |
|
pauljensen
2016/08/18 15:05:29
final
kapishnikov
2016/08/18 16:18:33
Done.
| |
| 104 | |
| 105 ShutdownTestUrlRequestCallback(CronetEngine cronetEngine) { | |
| 106 mCronetEngine = cronetEngine; | |
| 107 } | |
| 108 | |
| 104 @Override | 109 @Override |
| 105 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { | 110 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { |
| 106 // TODO: Remove logging when http://crbug.com/596929 & http://crbug. com/635025 is fixed. | |
| 107 Log.d(TAG, "ShutdownTestUrlRequestCallback.onSucceeded() has started "); | |
| 108 super.onSucceeded(request, info); | 111 super.onSucceeded(request, info); |
| 109 Log.d(TAG, "ShutdownTestUrlRequestCallback.onSucceeded() before call ing shutdown"); | 112 mCronetEngine.shutdown(); |
| 110 mTestFramework.mCronetEngine.shutdown(); | 113 mCallbackCompletionBlock.open(); |
| 111 Log.d(TAG, "ShutdownTestUrlRequestCallback.hasFinished() has finishe d"); | |
| 112 } | 114 } |
| 113 | 115 |
| 114 @Override | 116 @Override |
| 115 public void onFailed(UrlRequest request, UrlResponseInfo info, UrlReques tException error) { | 117 public void onFailed(UrlRequest request, UrlResponseInfo info, UrlReques tException error) { |
| 116 // TODO: Remove logging when http://crbug.com/596929 & http://crbug. com/635025 is fixed. | |
| 117 Log.d(TAG, "ShutdownTestUrlRequestCallback.onFailed() has started"); | |
| 118 super.onFailed(request, info, error); | 118 super.onFailed(request, info, error); |
| 119 Log.d(TAG, "ShutdownTestUrlRequestCallback.onFailed() before calling shutdown"); | 119 mCronetEngine.shutdown(); |
| 120 mTestFramework.mCronetEngine.shutdown(); | 120 mCallbackCompletionBlock.open(); |
| 121 Log.d(TAG, "ShutdownTestUrlRequestCallback.onFailed() has finished." ); | 121 } |
| 122 | |
| 123 void blockForCallbackToComplete() { | |
|
pauljensen
2016/08/18 15:05:29
add: // Wait for request completion callback.
kapishnikov
2016/08/18 16:18:33
Done.
| |
| 124 mCallbackCompletionBlock.block(); | |
| 122 } | 125 } |
| 123 } | 126 } |
| 124 | 127 |
| 125 static class TestExecutor implements Executor { | 128 static class TestExecutor implements Executor { |
| 126 private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable> (); | 129 private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable> (); |
| 127 | 130 |
| 128 @Override | 131 @Override |
| 129 public void execute(Runnable task) { | 132 public void execute(Runnable task) { |
| 130 mTaskQueue.add(task); | 133 mTaskQueue.add(task); |
| 131 } | 134 } |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 344 != EffectiveConnectionType.EFFECTIVE_CONNECTION_TYPE_UNKNOWN); | 347 != EffectiveConnectionType.EFFECTIVE_CONNECTION_TYPE_UNKNOWN); |
| 345 | 348 |
| 346 mTestFramework.mCronetEngine.shutdown(); | 349 mTestFramework.mCronetEngine.shutdown(); |
| 347 } | 350 } |
| 348 | 351 |
| 349 @SmallTest | 352 @SmallTest |
| 350 @Feature({"Cronet"}) | 353 @Feature({"Cronet"}) |
| 351 // TODO: Remove the annotation after fixing http://crbug.com/637979 & http:/ /crbug.com/637972 | 354 // TODO: Remove the annotation after fixing http://crbug.com/637979 & http:/ /crbug.com/637972 |
| 352 @OnlyRunNativeCronet | 355 @OnlyRunNativeCronet |
| 353 public void testShutdown() throws Exception { | 356 public void testShutdown() throws Exception { |
| 354 Log.i(TAG, "testShutdown() has started"); | |
| 355 mTestFramework = startCronetTestFramework(); | 357 mTestFramework = startCronetTestFramework(); |
| 356 TestUrlRequestCallback callback = new ShutdownTestUrlRequestCallback(); | 358 ShutdownTestUrlRequestCallback callback = |
| 359 new ShutdownTestUrlRequestCallback(mTestFramework.mCronetEngine) ; | |
| 357 // Block callback when response starts to verify that shutdown fails | 360 // Block callback when response starts to verify that shutdown fails |
| 358 // if there are active requests. | 361 // if there are active requests. |
| 359 callback.setAutoAdvance(false); | 362 callback.setAutoAdvance(false); |
| 360 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 363 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( |
| 361 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); | 364 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); |
| 362 UrlRequest urlRequest = urlRequestBuilder.build(); | 365 UrlRequest urlRequest = urlRequestBuilder.build(); |
| 363 urlRequest.start(); | 366 urlRequest.start(); |
| 364 try { | 367 try { |
| 365 mTestFramework.mCronetEngine.shutdown(); | 368 mTestFramework.mCronetEngine.shutdown(); |
| 366 fail("Should throw an exception"); | 369 fail("Should throw an exception"); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 388 } catch (Exception e) { | 391 } catch (Exception e) { |
| 389 assertEquals("Cannot shutdown with active requests.", | 392 assertEquals("Cannot shutdown with active requests.", |
| 390 e.getMessage()); | 393 e.getMessage()); |
| 391 } | 394 } |
| 392 | 395 |
| 393 // May not have read all the data, in theory. Just enable auto-advance | 396 // May not have read all the data, in theory. Just enable auto-advance |
| 394 // and finish the request. | 397 // and finish the request. |
| 395 callback.setAutoAdvance(true); | 398 callback.setAutoAdvance(true); |
| 396 callback.startNextRead(urlRequest); | 399 callback.startNextRead(urlRequest); |
| 397 callback.blockForDone(); | 400 callback.blockForDone(); |
| 398 // TODO: Remove sleep when http://crbug.com/596929 is fixed. | 401 callback.blockForCallbackToComplete(); |
| 399 // The sleep gives the thread that shuts down the engine time to complet e. | 402 callback.shutdownExecutor(); |
| 400 // See http://crbug.com/596929 | |
| 401 Thread.sleep(3000); | |
| 402 Log.i(TAG, "testShutdown() has finished"); | |
| 403 } | 403 } |
| 404 | 404 |
| 405 @SmallTest | 405 @SmallTest |
| 406 @Feature({"Cronet"}) | 406 @Feature({"Cronet"}) |
| 407 @OnlyRunNativeCronet | 407 @OnlyRunNativeCronet |
| 408 public void testShutdownDuringInit() throws Exception { | 408 public void testShutdownDuringInit() throws Exception { |
| 409 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki pLibraryInit(); | 409 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki pLibraryInit(); |
| 410 final ConditionVariable block = new ConditionVariable(false); | 410 final ConditionVariable block = new ConditionVariable(false); |
| 411 | 411 |
| 412 // Post a task to main thread to block until shutdown is called to test | 412 // Post a task to main thread to block until shutdown is called to test |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 486 } catch (Exception e) { | 486 } catch (Exception e) { |
| 487 assertEquals("Engine is shut down.", e.getMessage()); | 487 assertEquals("Engine is shut down.", e.getMessage()); |
| 488 } | 488 } |
| 489 } | 489 } |
| 490 | 490 |
| 491 @SmallTest | 491 @SmallTest |
| 492 @Feature({"Cronet"}) | 492 @Feature({"Cronet"}) |
| 493 // TODO: Remove the annotation after fixing http://crbug.com/637972 | 493 // TODO: Remove the annotation after fixing http://crbug.com/637972 |
| 494 @OnlyRunNativeCronet | 494 @OnlyRunNativeCronet |
| 495 public void testShutdownAfterError() throws Exception { | 495 public void testShutdownAfterError() throws Exception { |
| 496 Log.i(TAG, "testShutdownAfterError() has started"); | |
| 497 mTestFramework = startCronetTestFramework(); | 496 mTestFramework = startCronetTestFramework(); |
| 498 TestUrlRequestCallback callback = new ShutdownTestUrlRequestCallback(); | 497 ShutdownTestUrlRequestCallback callback = |
| 498 new ShutdownTestUrlRequestCallback(mTestFramework.mCronetEngine) ; | |
| 499 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(MOCK_CRONE T_TEST_FAILED_URL, | 499 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(MOCK_CRONE T_TEST_FAILED_URL, |
| 500 callback, callback.getExecutor(), mTestFramework.mCronetEngine); | 500 callback, callback.getExecutor(), mTestFramework.mCronetEngine); |
| 501 urlRequestBuilder.build().start(); | 501 urlRequestBuilder.build().start(); |
| 502 callback.blockForDone(); | 502 callback.blockForDone(); |
| 503 assertTrue(callback.mOnErrorCalled); | 503 assertTrue(callback.mOnErrorCalled); |
| 504 // TODO: Remove sleep when http://crbug.com/635025 is fixed. | 504 callback.blockForCallbackToComplete(); |
| 505 // The sleep gives the thread that shuts down the engine time to complet e. | 505 callback.shutdownExecutor(); |
| 506 // See http://crbug.com/637986 | |
| 507 Thread.sleep(3000); | |
| 508 Log.i(TAG, "testShutdownAfterError() has finished"); | |
| 509 } | 506 } |
| 510 | 507 |
| 511 @SmallTest | 508 @SmallTest |
| 512 @Feature({"Cronet"}) | 509 @Feature({"Cronet"}) |
| 513 public void testShutdownAfterCancel() throws Exception { | 510 public void testShutdownAfterCancel() throws Exception { |
| 514 mTestFramework = startCronetTestFramework(); | 511 mTestFramework = startCronetTestFramework(); |
| 515 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 512 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 516 // Block callback when response starts to verify that shutdown fails | 513 // Block callback when response starts to verify that shutdown fails |
| 517 // if there are active requests. | 514 // if there are active requests. |
| 518 callback.setAutoAdvance(false); | 515 callback.setAutoAdvance(false); |
| (...skipping 857 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1376 } | 1373 } |
| 1377 }.start(); | 1374 }.start(); |
| 1378 otherThreadDone.block(); | 1375 otherThreadDone.block(); |
| 1379 builder.build().shutdown(); | 1376 builder.build().shutdown(); |
| 1380 uiThreadDone.open(); | 1377 uiThreadDone.open(); |
| 1381 } | 1378 } |
| 1382 }); | 1379 }); |
| 1383 assertTrue(uiThreadDone.block(1000)); | 1380 assertTrue(uiThreadDone.block(1000)); |
| 1384 } | 1381 } |
| 1385 } | 1382 } |
| OLD | NEW |