From 0ccb34ca0801b0f65ec344b055d68d31247620f1 Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Tue, 16 Mar 2021 12:11:29 +0100 Subject: [PATCH] Fix failing ByteBufAdaptorTests and increase adaptor compatibility --- .../java/io/netty/buffer/api/RcSupport.java | 2 +- .../buffer/api/adaptor/ByteBufAdaptor.java | 68 +++++-- .../api/adaptor/ByteBufAdaptorTest.java | 174 ------------------ 3 files changed, 56 insertions(+), 188 deletions(-) diff --git a/src/main/java/io/netty/buffer/api/RcSupport.java b/src/main/java/io/netty/buffer/api/RcSupport.java index 48ddbaf..99a4310 100644 --- a/src/main/java/io/netty/buffer/api/RcSupport.java +++ b/src/main/java/io/netty/buffer/api/RcSupport.java @@ -98,7 +98,7 @@ public abstract class RcSupport, T extends RcSupport> impl */ protected IllegalStateException notSendableException() { return new IllegalStateException( - "Cannot send() a reference counted object with " + acquires + " outstanding acquires: " + this + '.'); + "Cannot send() a reference counted object with " + countBorrows() + " borrows: " + this + '.'); } @Override diff --git a/src/main/java/io/netty/buffer/api/adaptor/ByteBufAdaptor.java b/src/main/java/io/netty/buffer/api/adaptor/ByteBufAdaptor.java index 21dae15..fa0ec00 100644 --- a/src/main/java/io/netty/buffer/api/adaptor/ByteBufAdaptor.java +++ b/src/main/java/io/netty/buffer/api/adaptor/ByteBufAdaptor.java @@ -19,6 +19,8 @@ import io.netty.buffer.ByteBufConvertible; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufUtil; +import io.netty.buffer.DuplicatedByteBuf; +import io.netty.buffer.SlicedByteBuf; import io.netty.buffer.Unpooled; import io.netty.buffer.api.Buffer; import io.netty.buffer.api.BufferAllocator; @@ -957,8 +959,8 @@ public final class ByteBufAdaptor extends ByteBuf { @Override public ByteBuf readSlice(int length) { - ByteBuf slice = readRetainedSlice(length); - release(); + ByteBuf slice = slice(readerIndex(), length); + buffer.readerOffset(buffer.readerOffset() + length); return slice; } @@ -1411,22 +1413,18 @@ public final class ByteBufAdaptor extends ByteBuf { @Override public ByteBuf slice() { - ByteBuf slice = retainedSlice(); - release(); - return slice; + return slice(readerIndex(), readableBytes()); } @Override public ByteBuf retainedSlice() { - checkAccess(); - return wrap(buffer.slice()); + return retainedSlice(readerIndex(), readableBytes()); } @Override public ByteBuf slice(int index, int length) { - ByteBuf slice = retainedSlice(index, length); - release(); - return slice; + checkAccess(); + return new Slice(this, index, length); } @Override @@ -1439,11 +1437,55 @@ public final class ByteBufAdaptor extends ByteBuf { } } + private static final class Slice extends SlicedByteBuf { + private final int indexAdjustment; + private final int lengthAdjustment; + + Slice(ByteBuf buffer, int index, int length) { + super(buffer, index, length); + indexAdjustment = index; + lengthAdjustment = length; + } + + @Override + public ByteBuf retainedDuplicate() { + return new Slice(unwrap().retainedDuplicate(), indexAdjustment, lengthAdjustment); + } + + @Override + public ByteBuf retainedSlice(int index, int length) { + checkIndex(index, length); + return unwrap().retainedSlice(indexAdjustment + index, length); + } + } + + private static final class Duplicate extends DuplicatedByteBuf { + Duplicate(ByteBufAdaptor byteBuf) { + super(byteBuf); + } + + @Override + public ByteBuf duplicate() { + ((ByteBufAdaptor) unwrap()).checkAccess(); + return new Duplicate((ByteBufAdaptor) unwrap()); + } + + @Override + public ByteBuf retainedDuplicate() { + return unwrap().retainedDuplicate(); + } + + @Override + public ByteBuf retainedSlice(int index, int length) { + return unwrap().retainedSlice(index, length); + } + } + @Override public ByteBuf duplicate() { - ByteBuf duplicate = retainedDuplicate(); - release(); - return duplicate; + checkAccess(); + Duplicate duplicatedByteBuf = new Duplicate(this); + return duplicatedByteBuf.setIndex(readerIndex(), writerIndex()); } @Override diff --git a/src/test/java/io/netty/buffer/api/adaptor/ByteBufAdaptorTest.java b/src/test/java/io/netty/buffer/api/adaptor/ByteBufAdaptorTest.java index 16d994e..fd15e0b 100644 --- a/src/test/java/io/netty/buffer/api/adaptor/ByteBufAdaptorTest.java +++ b/src/test/java/io/netty/buffer/api/adaptor/ByteBufAdaptorTest.java @@ -39,36 +39,6 @@ public class ByteBufAdaptorTest extends AbstractByteBufTest { return alloc.buffer(capacity, capacity); } - @Ignore("New buffers not thread-safe like this.") - @Override - public void testSliceReadGatheringByteChannelMultipleThreads() throws Exception { - } - - @Ignore("New buffers not thread-safe like this.") - @Override - public void testDuplicateReadGatheringByteChannelMultipleThreads() throws Exception { - } - - @Ignore("New buffers not thread-safe like this.") - @Override - public void testSliceReadOutputStreamMultipleThreads() throws Exception { - } - - @Ignore("New buffers not thread-safe like this.") - @Override - public void testDuplicateReadOutputStreamMultipleThreads() throws Exception { - } - - @Ignore("New buffers not thread-safe like this.") - @Override - public void testSliceBytesInArrayMultipleThreads() throws Exception { - } - - @Ignore("New buffers not thread-safe like this.") - @Override - public void testDuplicateBytesInArrayMultipleThreads() throws Exception { - } - @Ignore("This test codifies that asking to reading 0 bytes from an empty but unclosed stream should return -1, " + "which is just weird.") @Override @@ -112,152 +82,8 @@ public class ByteBufAdaptorTest extends AbstractByteBufTest { public void testToByteBuffer2() { } - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testRetainedDuplicateUnreleasable3() { - } - - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testRetainedDuplicateUnreleasable4() { - } - - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testRetainedDuplicateAndRetainedSliceContentIsExpected() { - } - - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testMultipleRetainedSliceReleaseOriginal2() { - } - - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testMultipleRetainedSliceReleaseOriginal3() { - } - - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testMultipleRetainedSliceReleaseOriginal4() { - } - - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testReadRetainedSliceUnreleasable3() { - } - - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testReadRetainedSliceUnreleasable4() { - } - - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testRetainedSliceUnreleasable3() { - } - - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testRetainedSliceUnreleasable4() { - } - - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testRetainedSliceReleaseOriginal2() { - } - - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testRetainedSliceReleaseOriginal3() { - } - - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testRetainedSliceReleaseOriginal4() { - } - - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testMultipleRetainedDuplicateReleaseOriginal2() { - } - - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testMultipleRetainedDuplicateReleaseOriginal3() { - } - - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testMultipleRetainedDuplicateReleaseOriginal4() { - } - - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testRetainedDuplicateReleaseOriginal2() { - } - - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testRetainedDuplicateReleaseOriginal3() { - } - - @Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " + - "independent reference counts now. Also, this plays tricks with reference that we cannot support.") - @Override - public void testRetainedDuplicateReleaseOriginal4() { - } - @Ignore("No longer allowed to allocate 0 sized buffers, except for composite buffers with no components.") @Override public void testLittleEndianWithExpand() { } - - @Ignore("Test seems to inherently have double-free bug?") - @Override - public void testRetainedSliceAfterReleaseRetainedSliceDuplicate() { - } - - @Ignore("Test seems to inherently have double-free bug?") - @Override - public void testRetainedSliceAfterReleaseRetainedDuplicateSlice() { - } - - @Ignore("Test seems to inherently have double-free bug?") - @Override - public void testSliceAfterReleaseRetainedSliceDuplicate() { - } - - @Ignore("Test seems to inherently have double-free bug?") - @Override - public void testDuplicateAfterReleaseRetainedSliceDuplicate() { - } - - @Ignore("Test seems to inherently have double-free bug?") - @Override - public void testDuplicateAfterReleaseRetainedDuplicateSlice() { - } - - @Ignore("Test seems to inherently have double-free bug?") - @Override - public void testSliceAfterReleaseRetainedDuplicateSlice() { - } }