| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.os.ConditionVariable; | 9 import android.os.ConditionVariable; |
| 10 import android.support.test.filters.SmallTest; | 10 import android.support.test.filters.SmallTest; |
| (...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 assertEquals("123456", callback.mResponseAsString); | 393 assertEquals("123456", callback.mResponseAsString); |
| 394 assertEquals("bar", callback.mResponseInfo.getAllHeaders().get("echo-foo
").get(0)); | 394 assertEquals("bar", callback.mResponseInfo.getAllHeaders().get("echo-foo
").get(0)); |
| 395 assertEquals("", callback.mResponseInfo.getAllHeaders().get("echo-empty"
).get(0)); | 395 assertEquals("", callback.mResponseInfo.getAllHeaders().get("echo-empty"
).get(0)); |
| 396 assertEquals( | 396 assertEquals( |
| 397 "zebra", callback.mResponseInfo.getAllHeaders().get("echo-conten
t-type").get(0)); | 397 "zebra", callback.mResponseInfo.getAllHeaders().get("echo-conten
t-type").get(0)); |
| 398 } | 398 } |
| 399 | 399 |
| 400 @SmallTest | 400 @SmallTest |
| 401 @Feature({"Cronet"}) | 401 @Feature({"Cronet"}) |
| 402 @OnlyRunNativeCronet | 402 @OnlyRunNativeCronet |
| 403 // Regression test for crbug.com/692168. This test is racy, but it should ca
tch the regression |
| 404 // sometimes. |
| 405 public void testCancelWhileWriteDataPending() throws Exception { |
| 406 String url = Http2TestServer.getEchoStreamUrl(); |
| 407 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa
llback() { |
| 408 // Number of onWriteCompleted callbacks that have been invoked. |
| 409 private int mNumWriteCompleted = 0; |
| 410 @Override |
| 411 public void onWriteCompleted(BidirectionalStream stream, UrlResponse
Info info, |
| 412 ByteBuffer buffer, boolean endOfStream) { |
| 413 mNumWriteCompleted++; |
| 414 super.onWriteCompleted(stream, info, buffer, endOfStream); |
| 415 assertTrue(mNumWriteCompleted <= 2); |
| 416 // Cancel in the second write callback to get a better chance of
catching the race. |
| 417 if (mNumWriteCompleted == 2) { |
| 418 stream.cancel(); |
| 419 } |
| 420 } |
| 421 }; |
| 422 byte[] dummyString = "dummy".getBytes(); |
| 423 callback.addWriteData(dummyString, true); |
| 424 callback.addWriteData(dummyString, false); |
| 425 callback.addWriteData(dummyString, true); |
| 426 callback.addWriteData(dummyString, false); |
| 427 callback.addWriteData(dummyString, false); |
| 428 callback.addWriteData(dummyString, false); |
| 429 callback.addWriteData(dummyString, true); |
| 430 callback.addWriteData(dummyString, false); |
| 431 callback.addWriteData(dummyString, true); |
| 432 callback.addWriteData(dummyString, false); |
| 433 CronetBidirectionalStream stream = |
| 434 (CronetBidirectionalStream) mTestFramework.mCronetEngine |
| 435 .newBidirectionalStreamBuilder(url, callback, callback.g
etExecutor()) |
| 436 .build(); |
| 437 callback.setAutoAdvance(false); |
| 438 stream.start(); |
| 439 callback.waitForNextWriteStep(); // onStreamReady |
| 440 // Write 1 and flush(). |
| 441 callback.startNextWrite(stream); |
| 442 // Write 2, 3 and flush(). 2 and 3 are in the flush queue. |
| 443 callback.startNextWrite(stream); |
| 444 callback.setAutoAdvance(true); |
| 445 callback.blockForDone(); |
| 446 assertTrue(callback.mOnCanceledCalled); |
| 447 } |
| 448 |
| 449 @SmallTest |
| 450 @Feature({"Cronet"}) |
| 451 @OnlyRunNativeCronet |
| 403 public void testSimpleGetWithFlush() throws Exception { | 452 public void testSimpleGetWithFlush() throws Exception { |
| 404 // TODO(xunjieli): Use ParameterizedTest instead of the loop. | 453 // TODO(xunjieli): Use ParameterizedTest instead of the loop. |
| 405 for (int i = 0; i < 2; i++) { | 454 for (int i = 0; i < 2; i++) { |
| 406 String url = Http2TestServer.getEchoStreamUrl(); | 455 String url = Http2TestServer.getEchoStreamUrl(); |
| 407 TestBidirectionalStreamCallback callback = new TestBidirectionalStre
amCallback() { | 456 TestBidirectionalStreamCallback callback = new TestBidirectionalStre
amCallback() { |
| 408 @Override | 457 @Override |
| 409 public void onStreamReady(BidirectionalStream stream) { | 458 public void onStreamReady(BidirectionalStream stream) { |
| 410 try { | 459 try { |
| 411 // Attempt to write data for GET request. | 460 // Attempt to write data for GET request. |
| 412 stream.write(ByteBuffer.wrap("dummy".getBytes()), true); | 461 stream.write(ByteBuffer.wrap("dummy".getBytes()), true); |
| (...skipping 1037 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1450 private static String bufferContentsToString(ByteBuffer byteBuffer, int star
t, int end) { | 1499 private static String bufferContentsToString(ByteBuffer byteBuffer, int star
t, int end) { |
| 1451 // Use a duplicate to avoid modifying byteBuffer. | 1500 // Use a duplicate to avoid modifying byteBuffer. |
| 1452 ByteBuffer duplicate = byteBuffer.duplicate(); | 1501 ByteBuffer duplicate = byteBuffer.duplicate(); |
| 1453 duplicate.position(start); | 1502 duplicate.position(start); |
| 1454 duplicate.limit(end); | 1503 duplicate.limit(end); |
| 1455 byte[] contents = new byte[duplicate.remaining()]; | 1504 byte[] contents = new byte[duplicate.remaining()]; |
| 1456 duplicate.get(contents); | 1505 duplicate.get(contents); |
| 1457 return new String(contents); | 1506 return new String(contents); |
| 1458 } | 1507 } |
| 1459 } | 1508 } |
| OLD | NEW |