| 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 android.os.ConditionVariable; | 7 import android.os.ConditionVariable; |
| 8 import android.test.suitebuilder.annotation.SmallTest; | 8 import android.test.suitebuilder.annotation.SmallTest; |
| 9 | 9 |
| 10 import org.chromium.base.test.util.DisabledTest; | 10 import org.chromium.base.test.util.DisabledTest; |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 assertEquals("Test String1234567890woot!", callback.mResponseAsString); | 264 assertEquals("Test String1234567890woot!", callback.mResponseAsString); |
| 265 assertEquals("bar", callback.mResponseInfo.getAllHeaders().get("echo-foo
").get(0)); | 265 assertEquals("bar", callback.mResponseInfo.getAllHeaders().get("echo-foo
").get(0)); |
| 266 assertEquals("", callback.mResponseInfo.getAllHeaders().get("echo-empty"
).get(0)); | 266 assertEquals("", callback.mResponseInfo.getAllHeaders().get("echo-empty"
).get(0)); |
| 267 assertEquals( | 267 assertEquals( |
| 268 "zebra", callback.mResponseInfo.getAllHeaders().get("echo-conten
t-type").get(0)); | 268 "zebra", callback.mResponseInfo.getAllHeaders().get("echo-conten
t-type").get(0)); |
| 269 } | 269 } |
| 270 | 270 |
| 271 @SmallTest | 271 @SmallTest |
| 272 @Feature({"Cronet"}) | 272 @Feature({"Cronet"}) |
| 273 @OnlyRunNativeCronet | 273 @OnlyRunNativeCronet |
| 274 // Tests that a delayed flush() only sends buffers that have been written |
| 275 // before it is called, and it doesn't flush buffers in mPendingQueue. |
| 276 public void testFlushData() throws Exception { |
| 277 String url = Http2TestServer.getEchoStreamUrl(); |
| 278 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa
llback() { |
| 279 // Number of onWriteCompleted callbacks that have been invoked. |
| 280 private int mNumWriteCompleted = 0; |
| 281 @Override |
| 282 public void onWriteCompleted(BidirectionalStream stream, UrlResponse
Info info, |
| 283 ByteBuffer buffer, boolean endOfStream) { |
| 284 super.onWriteCompleted(stream, info, buffer, endOfStream); |
| 285 mNumWriteCompleted++; |
| 286 if (mNumWriteCompleted <= 3) { |
| 287 // 6 is in pending queue |
| 288 assertEquals(1, |
| 289 ((CronetBidirectionalStream) stream).getPendingQueue
SizeForTesting()); |
| 290 // 4 and 5 have been flushed. |
| 291 assertEquals( |
| 292 0, ((CronetBidirectionalStream) stream).getFlushQueu
eSizeForTesting()); |
| 293 } else if (mNumWriteCompleted == 5) { |
| 294 // Now flush 6. |
| 295 assertEquals(1, |
| 296 ((CronetBidirectionalStream) stream).getPendingQueue
SizeForTesting()); |
| 297 stream.flush(); |
| 298 assertEquals(0, |
| 299 ((CronetBidirectionalStream) stream).getPendingQueue
SizeForTesting()); |
| 300 assertEquals( |
| 301 0, ((CronetBidirectionalStream) stream).getFlushQueu
eSizeForTesting()); |
| 302 } |
| 303 } |
| 304 }; |
| 305 callback.addWriteData("1".getBytes(), false); |
| 306 callback.addWriteData("2".getBytes(), false); |
| 307 callback.addWriteData("3".getBytes(), true); |
| 308 callback.addWriteData("4".getBytes(), false); |
| 309 callback.addWriteData("5".getBytes(), true); |
| 310 callback.addWriteData("6".getBytes(), false); |
| 311 CronetBidirectionalStream stream = (CronetBidirectionalStream) new Bidir
ectionalStream |
| 312 .Builder(url, callback, callb
ack.getExecutor(), |
| 313 mTestFramework.mCrone
tEngine) |
| 314 .disableAutoFlush(true) |
| 315 .addHeader("foo", "bar") |
| 316 .addHeader("empty", "") |
| 317 .addHeader("Content-Type", "z
ebra") |
| 318 .build(); |
| 319 callback.setAutoAdvance(false); |
| 320 stream.start(); |
| 321 callback.waitForNextWriteStep(); // onStreamReady |
| 322 assertEquals(0, stream.getPendingQueueSizeForTesting()); |
| 323 assertEquals(0, stream.getFlushQueueSizeForTesting()); |
| 324 // Write 1, 2, 3 and flush(). |
| 325 callback.startNextWrite(stream); |
| 326 // Write 4, 5 and flush(). 4, 5 will be in flush queue. |
| 327 callback.startNextWrite(stream); |
| 328 // Write 6, but do not flush. 6 will be in pending queue. |
| 329 callback.startNextWrite(stream); |
| 330 |
| 331 callback.setAutoAdvance(true); |
| 332 callback.blockForDone(); |
| 333 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 334 assertEquals("123456", callback.mResponseAsString); |
| 335 assertEquals("bar", callback.mResponseInfo.getAllHeaders().get("echo-foo
").get(0)); |
| 336 assertEquals("", callback.mResponseInfo.getAllHeaders().get("echo-empty"
).get(0)); |
| 337 assertEquals( |
| 338 "zebra", callback.mResponseInfo.getAllHeaders().get("echo-conten
t-type").get(0)); |
| 339 } |
| 340 |
| 341 @SmallTest |
| 342 @Feature({"Cronet"}) |
| 343 @OnlyRunNativeCronet |
| 274 public void testSimpleGetWithFlush() throws Exception { | 344 public void testSimpleGetWithFlush() throws Exception { |
| 275 // TODO(xunjieli): Use ParameterizedTest instead of the loop. | 345 // TODO(xunjieli): Use ParameterizedTest instead of the loop. |
| 276 for (int i = 0; i < 2; i++) { | 346 for (int i = 0; i < 2; i++) { |
| 277 String url = Http2TestServer.getEchoStreamUrl(); | 347 String url = Http2TestServer.getEchoStreamUrl(); |
| 278 TestBidirectionalStreamCallback callback = new TestBidirectionalStre
amCallback() { | 348 TestBidirectionalStreamCallback callback = new TestBidirectionalStre
amCallback() { |
| 279 @Override | 349 @Override |
| 280 public void onStreamReady(BidirectionalStream stream) { | 350 public void onStreamReady(BidirectionalStream stream) { |
| 281 try { | 351 try { |
| 282 // Attempt to write data for GET request. | 352 // Attempt to write data for GET request. |
| 283 stream.write(ByteBuffer.wrap("dummy".getBytes()), true); | 353 stream.write(ByteBuffer.wrap("dummy".getBytes()), true); |
| (...skipping 997 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1281 private static String bufferContentsToString(ByteBuffer byteBuffer, int star
t, int end) { | 1351 private static String bufferContentsToString(ByteBuffer byteBuffer, int star
t, int end) { |
| 1282 // Use a duplicate to avoid modifying byteBuffer. | 1352 // Use a duplicate to avoid modifying byteBuffer. |
| 1283 ByteBuffer duplicate = byteBuffer.duplicate(); | 1353 ByteBuffer duplicate = byteBuffer.duplicate(); |
| 1284 duplicate.position(start); | 1354 duplicate.position(start); |
| 1285 duplicate.limit(end); | 1355 duplicate.limit(end); |
| 1286 byte[] contents = new byte[duplicate.remaining()]; | 1356 byte[] contents = new byte[duplicate.remaining()]; |
| 1287 duplicate.get(contents); | 1357 duplicate.get(contents); |
| 1288 return new String(contents); | 1358 return new String(contents); |
| 1289 } | 1359 } |
| 1290 } | 1360 } |
| OLD | NEW |