|
24 | 24 | import static org.junit.Assert.fail; |
25 | 25 |
|
26 | 26 | import com.google.cloud.storage.BufferedWritableByteChannelSession.BufferedWritableByteChannel; |
| 27 | +import com.google.cloud.storage.MinFlushBufferedWritableByteChannelTest.OnlyConsumeNBytes; |
27 | 28 | import com.google.cloud.storage.UnbufferedWritableByteChannelSession.UnbufferedWritableByteChannel; |
| 29 | +import com.google.cloud.storage.it.ChecksummedTestContent; |
28 | 30 | import com.google.common.collect.ImmutableList; |
29 | 31 | import java.io.ByteArrayOutputStream; |
30 | 32 | import java.io.IOException; |
@@ -401,6 +403,200 @@ public void close() throws IOException { |
401 | 403 | assertThat(closed.get()).isTrue(); |
402 | 404 | } |
403 | 405 |
|
| 406 | + @Example |
| 407 | + void nonBlockingWrite0DoesNotBlock() throws IOException { |
| 408 | + BufferHandle handle = BufferHandle.allocate(5); |
| 409 | + DefaultBufferedWritableByteChannel c = |
| 410 | + new DefaultBufferedWritableByteChannel(handle, new OnlyConsumeNBytes(0, 1), false); |
| 411 | + |
| 412 | + ChecksummedTestContent all = ChecksummedTestContent.gen(11); |
| 413 | + ByteBuffer s_0_4 = ByteBuffer.wrap(all.slice(0, 4).getBytes()); |
| 414 | + ByteBuffer s_4_4 = ByteBuffer.wrap(all.slice(0, 4).getBytes()); |
| 415 | + ByteBuffer s_8_3 = ByteBuffer.wrap(all.slice(0, 3).getBytes()); |
| 416 | + int written1 = c.write(s_0_4); |
| 417 | + assertThat(written1).isEqualTo(4); |
| 418 | + assertThat(s_0_4.remaining()).isEqualTo(0); |
| 419 | + |
| 420 | + int written2 = c.write(s_4_4); |
| 421 | + assertThat(written2).isEqualTo(0); |
| 422 | + assertThat(s_4_4.remaining()).isEqualTo(4); |
| 423 | + |
| 424 | + int written3 = c.write(s_8_3); |
| 425 | + assertThat(written3).isEqualTo(0); |
| 426 | + assertThat(s_8_3.remaining()).isEqualTo(3); |
| 427 | + |
| 428 | + assertThat(handle.remaining()).isEqualTo(1); |
| 429 | + } |
| 430 | + |
| 431 | + @Example |
| 432 | + void nonBlockingWritePartialDoesNotBlock_withoutBuffering() throws IOException { |
| 433 | + BufferHandle handle = BufferHandle.allocate(4); |
| 434 | + OnlyConsumeNBytes channel = new OnlyConsumeNBytes(4, 4); |
| 435 | + DefaultBufferedWritableByteChannel c = |
| 436 | + new DefaultBufferedWritableByteChannel(handle, channel, false); |
| 437 | + |
| 438 | + ChecksummedTestContent all = ChecksummedTestContent.gen(13); |
| 439 | + ByteBuffer s_0_4 = ByteBuffer.wrap(all.slice(0, 4).getBytes()); |
| 440 | + ByteBuffer s_4_4 = ByteBuffer.wrap(all.slice(4, 4).getBytes()); |
| 441 | + |
| 442 | + // write all 4 bytes |
| 443 | + int written1 = c.write(s_0_4); |
| 444 | + assertThat(written1).isEqualTo(4); |
| 445 | + assertThat(s_0_4.remaining()).isEqualTo(0); |
| 446 | + assertThat(handle.remaining()).isEqualTo(4); |
| 447 | + assertThat(channel.getBytesConsumed()).isEqualTo(4); |
| 448 | + |
| 449 | + // Attempt to write 4 bytes, but 0 will be consumed, break out without consuming any |
| 450 | + int written2 = c.write(s_4_4); |
| 451 | + assertThat(written2).isEqualTo(0); |
| 452 | + assertThat(s_4_4.remaining()).isEqualTo(4); |
| 453 | + assertThat(handle.remaining()).isEqualTo(4); |
| 454 | + assertThat(channel.getBytesConsumed()).isEqualTo(4); |
| 455 | + } |
| 456 | + |
| 457 | + @Example |
| 458 | + void nonBlockingWritePartialDoesNotBlock_withoutBuffering_oversized() throws IOException { |
| 459 | + BufferHandle handle = BufferHandle.allocate(2); |
| 460 | + OnlyConsumeNBytes channel = new OnlyConsumeNBytes(4, 2); |
| 461 | + DefaultBufferedWritableByteChannel c = |
| 462 | + new DefaultBufferedWritableByteChannel(handle, channel, false); |
| 463 | + |
| 464 | + ChecksummedTestContent all = ChecksummedTestContent.gen(13); |
| 465 | + ByteBuffer s_0_4 = ByteBuffer.wrap(all.slice(0, 4).getBytes()); |
| 466 | + ByteBuffer s_4_4 = ByteBuffer.wrap(all.slice(4, 4).getBytes()); |
| 467 | + |
| 468 | + // write all 4 bytes |
| 469 | + int written1 = c.write(s_0_4); |
| 470 | + assertThat(written1).isEqualTo(4); |
| 471 | + assertThat(s_0_4.remaining()).isEqualTo(0); |
| 472 | + assertThat(handle.remaining()).isEqualTo(2); |
| 473 | + assertThat(channel.getBytesConsumed()).isEqualTo(4); |
| 474 | + |
| 475 | + // Attempt to write 4 bytes, but 0 will be consumed, break out without consuming any |
| 476 | + int written2 = c.write(s_4_4); |
| 477 | + assertThat(written2).isEqualTo(0); |
| 478 | + assertThat(s_4_4.remaining()).isEqualTo(4); |
| 479 | + assertThat(handle.remaining()).isEqualTo(2); |
| 480 | + assertThat(channel.getBytesConsumed()).isEqualTo(4); |
| 481 | + } |
| 482 | + |
| 483 | + @Example |
| 484 | + void nonBlockingWritePartialDoesNotBlock_withBuffering() throws IOException { |
| 485 | + BufferHandle handle = BufferHandle.allocate(5); |
| 486 | + OnlyConsumeNBytes channel = new OnlyConsumeNBytes(5, 5); |
| 487 | + DefaultBufferedWritableByteChannel c = |
| 488 | + new DefaultBufferedWritableByteChannel(handle, channel, false); |
| 489 | + |
| 490 | + ChecksummedTestContent all = ChecksummedTestContent.gen(13); |
| 491 | + ByteBuffer s_0_4 = ByteBuffer.wrap(all.slice(0, 4).getBytes()); |
| 492 | + ByteBuffer s_4_4 = ByteBuffer.wrap(all.slice(4, 4).getBytes()); |
| 493 | + ByteBuffer s_8_12 = ByteBuffer.wrap(all.slice(8, 4).getBytes()); |
| 494 | + |
| 495 | + // write all 4 bytes |
| 496 | + int written1 = c.write(s_0_4); |
| 497 | + assertThat(written1).isEqualTo(4); |
| 498 | + assertThat(s_0_4.remaining()).isEqualTo(0); |
| 499 | + assertThat(handle.remaining()).isEqualTo(1); |
| 500 | + assertThat(channel.getBytesConsumed()).isEqualTo(0); |
| 501 | + |
| 502 | + // |
| 503 | + int written2 = c.write(s_4_4); |
| 504 | + assertThat(written2).isEqualTo(4); |
| 505 | + assertThat(s_4_4.remaining()).isEqualTo(0); |
| 506 | + assertThat(handle.remaining()).isEqualTo(2); |
| 507 | + assertThat(channel.getBytesConsumed()).isEqualTo(5); |
| 508 | + |
| 509 | + int written3 = c.write(s_8_12); |
| 510 | + assertThat(written3).isEqualTo(0); |
| 511 | + assertThat(s_8_12.remaining()).isEqualTo(4); |
| 512 | + assertThat(handle.remaining()).isEqualTo(2); |
| 513 | + assertThat(channel.getBytesConsumed()).isEqualTo(5); |
| 514 | + } |
| 515 | + |
| 516 | + @Example |
| 517 | + void nonBlockingWritePartialDoesNotBlock_withBuffering_oversized() throws IOException { |
| 518 | + BufferHandle handle = BufferHandle.allocate(3); |
| 519 | + OnlyConsumeNBytes channel = new OnlyConsumeNBytes(6, 3); |
| 520 | + DefaultBufferedWritableByteChannel c = |
| 521 | + new DefaultBufferedWritableByteChannel(handle, channel, false); |
| 522 | + |
| 523 | + ChecksummedTestContent all = ChecksummedTestContent.gen(13); |
| 524 | + ByteBuffer s_0_4 = ByteBuffer.wrap(all.slice(0, 4).getBytes()); |
| 525 | + ByteBuffer s_4_4 = ByteBuffer.wrap(all.slice(4, 4).getBytes()); |
| 526 | + ByteBuffer s_8_12 = ByteBuffer.wrap(all.slice(8, 4).getBytes()); |
| 527 | + |
| 528 | + // slice 3 bytes and consume them, then enqueue the remaining 1 byte |
| 529 | + int written1_1 = c.write(s_0_4); |
| 530 | + assertThat(written1_1).isEqualTo(4); |
| 531 | + assertThat(s_0_4.remaining()).isEqualTo(0); |
| 532 | + assertThat(handle.remaining()).isEqualTo(2); |
| 533 | + assertThat(channel.getBytesConsumed()).isEqualTo(3); |
| 534 | + |
| 535 | + // write 1 buffered byte and 2 sliced bytes, enqueue 2 remaining |
| 536 | + int written2 = c.write(s_4_4); |
| 537 | + assertThat(written2).isEqualTo(4); |
| 538 | + assertThat(s_4_4.remaining()).isEqualTo(0); |
| 539 | + assertThat(handle.remaining()).isEqualTo(1); |
| 540 | + assertThat(channel.getBytesConsumed()).isEqualTo(6); |
| 541 | + |
| 542 | + // attempt to write 4 bytes, non will be consumed and the buffer should remain the same |
| 543 | + int written3 = c.write(s_8_12); |
| 544 | + assertThat(written3).isEqualTo(0); |
| 545 | + assertThat(s_8_12.remaining()).isEqualTo(4); |
| 546 | + assertThat(handle.remaining()).isEqualTo(1); |
| 547 | + assertThat(channel.getBytesConsumed()).isEqualTo(6); |
| 548 | + } |
| 549 | + |
| 550 | + @Example |
| 551 | + void illegalStateExceptionIfWrittenLt0_slice_eqBuffer() { |
| 552 | + BufferHandle handle = BufferHandle.allocate(4); |
| 553 | + DefaultBufferedWritableByteChannel c = |
| 554 | + new DefaultBufferedWritableByteChannel(handle, new NegativeOneWritableByteChannel(), false); |
| 555 | + |
| 556 | + ChecksummedTestContent all = ChecksummedTestContent.gen(11); |
| 557 | + IllegalStateException ise = |
| 558 | + assertThrows(IllegalStateException.class, () -> c.write(all.slice(0, 4).asByteBuffer())); |
| 559 | + ise.printStackTrace(System.out); |
| 560 | + } |
| 561 | + |
| 562 | + @Example |
| 563 | + void illegalStateExceptionIfWrittenLt0_slice_gtBuffer() { |
| 564 | + BufferHandle handle = BufferHandle.allocate(4); |
| 565 | + DefaultBufferedWritableByteChannel c = |
| 566 | + new DefaultBufferedWritableByteChannel(handle, new NegativeOneWritableByteChannel(), false); |
| 567 | + |
| 568 | + ChecksummedTestContent all = ChecksummedTestContent.gen(11); |
| 569 | + IllegalStateException ise = |
| 570 | + assertThrows(IllegalStateException.class, () -> c.write(all.slice(0, 5).asByteBuffer())); |
| 571 | + ise.printStackTrace(System.out); |
| 572 | + } |
| 573 | + |
| 574 | + @Example |
| 575 | + void illegalStateExceptionIfWrittenLt0_slice_ltBuffer() { |
| 576 | + BufferHandle handle = BufferHandle.allocate(4); |
| 577 | + DefaultBufferedWritableByteChannel c = |
| 578 | + new DefaultBufferedWritableByteChannel(handle, new NegativeOneWritableByteChannel(), false); |
| 579 | + |
| 580 | + ChecksummedTestContent all = ChecksummedTestContent.gen(11); |
| 581 | + IllegalStateException ise = |
| 582 | + assertThrows( |
| 583 | + IllegalStateException.class, |
| 584 | + () -> { |
| 585 | + int written1 = c.write(all.slice(0, 3).asByteBuffer()); |
| 586 | + assertThat(written1).isEqualTo(3); |
| 587 | + c.write(all.slice(3, 3).asByteBuffer()); |
| 588 | + fail("should have errored in previous write call"); |
| 589 | + }); |
| 590 | + ise.printStackTrace(System.out); |
| 591 | + } |
| 592 | + |
| 593 | + @Example |
| 594 | + void test() { |
| 595 | + illegalStateExceptionIfWrittenLt0_slice_eqBuffer(); |
| 596 | + illegalStateExceptionIfWrittenLt0_slice_gtBuffer(); |
| 597 | + illegalStateExceptionIfWrittenLt0_slice_ltBuffer(); |
| 598 | + } |
| 599 | + |
404 | 600 | @Property |
405 | 601 | void bufferAllocationShouldOnlyHappenWhenNeeded(@ForAll("BufferSizes") WriteOps writeOps) |
406 | 602 | throws IOException { |
@@ -697,4 +893,20 @@ public ByteBuffer get() { |
697 | 893 | return delegate.get(); |
698 | 894 | } |
699 | 895 | } |
| 896 | + |
| 897 | + private static class NegativeOneWritableByteChannel implements UnbufferedWritableByteChannel { |
| 898 | + |
| 899 | + @Override |
| 900 | + public long write(ByteBuffer[] srcs, int offset, int length) { |
| 901 | + return -1; |
| 902 | + } |
| 903 | + |
| 904 | + @Override |
| 905 | + public boolean isOpen() { |
| 906 | + return true; |
| 907 | + } |
| 908 | + |
| 909 | + @Override |
| 910 | + public void close() {} |
| 911 | + } |
700 | 912 | } |
0 commit comments