Skip to content
This repository was archived by the owner on Dec 12, 2022. It is now read-only.

Commit 0ccb34c

Browse files
committed
Fix failing ByteBufAdaptorTests and increase adaptor compatibility
1 parent 1f4234d commit 0ccb34c

File tree

3 files changed

+56
-188
lines changed

3 files changed

+56
-188
lines changed

src/main/java/io/netty/buffer/api/RcSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ protected <E extends Throwable> E attachTrace(E throwable) {
9898
*/
9999
protected IllegalStateException notSendableException() {
100100
return new IllegalStateException(
101-
"Cannot send() a reference counted object with " + acquires + " outstanding acquires: " + this + '.');
101+
"Cannot send() a reference counted object with " + countBorrows() + " borrows: " + this + '.');
102102
}
103103

104104
@Override

src/main/java/io/netty/buffer/api/adaptor/ByteBufAdaptor.java

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import io.netty.buffer.ByteBuf;
2020
import io.netty.buffer.ByteBufAllocator;
2121
import io.netty.buffer.ByteBufUtil;
22+
import io.netty.buffer.DuplicatedByteBuf;
23+
import io.netty.buffer.SlicedByteBuf;
2224
import io.netty.buffer.Unpooled;
2325
import io.netty.buffer.api.Buffer;
2426
import io.netty.buffer.api.BufferAllocator;
@@ -957,8 +959,8 @@ public ByteBuf readBytes(int length) {
957959

958960
@Override
959961
public ByteBuf readSlice(int length) {
960-
ByteBuf slice = readRetainedSlice(length);
961-
release();
962+
ByteBuf slice = slice(readerIndex(), length);
963+
buffer.readerOffset(buffer.readerOffset() + length);
962964
return slice;
963965
}
964966

@@ -1411,22 +1413,18 @@ public ByteBuf copy(int index, int length) {
14111413

14121414
@Override
14131415
public ByteBuf slice() {
1414-
ByteBuf slice = retainedSlice();
1415-
release();
1416-
return slice;
1416+
return slice(readerIndex(), readableBytes());
14171417
}
14181418

14191419
@Override
14201420
public ByteBuf retainedSlice() {
1421-
checkAccess();
1422-
return wrap(buffer.slice());
1421+
return retainedSlice(readerIndex(), readableBytes());
14231422
}
14241423

14251424
@Override
14261425
public ByteBuf slice(int index, int length) {
1427-
ByteBuf slice = retainedSlice(index, length);
1428-
release();
1429-
return slice;
1426+
checkAccess();
1427+
return new Slice(this, index, length);
14301428
}
14311429

14321430
@Override
@@ -1439,11 +1437,55 @@ public ByteBuf retainedSlice(int index, int length) {
14391437
}
14401438
}
14411439

1440+
private static final class Slice extends SlicedByteBuf {
1441+
private final int indexAdjustment;
1442+
private final int lengthAdjustment;
1443+
1444+
Slice(ByteBuf buffer, int index, int length) {
1445+
super(buffer, index, length);
1446+
indexAdjustment = index;
1447+
lengthAdjustment = length;
1448+
}
1449+
1450+
@Override
1451+
public ByteBuf retainedDuplicate() {
1452+
return new Slice(unwrap().retainedDuplicate(), indexAdjustment, lengthAdjustment);
1453+
}
1454+
1455+
@Override
1456+
public ByteBuf retainedSlice(int index, int length) {
1457+
checkIndex(index, length);
1458+
return unwrap().retainedSlice(indexAdjustment + index, length);
1459+
}
1460+
}
1461+
1462+
private static final class Duplicate extends DuplicatedByteBuf {
1463+
Duplicate(ByteBufAdaptor byteBuf) {
1464+
super(byteBuf);
1465+
}
1466+
1467+
@Override
1468+
public ByteBuf duplicate() {
1469+
((ByteBufAdaptor) unwrap()).checkAccess();
1470+
return new Duplicate((ByteBufAdaptor) unwrap());
1471+
}
1472+
1473+
@Override
1474+
public ByteBuf retainedDuplicate() {
1475+
return unwrap().retainedDuplicate();
1476+
}
1477+
1478+
@Override
1479+
public ByteBuf retainedSlice(int index, int length) {
1480+
return unwrap().retainedSlice(index, length);
1481+
}
1482+
}
1483+
14421484
@Override
14431485
public ByteBuf duplicate() {
1444-
ByteBuf duplicate = retainedDuplicate();
1445-
release();
1446-
return duplicate;
1486+
checkAccess();
1487+
Duplicate duplicatedByteBuf = new Duplicate(this);
1488+
return duplicatedByteBuf.setIndex(readerIndex(), writerIndex());
14471489
}
14481490

14491491
@Override

src/test/java/io/netty/buffer/api/adaptor/ByteBufAdaptorTest.java

Lines changed: 0 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -39,36 +39,6 @@ protected ByteBuf newBuffer(int capacity, int maxCapacity) {
3939
return alloc.buffer(capacity, capacity);
4040
}
4141

42-
@Ignore("New buffers not thread-safe like this.")
43-
@Override
44-
public void testSliceReadGatheringByteChannelMultipleThreads() throws Exception {
45-
}
46-
47-
@Ignore("New buffers not thread-safe like this.")
48-
@Override
49-
public void testDuplicateReadGatheringByteChannelMultipleThreads() throws Exception {
50-
}
51-
52-
@Ignore("New buffers not thread-safe like this.")
53-
@Override
54-
public void testSliceReadOutputStreamMultipleThreads() throws Exception {
55-
}
56-
57-
@Ignore("New buffers not thread-safe like this.")
58-
@Override
59-
public void testDuplicateReadOutputStreamMultipleThreads() throws Exception {
60-
}
61-
62-
@Ignore("New buffers not thread-safe like this.")
63-
@Override
64-
public void testSliceBytesInArrayMultipleThreads() throws Exception {
65-
}
66-
67-
@Ignore("New buffers not thread-safe like this.")
68-
@Override
69-
public void testDuplicateBytesInArrayMultipleThreads() throws Exception {
70-
}
71-
7242
@Ignore("This test codifies that asking to reading 0 bytes from an empty but unclosed stream should return -1, " +
7343
"which is just weird.")
7444
@Override
@@ -112,152 +82,8 @@ public void testNioBufferExposeOnlyRegion() {
11282
public void testToByteBuffer2() {
11383
}
11484

115-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
116-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
117-
@Override
118-
public void testRetainedDuplicateUnreleasable3() {
119-
}
120-
121-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
122-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
123-
@Override
124-
public void testRetainedDuplicateUnreleasable4() {
125-
}
126-
127-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
128-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
129-
@Override
130-
public void testRetainedDuplicateAndRetainedSliceContentIsExpected() {
131-
}
132-
133-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
134-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
135-
@Override
136-
public void testMultipleRetainedSliceReleaseOriginal2() {
137-
}
138-
139-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
140-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
141-
@Override
142-
public void testMultipleRetainedSliceReleaseOriginal3() {
143-
}
144-
145-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
146-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
147-
@Override
148-
public void testMultipleRetainedSliceReleaseOriginal4() {
149-
}
150-
151-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
152-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
153-
@Override
154-
public void testReadRetainedSliceUnreleasable3() {
155-
}
156-
157-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
158-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
159-
@Override
160-
public void testReadRetainedSliceUnreleasable4() {
161-
}
162-
163-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
164-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
165-
@Override
166-
public void testRetainedSliceUnreleasable3() {
167-
}
168-
169-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
170-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
171-
@Override
172-
public void testRetainedSliceUnreleasable4() {
173-
}
174-
175-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
176-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
177-
@Override
178-
public void testRetainedSliceReleaseOriginal2() {
179-
}
180-
181-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
182-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
183-
@Override
184-
public void testRetainedSliceReleaseOriginal3() {
185-
}
186-
187-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
188-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
189-
@Override
190-
public void testRetainedSliceReleaseOriginal4() {
191-
}
192-
193-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
194-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
195-
@Override
196-
public void testMultipleRetainedDuplicateReleaseOriginal2() {
197-
}
198-
199-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
200-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
201-
@Override
202-
public void testMultipleRetainedDuplicateReleaseOriginal3() {
203-
}
204-
205-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
206-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
207-
@Override
208-
public void testMultipleRetainedDuplicateReleaseOriginal4() {
209-
}
210-
211-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
212-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
213-
@Override
214-
public void testRetainedDuplicateReleaseOriginal2() {
215-
}
216-
217-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
218-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
219-
@Override
220-
public void testRetainedDuplicateReleaseOriginal3() {
221-
}
222-
223-
@Ignore("This assumes a single reference count for the memory, but all buffers (views of memory) have " +
224-
"independent reference counts now. Also, this plays tricks with reference that we cannot support.")
225-
@Override
226-
public void testRetainedDuplicateReleaseOriginal4() {
227-
}
228-
22985
@Ignore("No longer allowed to allocate 0 sized buffers, except for composite buffers with no components.")
23086
@Override
23187
public void testLittleEndianWithExpand() {
23288
}
233-
234-
@Ignore("Test seems to inherently have double-free bug?")
235-
@Override
236-
public void testRetainedSliceAfterReleaseRetainedSliceDuplicate() {
237-
}
238-
239-
@Ignore("Test seems to inherently have double-free bug?")
240-
@Override
241-
public void testRetainedSliceAfterReleaseRetainedDuplicateSlice() {
242-
}
243-
244-
@Ignore("Test seems to inherently have double-free bug?")
245-
@Override
246-
public void testSliceAfterReleaseRetainedSliceDuplicate() {
247-
}
248-
249-
@Ignore("Test seems to inherently have double-free bug?")
250-
@Override
251-
public void testDuplicateAfterReleaseRetainedSliceDuplicate() {
252-
}
253-
254-
@Ignore("Test seems to inherently have double-free bug?")
255-
@Override
256-
public void testDuplicateAfterReleaseRetainedDuplicateSlice() {
257-
}
258-
259-
@Ignore("Test seems to inherently have double-free bug?")
260-
@Override
261-
public void testSliceAfterReleaseRetainedDuplicateSlice() {
262-
}
26389
}

0 commit comments

Comments
 (0)