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

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

Issue 2697833003: [Cronet] Add isDone() check in CronetBidirectionalStream#onWritevCompleted (Closed)
Patch Set: re-enable check Created 3 years, 10 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 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
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.
404 public void testCancelWhileWriteDataPending() throws Exception {
xunjieli 2017/02/15 15:51:43 Misha, so I came up with this test where we cancel
mef 2017/02/15 18:20:14 Great, thanks for making this work!
405 String url = Http2TestServer.getEchoStreamUrl();
406 // Use a direct executor to avoid race.
407 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa llback(
408 /*useDirectExecutor*/ true) {
409 @Override
410 public void onStreamReady(BidirectionalStream stream) {
411 // Start the first write.
412 stream.write(getDummyData(), false);
413 stream.flush();
414 }
415 @Override
416 public void onReadCompleted(BidirectionalStream stream, UrlResponseI nfo info,
417 ByteBuffer byteBuffer, boolean endOfStream) {
418 super.onReadCompleted(stream, info, byteBuffer, endOfStream);
419 // Cancel now when the write side is busy.
420 stream.cancel();
mef 2017/02/15 18:20:14 Nice, this is happening while write is in flight.
421 }
422 @Override
423 public void onWriteCompleted(BidirectionalStream stream, UrlResponse Info info,
424 ByteBuffer buffer, boolean endOfStream) {
425 // Flush twice to keep the flush queue non-empty.
426 stream.write(getDummyData(), false);
427 stream.flush();
428 stream.write(getDummyData(), false);
429 stream.flush();
430 }
431 // Returns a piece of dummy data to send to the server.
432 private ByteBuffer getDummyData() {
433 byte[] data = new byte[100];
434 for (int i = 0; i < data.length; i++) {
435 data[i] = 'x';
436 }
437 ByteBuffer dummyData = ByteBuffer.allocateDirect(data.length);
mef 2017/02/15 18:20:14 nit: I think you can do dummyData.put('x') in the
xunjieli 2017/02/15 20:32:46 If I put a single byte each time, flip() will only
xunjieli 2017/02/15 21:32:12 Sorry, my bad. flip() does reset the position to 0
438 dummyData.put(data);
439 dummyData.flip();
440 return dummyData;
441 }
442 };
443 CronetBidirectionalStream stream =
444 (CronetBidirectionalStream) mTestFramework.mCronetEngine
445 .newBidirectionalStreamBuilder(url, callback, callback.g etExecutor())
446 .build();
447 stream.start();
448 callback.blockForDone();
449 assertTrue(callback.mOnCanceledCalled);
450 }
451
452 @SmallTest
453 @Feature({"Cronet"})
454 @OnlyRunNativeCronet
403 public void testSimpleGetWithFlush() throws Exception { 455 public void testSimpleGetWithFlush() throws Exception {
404 // TODO(xunjieli): Use ParameterizedTest instead of the loop. 456 // TODO(xunjieli): Use ParameterizedTest instead of the loop.
405 for (int i = 0; i < 2; i++) { 457 for (int i = 0; i < 2; i++) {
406 String url = Http2TestServer.getEchoStreamUrl(); 458 String url = Http2TestServer.getEchoStreamUrl();
407 TestBidirectionalStreamCallback callback = new TestBidirectionalStre amCallback() { 459 TestBidirectionalStreamCallback callback = new TestBidirectionalStre amCallback() {
408 @Override 460 @Override
409 public void onStreamReady(BidirectionalStream stream) { 461 public void onStreamReady(BidirectionalStream stream) {
410 try { 462 try {
411 // Attempt to write data for GET request. 463 // Attempt to write data for GET request.
412 stream.write(ByteBuffer.wrap("dummy".getBytes()), true); 464 stream.write(ByteBuffer.wrap("dummy".getBytes()), true);
(...skipping 1037 matching lines...) Expand 10 before | Expand all | Expand 10 after
1450 private static String bufferContentsToString(ByteBuffer byteBuffer, int star t, int end) { 1502 private static String bufferContentsToString(ByteBuffer byteBuffer, int star t, int end) {
1451 // Use a duplicate to avoid modifying byteBuffer. 1503 // Use a duplicate to avoid modifying byteBuffer.
1452 ByteBuffer duplicate = byteBuffer.duplicate(); 1504 ByteBuffer duplicate = byteBuffer.duplicate();
1453 duplicate.position(start); 1505 duplicate.position(start);
1454 duplicate.limit(end); 1506 duplicate.limit(end);
1455 byte[] contents = new byte[duplicate.remaining()]; 1507 byte[] contents = new byte[duplicate.remaining()];
1456 duplicate.get(contents); 1508 duplicate.get(contents);
1457 return new String(contents); 1509 return new String(contents);
1458 } 1510 }
1459 } 1511 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698