Make tests on composite buffers pass again

This commit is contained in:
Chris Vest 2021-04-28 12:33:40 +02:00
parent e1cc445d11
commit 6506e29569
2 changed files with 48 additions and 49 deletions

View File

@ -201,7 +201,7 @@ public final class CompositeBuffer extends RcSupport<Buffer, CompositeBuffer> im
if (ise != null) { if (ise != null) {
throw ise; throw ise;
} }
return compose(allocator, bufs); return new CompositeBuffer(allocator, filterExternalBufs(Arrays.stream(bufs)), COMPOSITE_DROP, false);
} }
/** /**

View File

@ -61,11 +61,11 @@ public class BufferCompositionTest extends BufferTestSupport {
var e = assertThrows(IllegalArgumentException.class, () -> CompositeBuffer.compose(allocator, a, a)); var e = assertThrows(IllegalArgumentException.class, () -> CompositeBuffer.compose(allocator, a, a));
assertThat(e).hasMessageContaining("duplicate"); assertThat(e).hasMessageContaining("duplicate");
try (Buffer composite = CompositeBuffer.compose(allocator, a)) { try (CompositeBuffer composite = CompositeBuffer.compose(allocator, a)) {
a.close(); a.close();
try { try {
e = assertThrows(IllegalArgumentException.class, e = assertThrows(IllegalArgumentException.class,
() -> ((CompositeBuffer) composite).extendWith(a)); () -> composite.extendWith(a));
assertThat(e).hasMessageContaining("duplicate"); assertThat(e).hasMessageContaining("duplicate");
} finally { } finally {
a.acquire(); a.acquire();
@ -90,19 +90,19 @@ public class BufferCompositionTest extends BufferTestSupport {
public void compositeBufferMustNotBeAllowedToContainThemselves() { public void compositeBufferMustNotBeAllowedToContainThemselves() {
try (BufferAllocator allocator = BufferAllocator.heap()) { try (BufferAllocator allocator = BufferAllocator.heap()) {
Buffer a = allocator.allocate(4); Buffer a = allocator.allocate(4);
Buffer buf = CompositeBuffer.compose(allocator, a); CompositeBuffer buf = CompositeBuffer.compose(allocator, a);
try (buf; a) { try (buf; a) {
a.close(); a.close();
try { try {
assertThrows(IllegalArgumentException.class, () -> ((CompositeBuffer) buf).extendWith(buf)); assertThrows(IllegalArgumentException.class, () -> buf.extendWith(buf));
assertTrue(buf.isOwned()); assertTrue(buf.isOwned());
try (Buffer composite = CompositeBuffer.compose(allocator, buf)) { try (Buffer composite = CompositeBuffer.compose(allocator, buf)) {
// the composing increments the reference count of constituent buffers... // the composing increments the reference count of constituent buffers...
// counter-act this so it can be extended: // counter-act this, so it can be extended:
a.close(); // buf is now owned so it can be extended. a.close(); // buf is now owned, so it can be extended.
try { try {
assertThrows(ClassCastException.class, assertThrows(IllegalArgumentException.class,
() -> ((CompositeBuffer) buf).extendWith(composite)); () -> buf.extendWith(composite));
} finally { } finally {
a.acquire(); // restore the reference count to align with our try-with-resources structure. a.acquire(); // restore the reference count to align with our try-with-resources structure.
} }
@ -162,8 +162,7 @@ public class BufferCompositionTest extends BufferTestSupport {
try (BufferAllocator allocator = BufferAllocator.heap(); try (BufferAllocator allocator = BufferAllocator.heap();
Buffer a = allocator.allocate(8); Buffer a = allocator.allocate(8);
Buffer b = allocator.allocate(8)) { Buffer b = allocator.allocate(8)) {
var exc = assertThrows(IllegalArgumentException.class, () -> ((CompositeBuffer) a).extendWith(b)); assertThrows(ClassCastException.class, () -> ((CompositeBuffer) a).extendWith(b));
assertThat(exc).hasMessageContaining("Expected").hasMessageContaining("composite");
} }
} }
@ -172,9 +171,9 @@ public class BufferCompositionTest extends BufferTestSupport {
try (BufferAllocator allocator = BufferAllocator.heap(); try (BufferAllocator allocator = BufferAllocator.heap();
Buffer a = allocator.allocate(8); Buffer a = allocator.allocate(8);
Buffer b = allocator.allocate(8); Buffer b = allocator.allocate(8);
Buffer composed = CompositeBuffer.compose(allocator, a)) { CompositeBuffer composed = CompositeBuffer.compose(allocator, a)) {
try (Buffer ignore = composed.acquire()) { try (Buffer ignore = composed.acquire()) {
var exc = assertThrows(IllegalStateException.class, () -> ((CompositeBuffer) composed).extendWith(b)); var exc = assertThrows(IllegalStateException.class, () -> composed.extendWith(b));
assertThat(exc).hasMessageContaining("owned"); assertThat(exc).hasMessageContaining("owned");
} }
} }
@ -183,13 +182,13 @@ public class BufferCompositionTest extends BufferTestSupport {
@Test @Test
public void extendingCompositeBufferWithItselfMustThrow() { public void extendingCompositeBufferWithItselfMustThrow() {
try (BufferAllocator allocator = BufferAllocator.heap()) { try (BufferAllocator allocator = BufferAllocator.heap()) {
Buffer composite; CompositeBuffer composite;
try (Buffer a = allocator.allocate(8)) { try (Buffer a = allocator.allocate(8)) {
composite = CompositeBuffer.compose(allocator, a); composite = CompositeBuffer.compose(allocator, a);
} }
try (composite) { try (composite) {
var exc = assertThrows(IllegalArgumentException.class, var exc = assertThrows(IllegalArgumentException.class,
() -> ((CompositeBuffer) composite).extendWith(composite)); () -> composite.extendWith(composite));
assertThat(exc).hasMessageContaining("cannot be extended"); assertThat(exc).hasMessageContaining("cannot be extended");
} }
} }
@ -198,20 +197,20 @@ public class BufferCompositionTest extends BufferTestSupport {
@Test @Test
public void extendingWithZeroCapacityBufferHasNoEffect() { public void extendingWithZeroCapacityBufferHasNoEffect() {
try (BufferAllocator allocator = BufferAllocator.heap(); try (BufferAllocator allocator = BufferAllocator.heap();
Buffer composite = CompositeBuffer.compose(allocator)) { CompositeBuffer composite = CompositeBuffer.compose(allocator)) {
((CompositeBuffer) composite).extendWith(composite); composite.extendWith(composite);
assertThat(composite.capacity()).isZero(); assertThat(composite.capacity()).isZero();
assertThat(composite.countComponents()).isZero(); assertThat(composite.countComponents()).isZero();
} }
try (BufferAllocator allocator = BufferAllocator.heap()) { try (BufferAllocator allocator = BufferAllocator.heap()) {
Buffer a = allocator.allocate(1); Buffer a = allocator.allocate(1);
Buffer composite = CompositeBuffer.compose(allocator, a); CompositeBuffer composite = CompositeBuffer.compose(allocator, a);
a.close(); a.close();
assertTrue(composite.isOwned()); assertTrue(composite.isOwned());
assertThat(composite.capacity()).isOne(); assertThat(composite.capacity()).isOne();
assertThat(composite.countComponents()).isOne(); assertThat(composite.countComponents()).isOne();
try (Buffer b = CompositeBuffer.compose(allocator)) { try (Buffer b = CompositeBuffer.compose(allocator)) {
((CompositeBuffer) composite).extendWith(b); composite.extendWith(b);
} }
assertTrue(composite.isOwned()); assertTrue(composite.isOwned());
assertThat(composite.capacity()).isOne(); assertThat(composite.capacity()).isOne();
@ -222,18 +221,18 @@ public class BufferCompositionTest extends BufferTestSupport {
@Test @Test
public void extendingCompositeBufferWithNullMustThrow() { public void extendingCompositeBufferWithNullMustThrow() {
try (BufferAllocator allocator = BufferAllocator.heap(); try (BufferAllocator allocator = BufferAllocator.heap();
Buffer composite = CompositeBuffer.compose(allocator)) { CompositeBuffer composite = CompositeBuffer.compose(allocator)) {
assertThrows(NullPointerException.class, () -> ((CompositeBuffer) composite).extendWith(null)); assertThrows(NullPointerException.class, () -> composite.extendWith(null));
} }
} }
@Test @Test
public void extendingCompositeBufferMustIncreaseCapacityByGivenBigEndianBuffer() { public void extendingCompositeBufferMustIncreaseCapacityByGivenBigEndianBuffer() {
try (BufferAllocator allocator = BufferAllocator.heap(); try (BufferAllocator allocator = BufferAllocator.heap();
Buffer composite = CompositeBuffer.compose(allocator)) { CompositeBuffer composite = CompositeBuffer.compose(allocator)) {
assertThat(composite.capacity()).isZero(); assertThat(composite.capacity()).isZero();
try (Buffer buf = allocator.allocate(8, BIG_ENDIAN)) { try (Buffer buf = allocator.allocate(8, BIG_ENDIAN)) {
((CompositeBuffer) composite).extendWith(buf); composite.extendWith(buf);
} }
assertThat(composite.capacity()).isEqualTo(8); assertThat(composite.capacity()).isEqualTo(8);
composite.writeLong(0x0102030405060708L); composite.writeLong(0x0102030405060708L);
@ -244,10 +243,10 @@ public class BufferCompositionTest extends BufferTestSupport {
@Test @Test
public void extendingCompositeBufferMustIncreaseCapacityByGivenLittleEndianBuffer() { public void extendingCompositeBufferMustIncreaseCapacityByGivenLittleEndianBuffer() {
try (BufferAllocator allocator = BufferAllocator.heap(); try (BufferAllocator allocator = BufferAllocator.heap();
Buffer composite = CompositeBuffer.compose(allocator)) { CompositeBuffer composite = CompositeBuffer.compose(allocator)) {
assertThat(composite.capacity()).isZero(); assertThat(composite.capacity()).isZero();
try (Buffer buf = allocator.allocate(8, LITTLE_ENDIAN)) { try (Buffer buf = allocator.allocate(8, LITTLE_ENDIAN)) {
((CompositeBuffer) composite).extendWith(buf); composite.extendWith(buf);
} }
assertThat(composite.capacity()).isEqualTo(8); assertThat(composite.capacity()).isEqualTo(8);
composite.writeLong(0x0102030405060708L); composite.writeLong(0x0102030405060708L);
@ -258,14 +257,14 @@ public class BufferCompositionTest extends BufferTestSupport {
@Test @Test
public void extendingBigEndianCompositeBufferMustThrowIfExtensionIsLittleEndian() { public void extendingBigEndianCompositeBufferMustThrowIfExtensionIsLittleEndian() {
try (BufferAllocator allocator = BufferAllocator.heap()) { try (BufferAllocator allocator = BufferAllocator.heap()) {
Buffer composite; CompositeBuffer composite;
try (Buffer a = allocator.allocate(8, BIG_ENDIAN)) { try (Buffer a = allocator.allocate(8, BIG_ENDIAN)) {
composite = CompositeBuffer.compose(allocator, a); composite = CompositeBuffer.compose(allocator, a);
} }
try (composite) { try (composite) {
try (Buffer b = allocator.allocate(8, LITTLE_ENDIAN)) { try (Buffer b = allocator.allocate(8, LITTLE_ENDIAN)) {
var exc = assertThrows(IllegalArgumentException.class, var exc = assertThrows(IllegalArgumentException.class,
() -> ((CompositeBuffer) composite).extendWith(b)); () -> composite.extendWith(b));
assertThat(exc).hasMessageContaining("byte order"); assertThat(exc).hasMessageContaining("byte order");
} }
} }
@ -275,14 +274,14 @@ public class BufferCompositionTest extends BufferTestSupport {
@Test @Test
public void extendingLittleEndianCompositeBufferMustThrowIfExtensionIsBigEndian() { public void extendingLittleEndianCompositeBufferMustThrowIfExtensionIsBigEndian() {
try (BufferAllocator allocator = BufferAllocator.heap()) { try (BufferAllocator allocator = BufferAllocator.heap()) {
Buffer composite; CompositeBuffer composite;
try (Buffer a = allocator.allocate(8, LITTLE_ENDIAN)) { try (Buffer a = allocator.allocate(8, LITTLE_ENDIAN)) {
composite = CompositeBuffer.compose(allocator, a); composite = CompositeBuffer.compose(allocator, a);
} }
try (composite) { try (composite) {
try (Buffer b = allocator.allocate(8, BIG_ENDIAN)) { try (Buffer b = allocator.allocate(8, BIG_ENDIAN)) {
var exc = assertThrows(IllegalArgumentException.class, var exc = assertThrows(IllegalArgumentException.class,
() -> ((CompositeBuffer) composite).extendWith(b)); () -> composite.extendWith(b));
assertThat(exc).hasMessageContaining("byte order"); assertThat(exc).hasMessageContaining("byte order");
} }
} }
@ -292,9 +291,9 @@ public class BufferCompositionTest extends BufferTestSupport {
@Test @Test
public void emptyCompositeBufferMustAllowExtendingWithBufferWithBigEndianByteOrder() { public void emptyCompositeBufferMustAllowExtendingWithBufferWithBigEndianByteOrder() {
try (BufferAllocator allocator = BufferAllocator.heap()) { try (BufferAllocator allocator = BufferAllocator.heap()) {
try (Buffer composite = CompositeBuffer.compose(allocator)) { try (CompositeBuffer composite = CompositeBuffer.compose(allocator)) {
try (Buffer b = allocator.allocate(8, BIG_ENDIAN)) { try (Buffer b = allocator.allocate(8, BIG_ENDIAN)) {
((CompositeBuffer) composite).extendWith(b); composite.extendWith(b);
assertThat(composite.order()).isEqualTo(BIG_ENDIAN); assertThat(composite.order()).isEqualTo(BIG_ENDIAN);
} }
} }
@ -304,9 +303,9 @@ public class BufferCompositionTest extends BufferTestSupport {
@Test @Test
public void emptyCompositeBufferMustAllowExtendingWithBufferWithLittleEndianByteOrder() { public void emptyCompositeBufferMustAllowExtendingWithBufferWithLittleEndianByteOrder() {
try (BufferAllocator allocator = BufferAllocator.heap()) { try (BufferAllocator allocator = BufferAllocator.heap()) {
try (Buffer composite = CompositeBuffer.compose(allocator)) { try (CompositeBuffer composite = CompositeBuffer.compose(allocator)) {
try (Buffer b = allocator.allocate(8, LITTLE_ENDIAN)) { try (Buffer b = allocator.allocate(8, LITTLE_ENDIAN)) {
((CompositeBuffer) composite).extendWith(b); composite.extendWith(b);
assertThat(composite.order()).isEqualTo(LITTLE_ENDIAN); assertThat(composite.order()).isEqualTo(LITTLE_ENDIAN);
} }
} }
@ -316,9 +315,9 @@ public class BufferCompositionTest extends BufferTestSupport {
@Test @Test
public void emptyCompositeBufferMustAllowExtendingWithReadOnlyBuffer() { public void emptyCompositeBufferMustAllowExtendingWithReadOnlyBuffer() {
try (BufferAllocator allocator = BufferAllocator.heap()) { try (BufferAllocator allocator = BufferAllocator.heap()) {
try (Buffer composite = CompositeBuffer.compose(allocator)) { try (CompositeBuffer composite = CompositeBuffer.compose(allocator)) {
try (Buffer b = allocator.allocate(8).readOnly(true)) { try (Buffer b = allocator.allocate(8).readOnly(true)) {
((CompositeBuffer) composite).extendWith(b); composite.extendWith(b);
assertTrue(composite.readOnly()); assertTrue(composite.readOnly());
} }
} }
@ -328,7 +327,7 @@ public class BufferCompositionTest extends BufferTestSupport {
@Test @Test
public void whenExtendingCompositeBufferWithWriteOffsetAtCapacityExtensionWriteOffsetCanBeNonZero() { public void whenExtendingCompositeBufferWithWriteOffsetAtCapacityExtensionWriteOffsetCanBeNonZero() {
try (BufferAllocator allocator = BufferAllocator.heap()) { try (BufferAllocator allocator = BufferAllocator.heap()) {
Buffer composite; CompositeBuffer composite;
try (Buffer a = allocator.allocate(8)) { try (Buffer a = allocator.allocate(8)) {
composite = CompositeBuffer.compose(allocator, a); composite = CompositeBuffer.compose(allocator, a);
} }
@ -336,7 +335,7 @@ public class BufferCompositionTest extends BufferTestSupport {
composite.writeLong(0); composite.writeLong(0);
try (Buffer b = allocator.allocate(8)) { try (Buffer b = allocator.allocate(8)) {
b.writeInt(1); b.writeInt(1);
((CompositeBuffer) composite).extendWith(b); composite.extendWith(b);
assertThat(composite.capacity()).isEqualTo(16); assertThat(composite.capacity()).isEqualTo(16);
assertThat(composite.writerOffset()).isEqualTo(12); assertThat(composite.writerOffset()).isEqualTo(12);
} }
@ -347,7 +346,7 @@ public class BufferCompositionTest extends BufferTestSupport {
@Test @Test
public void whenExtendingCompositeBufferWithWriteOffsetLessThanCapacityExtensionWriteOffsetMustZero() { public void whenExtendingCompositeBufferWithWriteOffsetLessThanCapacityExtensionWriteOffsetMustZero() {
try (BufferAllocator allocator = BufferAllocator.heap()) { try (BufferAllocator allocator = BufferAllocator.heap()) {
Buffer composite; CompositeBuffer composite;
try (Buffer a = allocator.allocate(8)) { try (Buffer a = allocator.allocate(8)) {
composite = CompositeBuffer.compose(allocator, a); composite = CompositeBuffer.compose(allocator, a);
} }
@ -356,10 +355,10 @@ public class BufferCompositionTest extends BufferTestSupport {
try (Buffer b = allocator.allocate(8)) { try (Buffer b = allocator.allocate(8)) {
b.writeInt(1); b.writeInt(1);
var exc = assertThrows(IllegalArgumentException.class, var exc = assertThrows(IllegalArgumentException.class,
() -> ((CompositeBuffer) composite).extendWith(b)); () -> composite.extendWith(b));
assertThat(exc).hasMessageContaining("unwritten gap"); assertThat(exc).hasMessageContaining("unwritten gap");
b.writerOffset(0); b.writerOffset(0);
((CompositeBuffer) composite).extendWith(b); composite.extendWith(b);
assertThat(composite.capacity()).isEqualTo(16); assertThat(composite.capacity()).isEqualTo(16);
assertThat(composite.writerOffset()).isEqualTo(4); assertThat(composite.writerOffset()).isEqualTo(4);
} }
@ -370,7 +369,7 @@ public class BufferCompositionTest extends BufferTestSupport {
@Test @Test
public void whenExtendingCompositeBufferWithReadOffsetAtCapacityExtensionReadOffsetCanBeNonZero() { public void whenExtendingCompositeBufferWithReadOffsetAtCapacityExtensionReadOffsetCanBeNonZero() {
try (BufferAllocator allocator = BufferAllocator.heap()) { try (BufferAllocator allocator = BufferAllocator.heap()) {
Buffer composite; CompositeBuffer composite;
try (Buffer a = allocator.allocate(8)) { try (Buffer a = allocator.allocate(8)) {
composite = CompositeBuffer.compose(allocator, a); composite = CompositeBuffer.compose(allocator, a);
} }
@ -380,7 +379,7 @@ public class BufferCompositionTest extends BufferTestSupport {
try (Buffer b = allocator.allocate(8)) { try (Buffer b = allocator.allocate(8)) {
b.writeInt(1); b.writeInt(1);
b.readInt(); b.readInt();
((CompositeBuffer) composite).extendWith(b); composite.extendWith(b);
assertThat(composite.capacity()).isEqualTo(16); assertThat(composite.capacity()).isEqualTo(16);
assertThat(composite.writerOffset()).isEqualTo(12); assertThat(composite.writerOffset()).isEqualTo(12);
} }
@ -391,7 +390,7 @@ public class BufferCompositionTest extends BufferTestSupport {
@Test @Test
public void whenExtendingCompositeBufferWithReadOffsetLessThanCapacityExtensionReadOffsetMustZero() { public void whenExtendingCompositeBufferWithReadOffsetLessThanCapacityExtensionReadOffsetMustZero() {
try (BufferAllocator allocator = BufferAllocator.heap()) { try (BufferAllocator allocator = BufferAllocator.heap()) {
Buffer composite; CompositeBuffer composite;
try (Buffer a = allocator.allocate(8)) { try (Buffer a = allocator.allocate(8)) {
composite = CompositeBuffer.compose(allocator, a); composite = CompositeBuffer.compose(allocator, a);
} }
@ -402,10 +401,10 @@ public class BufferCompositionTest extends BufferTestSupport {
b.writeInt(1); b.writeInt(1);
b.readInt(); b.readInt();
var exc = assertThrows(IllegalArgumentException.class, var exc = assertThrows(IllegalArgumentException.class,
() -> ((CompositeBuffer) composite).extendWith(b)); () -> composite.extendWith(b));
assertThat(exc).hasMessageContaining("unread gap"); assertThat(exc).hasMessageContaining("unread gap");
b.readerOffset(0); b.readerOffset(0);
((CompositeBuffer) composite).extendWith(b); composite.extendWith(b);
assertThat(composite.capacity()).isEqualTo(16); assertThat(composite.capacity()).isEqualTo(16);
assertThat(composite.writerOffset()).isEqualTo(12); assertThat(composite.writerOffset()).isEqualTo(12);
assertThat(composite.readerOffset()).isEqualTo(4); assertThat(composite.readerOffset()).isEqualTo(4);
@ -449,12 +448,12 @@ public class BufferCompositionTest extends BufferTestSupport {
@Test @Test
public void compositeWritableBufferCannotBeExtendedWithReadOnlyBuffer() { public void compositeWritableBufferCannotBeExtendedWithReadOnlyBuffer() {
try (BufferAllocator allocator = BufferAllocator.heap()) { try (BufferAllocator allocator = BufferAllocator.heap()) {
Buffer composite; CompositeBuffer composite;
try (Buffer a = allocator.allocate(8)) { try (Buffer a = allocator.allocate(8)) {
composite = CompositeBuffer.compose(allocator, a); composite = CompositeBuffer.compose(allocator, a);
} }
try (composite; Buffer b = allocator.allocate(8).readOnly(true)) { try (composite; Buffer b = allocator.allocate(8).readOnly(true)) {
assertThrows(IllegalArgumentException.class, () -> ((CompositeBuffer) composite).extendWith(b)); assertThrows(IllegalArgumentException.class, () -> composite.extendWith(b));
} }
} }
} }
@ -462,12 +461,12 @@ public class BufferCompositionTest extends BufferTestSupport {
@Test @Test
public void compositeReadOnlyBufferCannotBeExtendedWithWritableBuffer() { public void compositeReadOnlyBufferCannotBeExtendedWithWritableBuffer() {
try (BufferAllocator allocator = BufferAllocator.heap()) { try (BufferAllocator allocator = BufferAllocator.heap()) {
Buffer composite; CompositeBuffer composite;
try (Buffer a = allocator.allocate(8).readOnly(true)) { try (Buffer a = allocator.allocate(8).readOnly(true)) {
composite = CompositeBuffer.compose(allocator, a); composite = CompositeBuffer.compose(allocator, a);
} }
try (composite; Buffer b = allocator.allocate(8)) { try (composite; Buffer b = allocator.allocate(8)) {
assertThrows(IllegalArgumentException.class, () -> ((CompositeBuffer) composite).extendWith(b)); assertThrows(IllegalArgumentException.class, () -> composite.extendWith(b));
} }
} }
} }