Skip to content

Commit c3b4d15

Browse files
authored
fixes DefaultPayload.create(ByteBuf, ByteBuf) to release params (#886)
This PR makes `DefaultPayload.create(ByteBuf, ByteBuf)` behaves consistent with `ByteBufPayload.create(ByteBuf, ByteBuf)` method. Please note, both methods do not require a user to take any further actions on releasing provided data and metadata `ByteBuf`s. This is a responsibility of the Payload to do so immediately (in the case of `DefaultPayload`) or later, upon the release faze (in case of `ByteBufPayload`) Signed-off-by: Oleh Dokuka <[email protected]>
1 parent 1673eed commit c3b4d15

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

rsocket-core/src/main/java/io/rsocket/util/DefaultPayload.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,18 @@ public static Payload create(ByteBuf data) {
9999
}
100100

101101
public static Payload create(ByteBuf data, @Nullable ByteBuf metadata) {
102-
return create(data.nioBuffer(), metadata == null ? null : metadata.nioBuffer());
102+
try {
103+
return create(data.nioBuffer(), metadata == null ? null : metadata.nioBuffer());
104+
} finally {
105+
data.release();
106+
if (metadata != null) {
107+
metadata.release();
108+
}
109+
}
103110
}
104111

105112
public static Payload create(Payload payload) {
106-
return create(
107-
Unpooled.copiedBuffer(payload.sliceData()),
108-
payload.hasMetadata() ? Unpooled.copiedBuffer(payload.sliceMetadata()) : null);
113+
return create(payload.getData(), payload.hasMetadata() ? payload.getMetadata() : null);
109114
}
110115

111116
@Override

rsocket-core/src/test/java/io/rsocket/frame/SetupFrameCodecTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ void testEncodingNoResume() {
2525
assertEquals(0, SetupFrameCodec.resumeToken(frame).readableBytes());
2626
assertEquals("metadata_type", SetupFrameCodec.metadataMimeType(frame));
2727
assertEquals("data_type", SetupFrameCodec.dataMimeType(frame));
28-
assertEquals(metadata, SetupFrameCodec.metadata(frame));
29-
assertEquals(data, SetupFrameCodec.data(frame));
28+
assertEquals(payload.metadata(), SetupFrameCodec.metadata(frame));
29+
assertEquals(payload.data(), SetupFrameCodec.data(frame));
3030
assertEquals(SetupFrameCodec.CURRENT_VERSION, SetupFrameCodec.version(frame));
3131
frame.release();
3232
}
@@ -49,8 +49,8 @@ void testEncodingResume() {
4949
assertEquals(token, SetupFrameCodec.resumeToken(frame));
5050
assertEquals("metadata_type", SetupFrameCodec.metadataMimeType(frame));
5151
assertEquals("data_type", SetupFrameCodec.dataMimeType(frame));
52-
assertEquals(metadata, SetupFrameCodec.metadata(frame));
53-
assertEquals(data, SetupFrameCodec.data(frame));
52+
assertEquals(payload.metadata(), SetupFrameCodec.metadata(frame));
53+
assertEquals(payload.data(), SetupFrameCodec.data(frame));
5454
assertEquals(SetupFrameCodec.CURRENT_VERSION, SetupFrameCodec.version(frame));
5555
frame.release();
5656
}

rsocket-core/src/test/java/io/rsocket/util/DefaultPayloadTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919
import static org.hamcrest.MatcherAssert.assertThat;
2020
import static org.hamcrest.Matchers.equalTo;
2121

22+
import io.netty.buffer.ByteBuf;
23+
import io.netty.buffer.ByteBufAllocator;
2224
import io.netty.buffer.Unpooled;
2325
import io.rsocket.Payload;
26+
import io.rsocket.buffer.LeaksTrackingByteBufAllocator;
2427
import java.nio.ByteBuffer;
28+
import java.util.concurrent.ThreadLocalRandom;
2529
import org.assertj.core.api.Assertions;
2630
import org.junit.Test;
2731

@@ -74,4 +78,32 @@ public void shouldIndicateThatItHasMetadata2() {
7478

7579
Assertions.assertThat(payload.hasMetadata()).isTrue();
7680
}
81+
82+
@Test
83+
public void shouldReleaseGivenByteBufDataAndMetadataUpOnPayloadCreation() {
84+
LeaksTrackingByteBufAllocator allocator =
85+
LeaksTrackingByteBufAllocator.instrument(ByteBufAllocator.DEFAULT);
86+
for (byte i = 0; i < 126; i++) {
87+
ByteBuf data = allocator.buffer();
88+
data.writeByte(i);
89+
90+
boolean metadataPresent = ThreadLocalRandom.current().nextBoolean();
91+
ByteBuf metadata = null;
92+
if (metadataPresent) {
93+
metadata = allocator.buffer();
94+
metadata.writeByte(i + 1);
95+
}
96+
97+
Payload payload = DefaultPayload.create(data, metadata);
98+
99+
Assertions.assertThat(payload.getData()).isEqualTo(ByteBuffer.wrap(new byte[] {i}));
100+
101+
Assertions.assertThat(payload.getMetadata())
102+
.isEqualTo(
103+
metadataPresent
104+
? ByteBuffer.wrap(new byte[] {(byte) (i + 1)})
105+
: DefaultPayload.EMPTY_BUFFER);
106+
allocator.assertHasNoLeaks();
107+
}
108+
}
77109
}

0 commit comments

Comments
 (0)