From 5087471ed3e5ba4b171dae41cf5ca229e9cb08a3 Mon Sep 17 00:00:00 2001 From: Luis De Bello Date: Tue, 1 Sep 2020 18:44:42 -0300 Subject: [PATCH] ARTEMIS-2696 Releasing ByteBuf after reading content on WebSocket --- .../websocket/WebSocketFrameEncoder.java | 2 ++ .../websocket/WebSocketFrameEncoderTest.java | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/protocol/websocket/WebSocketFrameEncoder.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/protocol/websocket/WebSocketFrameEncoder.java index 703998b5b3..813a2a082e 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/protocol/websocket/WebSocketFrameEncoder.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/protocol/websocket/WebSocketFrameEncoder.java @@ -63,5 +63,7 @@ public class WebSocketFrameEncoder extends ChannelOutboundHandlerAdapter { byteBuf.readBytes(fragment, length); ctx.writeAndFlush(new ContinuationWebSocketFrame(finalFragment, 0, fragment), promise); } + + byteBuf.release(); } } diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/protocol/websocket/WebSocketFrameEncoderTest.java b/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/protocol/websocket/WebSocketFrameEncoderTest.java index fc67c9a005..047464fa78 100644 --- a/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/protocol/websocket/WebSocketFrameEncoderTest.java +++ b/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/protocol/websocket/WebSocketFrameEncoderTest.java @@ -29,6 +29,7 @@ import static org.mockito.Mockito.verifyZeroInteractions; import java.nio.charset.StandardCharsets; import java.util.List; +import io.netty.buffer.ByteBufUtil; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -75,6 +76,28 @@ public class WebSocketFrameEncoderTest { verifyZeroInteractions(promise); } + @Test + public void testWriteReleaseBuffer() throws Exception { + String content = "Buffer should be released"; + + int utf8Bytes = ByteBufUtil.utf8Bytes(content); + ByteBuf msg = Unpooled.directBuffer(utf8Bytes); + ByteBufUtil.reserveAndWriteUtf8(msg, content, utf8Bytes); + + ArgumentCaptor frameCaptor = ArgumentCaptor.forClass(WebSocketFrame.class); + + spy.write(ctx, msg, promise); //test + + assertEquals(0, msg.refCnt()); + assertEquals(0, msg.readableBytes()); + verify(ctx).writeAndFlush(frameCaptor.capture(), eq(promise)); + WebSocketFrame frame = frameCaptor.getValue(); + assertTrue(frame instanceof BinaryWebSocketFrame); + assertTrue(frame.isFinalFragment()); + assertEquals(content, frame.content().toString(StandardCharsets.UTF_8)); + } + + @Test public void testWriteSingleFrame() throws Exception { String content = "Content MSG length less than max frame payload length: " + maxFramePayloadLength;