| 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 List<ByteBuffer> pendingData = |
| 289 ((CronetBidirectionalStream) stream).getPendingDataF
orTesting(); |
| 290 assertEquals(1, pendingData.size()); |
| 291 ByteBuffer pendingBuffer = pendingData.get(0); |
| 292 byte[] content = new byte[pendingBuffer.remaining()]; |
| 293 pendingBuffer.get(content); |
| 294 assertTrue(Arrays.equals("6".getBytes(), content)); |
| 295 |
| 296 // "4" and "5" have been flushed. |
| 297 assertEquals(0, |
| 298 ((CronetBidirectionalStream) stream).getFlushDataFor
Testing().size()); |
| 299 } else if (mNumWriteCompleted == 5) { |
| 300 // Now flush "6", which is still in pending queue. |
| 301 List<ByteBuffer> pendingData = |
| 302 ((CronetBidirectionalStream) stream).getPendingDataF
orTesting(); |
| 303 assertEquals(1, pendingData.size()); |
| 304 ByteBuffer pendingBuffer = pendingData.get(0); |
| 305 byte[] content = new byte[pendingBuffer.remaining()]; |
| 306 pendingBuffer.get(content); |
| 307 assertTrue(Arrays.equals("6".getBytes(), content)); |
| 308 |
| 309 stream.flush(); |
| 310 |
| 311 assertEquals(0, |
| 312 ((CronetBidirectionalStream) stream).getPendingDataF
orTesting().size()); |
| 313 assertEquals(0, |
| 314 ((CronetBidirectionalStream) stream).getFlushDataFor
Testing().size()); |
| 315 } |
| 316 } |
| 317 }; |
| 318 callback.addWriteData("1".getBytes(), false); |
| 319 callback.addWriteData("2".getBytes(), false); |
| 320 callback.addWriteData("3".getBytes(), true); |
| 321 callback.addWriteData("4".getBytes(), false); |
| 322 callback.addWriteData("5".getBytes(), true); |
| 323 callback.addWriteData("6".getBytes(), false); |
| 324 CronetBidirectionalStream stream = (CronetBidirectionalStream) new Bidir
ectionalStream |
| 325 .Builder(url, callback, callb
ack.getExecutor(), |
| 326 mTestFramework.mCrone
tEngine) |
| 327 .disableAutoFlush(true) |
| 328 .addHeader("foo", "bar") |
| 329 .addHeader("empty", "") |
| 330 .addHeader("Content-Type", "z
ebra") |
| 331 .build(); |
| 332 callback.setAutoAdvance(false); |
| 333 stream.start(); |
| 334 callback.waitForNextWriteStep(); // onStreamReady |
| 335 |
| 336 assertEquals(0, stream.getPendingDataForTesting().size()); |
| 337 assertEquals(0, stream.getFlushDataForTesting().size()); |
| 338 |
| 339 // Write 1, 2, 3 and flush(). |
| 340 callback.startNextWrite(stream); |
| 341 // Write 4, 5 and flush(). 4, 5 will be in flush queue. |
| 342 callback.startNextWrite(stream); |
| 343 // Write 6, but do not flush. 6 will be in pending queue. |
| 344 callback.startNextWrite(stream); |
| 345 |
| 346 callback.setAutoAdvance(true); |
| 347 callback.blockForDone(); |
| 348 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 349 assertEquals("123456", callback.mResponseAsString); |
| 350 assertEquals("bar", callback.mResponseInfo.getAllHeaders().get("echo-foo
").get(0)); |
| 351 assertEquals("", callback.mResponseInfo.getAllHeaders().get("echo-empty"
).get(0)); |
| 352 assertEquals( |
| 353 "zebra", callback.mResponseInfo.getAllHeaders().get("echo-conten
t-type").get(0)); |
| 354 } |
| 355 |
| 356 @SmallTest |
| 357 @Feature({"Cronet"}) |
| 358 @OnlyRunNativeCronet |
| 274 public void testSimpleGetWithFlush() throws Exception { | 359 public void testSimpleGetWithFlush() throws Exception { |
| 275 // TODO(xunjieli): Use ParameterizedTest instead of the loop. | 360 // TODO(xunjieli): Use ParameterizedTest instead of the loop. |
| 276 for (int i = 0; i < 2; i++) { | 361 for (int i = 0; i < 2; i++) { |
| 277 String url = Http2TestServer.getEchoStreamUrl(); | 362 String url = Http2TestServer.getEchoStreamUrl(); |
| 278 TestBidirectionalStreamCallback callback = new TestBidirectionalStre
amCallback() { | 363 TestBidirectionalStreamCallback callback = new TestBidirectionalStre
amCallback() { |
| 279 @Override | 364 @Override |
| 280 public void onStreamReady(BidirectionalStream stream) { | 365 public void onStreamReady(BidirectionalStream stream) { |
| 281 try { | 366 try { |
| 282 // Attempt to write data for GET request. | 367 // Attempt to write data for GET request. |
| 283 stream.write(ByteBuffer.wrap("dummy".getBytes()), true); | 368 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) { | 1366 private static String bufferContentsToString(ByteBuffer byteBuffer, int star
t, int end) { |
| 1282 // Use a duplicate to avoid modifying byteBuffer. | 1367 // Use a duplicate to avoid modifying byteBuffer. |
| 1283 ByteBuffer duplicate = byteBuffer.duplicate(); | 1368 ByteBuffer duplicate = byteBuffer.duplicate(); |
| 1284 duplicate.position(start); | 1369 duplicate.position(start); |
| 1285 duplicate.limit(end); | 1370 duplicate.limit(end); |
| 1286 byte[] contents = new byte[duplicate.remaining()]; | 1371 byte[] contents = new byte[duplicate.remaining()]; |
| 1287 duplicate.get(contents); | 1372 duplicate.get(contents); |
| 1288 return new String(contents); | 1373 return new String(contents); |
| 1289 } | 1374 } |
| 1290 } | 1375 } |
| OLD | NEW |