Make CompositeBuffer an interface (#11687)
Motivation: Having composite buffer as a class has caused problems in the past. Making it an interface makes it possible to stub or decorate the composite buffer implementation. Modification: Make CompositeBuffer an interface, and move the implementation to DefaultCompositeBuffer. Then hide the implementation and permit construction only through static factory methods. Result: CompositeBuffer is now an interface.
This commit is contained in:
parent
8339a00fd6
commit
0973673fce
File diff suppressed because it is too large
Load Diff
1688
buffer/src/main/java/io/netty/buffer/api/DefaultCompositeBuffer.java
Normal file
1688
buffer/src/main/java/io/netty/buffer/api/DefaultCompositeBuffer.java
Normal file
File diff suppressed because it is too large
Load Diff
@ -116,7 +116,7 @@ public class BufferCompositionTest extends BufferTestSupport {
|
|||||||
Buffer a = allocator.allocate(8);
|
Buffer a = allocator.allocate(8);
|
||||||
Buffer b = allocator.allocate(8);
|
Buffer b = allocator.allocate(8);
|
||||||
CompositeBuffer composed = CompositeBuffer.compose(allocator, a.send())) {
|
CompositeBuffer composed = CompositeBuffer.compose(allocator, a.send())) {
|
||||||
try (Buffer ignore = acquire(composed)) {
|
try (Buffer ignore = acquire((ResourceSupport<?, ?>) composed)) {
|
||||||
var exc = assertThrows(IllegalStateException.class, () -> composed.extendWith(b.send()));
|
var exc = assertThrows(IllegalStateException.class, () -> composed.extendWith(b.send()));
|
||||||
assertThat(exc).hasMessageContaining("owned");
|
assertThat(exc).hasMessageContaining("owned");
|
||||||
}
|
}
|
||||||
@ -147,13 +147,13 @@ public class BufferCompositionTest extends BufferTestSupport {
|
|||||||
try (BufferAllocator allocator = BufferAllocator.onHeapUnpooled()) {
|
try (BufferAllocator allocator = BufferAllocator.onHeapUnpooled()) {
|
||||||
Buffer a = allocator.allocate(1);
|
Buffer a = allocator.allocate(1);
|
||||||
CompositeBuffer composite = CompositeBuffer.compose(allocator, a.send());
|
CompositeBuffer composite = CompositeBuffer.compose(allocator, a.send());
|
||||||
assertTrue(isOwned(composite));
|
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
|
||||||
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)) {
|
||||||
composite.extendWith(b.send());
|
composite.extendWith(b.send());
|
||||||
}
|
}
|
||||||
assertTrue(isOwned(composite));
|
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
|
||||||
assertThat(composite.capacity()).isOne();
|
assertThat(composite.capacity()).isOne();
|
||||||
assertThat(composite.countComponents()).isOne();
|
assertThat(composite.countComponents()).isOne();
|
||||||
}
|
}
|
||||||
@ -396,11 +396,11 @@ public class BufferCompositionTest extends BufferTestSupport {
|
|||||||
allocator.allocate(8).send(),
|
allocator.allocate(8).send(),
|
||||||
allocator.allocate(8).send())) {
|
allocator.allocate(8).send())) {
|
||||||
try (CompositeBuffer split = composite.splitComponentsFloor(4)) {
|
try (CompositeBuffer split = composite.splitComponentsFloor(4)) {
|
||||||
assertTrue(isOwned(split));
|
assertTrue(isOwned((ResourceSupport<?, ?>) split));
|
||||||
assertTrue(split.isAccessible());
|
assertTrue(split.isAccessible());
|
||||||
assertThat(split.capacity()).isZero();
|
assertThat(split.capacity()).isZero();
|
||||||
|
|
||||||
assertTrue(isOwned(composite));
|
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
|
||||||
assertTrue(composite.isAccessible());
|
assertTrue(composite.isAccessible());
|
||||||
assertThat(composite.capacity()).isEqualTo(16);
|
assertThat(composite.capacity()).isEqualTo(16);
|
||||||
}
|
}
|
||||||
@ -414,11 +414,11 @@ public class BufferCompositionTest extends BufferTestSupport {
|
|||||||
allocator.allocate(8).send(),
|
allocator.allocate(8).send(),
|
||||||
allocator.allocate(8).send())) {
|
allocator.allocate(8).send())) {
|
||||||
try (CompositeBuffer split = composite.splitComponentsFloor(7)) {
|
try (CompositeBuffer split = composite.splitComponentsFloor(7)) {
|
||||||
assertTrue(isOwned(split));
|
assertTrue(isOwned((ResourceSupport<?, ?>) split));
|
||||||
assertTrue(split.isAccessible());
|
assertTrue(split.isAccessible());
|
||||||
assertThat(split.capacity()).isZero();
|
assertThat(split.capacity()).isZero();
|
||||||
|
|
||||||
assertTrue(isOwned(composite));
|
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
|
||||||
assertTrue(composite.isAccessible());
|
assertTrue(composite.isAccessible());
|
||||||
assertThat(composite.capacity()).isEqualTo(16);
|
assertThat(composite.capacity()).isEqualTo(16);
|
||||||
}
|
}
|
||||||
@ -432,11 +432,11 @@ public class BufferCompositionTest extends BufferTestSupport {
|
|||||||
allocator.allocate(8).send(),
|
allocator.allocate(8).send(),
|
||||||
allocator.allocate(8).send())) {
|
allocator.allocate(8).send())) {
|
||||||
try (CompositeBuffer split = composite.splitComponentsFloor(12)) {
|
try (CompositeBuffer split = composite.splitComponentsFloor(12)) {
|
||||||
assertTrue(isOwned(split));
|
assertTrue(isOwned((ResourceSupport<?, ?>) split));
|
||||||
assertTrue(split.isAccessible());
|
assertTrue(split.isAccessible());
|
||||||
assertThat(split.capacity()).isEqualTo(8);
|
assertThat(split.capacity()).isEqualTo(8);
|
||||||
|
|
||||||
assertTrue(isOwned(composite));
|
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
|
||||||
assertTrue(composite.isAccessible());
|
assertTrue(composite.isAccessible());
|
||||||
assertThat(composite.capacity()).isEqualTo(8);
|
assertThat(composite.capacity()).isEqualTo(8);
|
||||||
}
|
}
|
||||||
@ -450,11 +450,11 @@ public class BufferCompositionTest extends BufferTestSupport {
|
|||||||
allocator.allocate(8).send(),
|
allocator.allocate(8).send(),
|
||||||
allocator.allocate(8).send())) {
|
allocator.allocate(8).send())) {
|
||||||
try (CompositeBuffer split = composite.splitComponentsFloor(8)) {
|
try (CompositeBuffer split = composite.splitComponentsFloor(8)) {
|
||||||
assertTrue(isOwned(split));
|
assertTrue(isOwned((ResourceSupport<?, ?>) split));
|
||||||
assertTrue(split.isAccessible());
|
assertTrue(split.isAccessible());
|
||||||
assertThat(split.capacity()).isEqualTo(8);
|
assertThat(split.capacity()).isEqualTo(8);
|
||||||
|
|
||||||
assertTrue(isOwned(composite));
|
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
|
||||||
assertTrue(composite.isAccessible());
|
assertTrue(composite.isAccessible());
|
||||||
assertThat(composite.capacity()).isEqualTo(8);
|
assertThat(composite.capacity()).isEqualTo(8);
|
||||||
}
|
}
|
||||||
@ -468,11 +468,11 @@ public class BufferCompositionTest extends BufferTestSupport {
|
|||||||
allocator.allocate(8).send(),
|
allocator.allocate(8).send(),
|
||||||
allocator.allocate(8).send())) {
|
allocator.allocate(8).send())) {
|
||||||
try (CompositeBuffer split = composite.splitComponentsCeil(4)) {
|
try (CompositeBuffer split = composite.splitComponentsCeil(4)) {
|
||||||
assertTrue(isOwned(split));
|
assertTrue(isOwned((ResourceSupport<?, ?>) split));
|
||||||
assertTrue(split.isAccessible());
|
assertTrue(split.isAccessible());
|
||||||
assertThat(split.capacity()).isEqualTo(8);
|
assertThat(split.capacity()).isEqualTo(8);
|
||||||
|
|
||||||
assertTrue(isOwned(composite));
|
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
|
||||||
assertTrue(composite.isAccessible());
|
assertTrue(composite.isAccessible());
|
||||||
assertThat(composite.capacity()).isEqualTo(8);
|
assertThat(composite.capacity()).isEqualTo(8);
|
||||||
}
|
}
|
||||||
@ -486,11 +486,11 @@ public class BufferCompositionTest extends BufferTestSupport {
|
|||||||
allocator.allocate(8).send(),
|
allocator.allocate(8).send(),
|
||||||
allocator.allocate(8).send())) {
|
allocator.allocate(8).send())) {
|
||||||
try (CompositeBuffer split = composite.splitComponentsCeil(7)) {
|
try (CompositeBuffer split = composite.splitComponentsCeil(7)) {
|
||||||
assertTrue(isOwned(split));
|
assertTrue(isOwned((ResourceSupport<?, ?>) split));
|
||||||
assertTrue(split.isAccessible());
|
assertTrue(split.isAccessible());
|
||||||
assertThat(split.capacity()).isEqualTo(8);
|
assertThat(split.capacity()).isEqualTo(8);
|
||||||
|
|
||||||
assertTrue(isOwned(composite));
|
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
|
||||||
assertTrue(composite.isAccessible());
|
assertTrue(composite.isAccessible());
|
||||||
assertThat(composite.capacity()).isEqualTo(8);
|
assertThat(composite.capacity()).isEqualTo(8);
|
||||||
}
|
}
|
||||||
@ -498,17 +498,17 @@ public class BufferCompositionTest extends BufferTestSupport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void splitComponentsCeilMustGiveBufferWithFirstAndSecondComponentForfOffsetInSecondComponent() {
|
public void splitComponentsCeilMustGiveBufferWithFirstAndSecondComponentForOffsetInSecondComponent() {
|
||||||
try (BufferAllocator allocator = BufferAllocator.onHeapUnpooled();
|
try (BufferAllocator allocator = BufferAllocator.onHeapUnpooled();
|
||||||
CompositeBuffer composite = CompositeBuffer.compose(allocator,
|
CompositeBuffer composite = CompositeBuffer.compose(allocator,
|
||||||
allocator.allocate(8).send(),
|
allocator.allocate(8).send(),
|
||||||
allocator.allocate(8).send())) {
|
allocator.allocate(8).send())) {
|
||||||
try (CompositeBuffer split = composite.splitComponentsCeil(12)) {
|
try (CompositeBuffer split = composite.splitComponentsCeil(12)) {
|
||||||
assertTrue(isOwned(split));
|
assertTrue(isOwned((ResourceSupport<?, ?>) split));
|
||||||
assertTrue(split.isAccessible());
|
assertTrue(split.isAccessible());
|
||||||
assertThat(split.capacity()).isEqualTo(16);
|
assertThat(split.capacity()).isEqualTo(16);
|
||||||
|
|
||||||
assertTrue(isOwned(composite));
|
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
|
||||||
assertTrue(composite.isAccessible());
|
assertTrue(composite.isAccessible());
|
||||||
assertThat(composite.capacity()).isEqualTo(0);
|
assertThat(composite.capacity()).isEqualTo(0);
|
||||||
}
|
}
|
||||||
@ -520,11 +520,11 @@ public class BufferCompositionTest extends BufferTestSupport {
|
|||||||
allocator.allocate(8).send(),
|
allocator.allocate(8).send(),
|
||||||
allocator.allocate(8).send())) {
|
allocator.allocate(8).send())) {
|
||||||
try (CompositeBuffer split = composite.splitComponentsCeil(12)) {
|
try (CompositeBuffer split = composite.splitComponentsCeil(12)) {
|
||||||
assertTrue(isOwned(split));
|
assertTrue(isOwned((ResourceSupport<?, ?>) split));
|
||||||
assertTrue(split.isAccessible());
|
assertTrue(split.isAccessible());
|
||||||
assertThat(split.capacity()).isEqualTo(16);
|
assertThat(split.capacity()).isEqualTo(16);
|
||||||
|
|
||||||
assertTrue(isOwned(composite));
|
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
|
||||||
assertTrue(composite.isAccessible());
|
assertTrue(composite.isAccessible());
|
||||||
assertThat(composite.capacity()).isEqualTo(8);
|
assertThat(composite.capacity()).isEqualTo(8);
|
||||||
}
|
}
|
||||||
@ -532,17 +532,17 @@ public class BufferCompositionTest extends BufferTestSupport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void splitComponentsCeilMustGiveBufferWithFirstComponentForfOffsetOnFirstByteInSecondComponent() {
|
public void splitComponentsCeilMustGiveBufferWithFirstComponentForOffsetOnFirstByteInSecondComponent() {
|
||||||
try (BufferAllocator allocator = BufferAllocator.onHeapUnpooled();
|
try (BufferAllocator allocator = BufferAllocator.onHeapUnpooled();
|
||||||
CompositeBuffer composite = CompositeBuffer.compose(allocator,
|
CompositeBuffer composite = CompositeBuffer.compose(allocator,
|
||||||
allocator.allocate(8).send(),
|
allocator.allocate(8).send(),
|
||||||
allocator.allocate(8).send())) {
|
allocator.allocate(8).send())) {
|
||||||
try (CompositeBuffer split = composite.splitComponentsCeil(7)) {
|
try (CompositeBuffer split = composite.splitComponentsCeil(7)) {
|
||||||
assertTrue(isOwned(split));
|
assertTrue(isOwned((ResourceSupport<?, ?>) split));
|
||||||
assertTrue(split.isAccessible());
|
assertTrue(split.isAccessible());
|
||||||
assertThat(split.capacity()).isEqualTo(8);
|
assertThat(split.capacity()).isEqualTo(8);
|
||||||
|
|
||||||
assertTrue(isOwned(composite));
|
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
|
||||||
assertTrue(composite.isAccessible());
|
assertTrue(composite.isAccessible());
|
||||||
assertThat(composite.capacity()).isEqualTo(8);
|
assertThat(composite.capacity()).isEqualTo(8);
|
||||||
}
|
}
|
||||||
@ -556,11 +556,11 @@ public class BufferCompositionTest extends BufferTestSupport {
|
|||||||
allocator.allocate(8).send(),
|
allocator.allocate(8).send(),
|
||||||
allocator.allocate(8).send())) {
|
allocator.allocate(8).send())) {
|
||||||
try (CompositeBuffer split = composite.splitComponentsCeil(0)) {
|
try (CompositeBuffer split = composite.splitComponentsCeil(0)) {
|
||||||
assertTrue(isOwned(split));
|
assertTrue(isOwned((ResourceSupport<?, ?>) split));
|
||||||
assertTrue(split.isAccessible());
|
assertTrue(split.isAccessible());
|
||||||
assertThat(split.capacity()).isZero();
|
assertThat(split.capacity()).isZero();
|
||||||
|
|
||||||
assertTrue(isOwned(composite));
|
assertTrue(isOwned((ResourceSupport<?, ?>) composite));
|
||||||
assertTrue(composite.isAccessible());
|
assertTrue(composite.isAccessible());
|
||||||
assertThat(composite.capacity()).isEqualTo(16);
|
assertThat(composite.capacity()).isEqualTo(16);
|
||||||
}
|
}
|
||||||
@ -612,7 +612,7 @@ public class BufferCompositionTest extends BufferTestSupport {
|
|||||||
throw new RuntimeException("Expected.");
|
throw new RuntimeException("Expected.");
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
Statics.unsafeSetDrop(composite, throwingDrop);
|
Statics.unsafeSetDrop((ResourceSupport<?, ?>) composite, throwingDrop);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
composite.close();
|
composite.close();
|
||||||
throw e;
|
throw e;
|
||||||
|
Loading…
Reference in New Issue
Block a user