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

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

Issue 972213002: [Cronet] Implement batch read in CronetInputStream (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix findbug warning Created 5 years, 9 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
« no previous file with comments | « components/cronet/android/java/src/org/chromium/net/urlconnection/CronetInputStream.java ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.urlconnection; 5 package org.chromium.net.urlconnection;
6 6
7 import android.os.Build; 7 import android.os.Build;
8 import android.test.suitebuilder.annotation.SmallTest; 8 import android.test.suitebuilder.annotation.SmallTest;
9 9
10 import org.chromium.base.test.util.Feature; 10 import org.chromium.base.test.util.Feature;
11 import org.chromium.net.CronetTestActivity; 11 import org.chromium.net.CronetTestActivity;
12 import org.chromium.net.CronetTestBase; 12 import org.chromium.net.CronetTestBase;
13 import org.chromium.net.MockUrlRequestJobFactory;
13 import org.chromium.net.NativeTestServer; 14 import org.chromium.net.NativeTestServer;
14 import org.chromium.net.UrlRequestContextConfig; 15 import org.chromium.net.UrlRequestContextConfig;
15 import org.chromium.net.UrlRequestException; 16 import org.chromium.net.UrlRequestException;
16 17
17 import java.io.ByteArrayOutputStream; 18 import java.io.ByteArrayOutputStream;
18 import java.io.FileNotFoundException; 19 import java.io.FileNotFoundException;
19 import java.io.IOException; 20 import java.io.IOException;
20 import java.io.InputStream; 21 import java.io.InputStream;
21 import java.net.HttpURLConnection; 22 import java.net.HttpURLConnection;
22 import java.net.MalformedURLException; 23 import java.net.MalformedURLException;
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 } catch (UnsupportedOperationException e) { 428 } catch (UnsupportedOperationException e) {
428 // Expected. 429 // Expected.
429 } 430 }
430 431
431 connection.disconnect(); 432 connection.disconnect();
432 } 433 }
433 434
434 @SmallTest 435 @SmallTest
435 @Feature({"Cronet"}) 436 @Feature({"Cronet"})
436 @CompareDefaultWithCronet 437 @CompareDefaultWithCronet
437 public void testInputStreamReadOneByte() throws Exception { 438 public void testInputStreamBatchReadBoundaryConditions() throws Exception {
438 String testInputString = "this is a really long header"; 439 String testInputString = "this is a very important header";
440 byte[] testInputBytes = testInputString.getBytes();
439 URL url = new URL(NativeTestServer.getEchoHeaderURL("foo")); 441 URL url = new URL(NativeTestServer.getEchoHeaderURL("foo"));
440 HttpURLConnection urlConnection = 442 HttpURLConnection urlConnection =
441 (HttpURLConnection) url.openConnection(); 443 (HttpURLConnection) url.openConnection();
442 urlConnection.addRequestProperty("foo", testInputString); 444 urlConnection.addRequestProperty("foo", testInputString);
443 assertEquals(200, urlConnection.getResponseCode()); 445 assertEquals(200, urlConnection.getResponseCode());
444 assertEquals("OK", urlConnection.getResponseMessage()); 446 assertEquals("OK", urlConnection.getResponseMessage());
445 InputStream in = urlConnection.getInputStream(); 447 InputStream in = urlConnection.getInputStream();
448 try {
449 // Negative byteOffset.
450 int r = in.read(new byte[10], -1, 1);
451 fail();
452 } catch (IndexOutOfBoundsException e) {
453 // Expected.
454 }
455 try {
456 // Negative byteCount.
457 int r = in.read(new byte[10], 1, -1);
458 fail();
459 } catch (IndexOutOfBoundsException e) {
460 // Expected.
461 }
462 try {
463 // Read more than what buffer can hold.
464 int r = in.read(new byte[10], 0, 11);
465 fail();
466 } catch (IndexOutOfBoundsException e) {
467 // Expected.
468 }
469 urlConnection.disconnect();
470 }
471
472 @SmallTest
473 @Feature({"Cronet"})
474 @OnlyRunCronetHttpURLConnection
475 public void testInputStreamReadOneByte() throws Exception {
476 String data = "MyBigFunkyData";
477 int dataLength = data.length();
478 int repeatCount = 100000;
479 MockUrlRequestJobFactory mockUrlRequestJobFactory = new MockUrlRequestJo bFactory(
480 getInstrumentation().getTargetContext());
481 URL url = new URL(mockUrlRequestJobFactory.getMockUrlForData(data,
482 repeatCount));
483 HttpURLConnection connection =
484 (HttpURLConnection) url.openConnection();
485 InputStream in = connection.getInputStream();
446 ByteArrayOutputStream out = new ByteArrayOutputStream(); 486 ByteArrayOutputStream out = new ByteArrayOutputStream();
447 int b; 487 int b;
448 while ((b = in.read()) != -1) { 488 while ((b = in.read()) != -1) {
449 out.write(b); 489 out.write(b);
450 } 490 }
451 urlConnection.disconnect(); 491
452 assertTrue(Arrays.equals( 492 // All data has been read. Try reading beyond what is available should g ive -1.
453 testInputString.getBytes(), out.toByteArray())); 493 assertEquals(-1, in.read());
494 String responseData = new String(out.toByteArray());
495 for (int i = 0; i < repeatCount; ++i) {
496 assertEquals(data, responseData.substring(dataLength * i,
497 dataLength * (i + 1)));
498 }
499 // TODO(xunjieli): Enable after crbug.com/463720 is fixed.
500 // assertEquals(200, connection.getResponseCode());
501 // assertEquals("OK", connection.getResponseMessage());
454 } 502 }
455 503
456 @SmallTest 504 @SmallTest
457 @Feature({"Cronet"}) 505 @Feature({"Cronet"})
458 @CompareDefaultWithCronet 506 @CompareDefaultWithCronet
459 public void testInputStreamReadMoreBytesThanAvailable() throws Exception { 507 public void testInputStreamReadMoreBytesThanAvailable() throws Exception {
460 String testInputString = "this is a really long header"; 508 String testInputString = "this is a really long header";
461 byte[] testInputBytes = testInputString.getBytes(); 509 byte[] testInputBytes = testInputString.getBytes();
462 URL url = new URL(NativeTestServer.getEchoHeaderURL("foo")); 510 URL url = new URL(NativeTestServer.getEchoHeaderURL("foo"));
463 HttpURLConnection urlConnection = 511 HttpURLConnection urlConnection =
464 (HttpURLConnection) url.openConnection(); 512 (HttpURLConnection) url.openConnection();
465 urlConnection.addRequestProperty("foo", testInputString); 513 urlConnection.addRequestProperty("foo", testInputString);
466 assertEquals(200, urlConnection.getResponseCode()); 514 assertEquals(200, urlConnection.getResponseCode());
467 assertEquals("OK", urlConnection.getResponseMessage()); 515 assertEquals("OK", urlConnection.getResponseMessage());
468 InputStream in = urlConnection.getInputStream(); 516 InputStream in = urlConnection.getInputStream();
469 byte[] actualOutput = new byte[testInputBytes.length + 256]; 517 byte[] actualOutput = new byte[testInputBytes.length + 256];
470 int bytesRead = in.read(actualOutput, 0, actualOutput.length); 518 int bytesRead = in.read(actualOutput, 0, actualOutput.length);
519 assertEquals(testInputBytes.length, bytesRead);
471 byte[] readSomeMore = new byte[10]; 520 byte[] readSomeMore = new byte[10];
472 int bytesReadBeyondAvailable = in.read(readSomeMore, 0, 10); 521 int bytesReadBeyondAvailable = in.read(readSomeMore, 0, 10);
473 urlConnection.disconnect();
474 assertEquals(testInputBytes.length, bytesRead);
475 assertEquals(-1, bytesReadBeyondAvailable); 522 assertEquals(-1, bytesReadBeyondAvailable);
476 for (int i = 0; i < bytesRead; i++) { 523 for (int i = 0; i < bytesRead; i++) {
477 assertEquals(testInputBytes[i], actualOutput[i]); 524 assertEquals(testInputBytes[i], actualOutput[i]);
478 } 525 }
526 urlConnection.disconnect();
527 }
528
529 /**
530 * Tests batch reading on CronetInputStream when
531 * {@link CronetHttpURLConnection#getMoreData} is called multiple times.
532 */
533 @SmallTest
534 @Feature({"Cronet"})
535 @OnlyRunCronetHttpURLConnection
536 public void testBigDataRead() throws Exception {
537 String data = "MyBigFunkyData";
538 int dataLength = data.length();
539 int repeatCount = 100000;
540 MockUrlRequestJobFactory mockUrlRequestJobFactory = new MockUrlRequestJo bFactory(
541 getInstrumentation().getTargetContext());
542 URL url = new URL(mockUrlRequestJobFactory.getMockUrlForData(data,
543 repeatCount));
544 HttpURLConnection connection =
545 (HttpURLConnection) url.openConnection();
546 InputStream in = connection.getInputStream();
547 byte[] actualOutput = new byte[dataLength * repeatCount];
548 int totalBytesRead = 0;
549 // Number of bytes to read each time. It is incremented by one from 0.
550 int numBytesToRead = 0;
551 while (totalBytesRead < actualOutput.length) {
552 if (actualOutput.length - totalBytesRead < numBytesToRead) {
553 // Do not read out of bound.
554 numBytesToRead = actualOutput.length - totalBytesRead;
555 }
556 int bytesRead = in.read(actualOutput, totalBytesRead, numBytesToRead );
557 assertTrue(bytesRead <= numBytesToRead);
558 totalBytesRead += bytesRead;
559 numBytesToRead++;
560 }
561
562 // All data has been read. Try reading beyond what is available should g ive -1.
563 assertEquals(0, in.read(actualOutput, 0, 0));
564 assertEquals(-1, in.read(actualOutput, 0, 1));
565
566 String responseData = new String(actualOutput);
567 for (int i = 0; i < repeatCount; ++i) {
568 assertEquals(data, responseData.substring(dataLength * i,
569 dataLength * (i + 1)));
570 }
571 // TODO(xunjieli): Enable after crbug.com/463720 is fixed.
572 // assertEquals(200, connection.getResponseCode());
573 // assertEquals("OK", connection.getResponseMessage());
479 } 574 }
480 575
481 @SmallTest 576 @SmallTest
482 @Feature({"Cronet"}) 577 @Feature({"Cronet"})
483 @CompareDefaultWithCronet 578 @CompareDefaultWithCronet
484 public void testInputStreamReadExactBytesAvailable() throws Exception { 579 public void testInputStreamReadExactBytesAvailable() throws Exception {
485 String testInputString = "this is a really long header"; 580 String testInputString = "this is a really long header";
486 byte[] testInputBytes = testInputString.getBytes(); 581 byte[] testInputBytes = testInputString.getBytes();
487 URL url = new URL(NativeTestServer.getEchoHeaderURL("foo")); 582 URL url = new URL(NativeTestServer.getEchoHeaderURL("foo"));
488 HttpURLConnection urlConnection = 583 HttpURLConnection urlConnection =
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after
851 String headerName) { 946 String headerName) {
852 Pattern pattern = Pattern.compile(headerName + ":\\s(.*)\\r\\n"); 947 Pattern pattern = Pattern.compile(headerName + ":\\s(.*)\\r\\n");
853 Matcher matcher = pattern.matcher(allHeaders); 948 Matcher matcher = pattern.matcher(allHeaders);
854 List<String> headerValues = new ArrayList<String>(); 949 List<String> headerValues = new ArrayList<String>();
855 while (matcher.find()) { 950 while (matcher.find()) {
856 headerValues.add(matcher.group(1)); 951 headerValues.add(matcher.group(1));
857 } 952 }
858 return headerValues; 953 return headerValues;
859 } 954 }
860 } 955 }
OLDNEW
« no previous file with comments | « components/cronet/android/java/src/org/chromium/net/urlconnection/CronetInputStream.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698