ARTEMIS-2696 Releasing ByteBuf after reading content on WebSocket

This commit is contained in:
Luis De Bello 2020-09-01 18:44:42 -03:00 committed by Clebert Suconic
parent d934dc6f67
commit 5087471ed3
2 changed files with 25 additions and 0 deletions

View File

@ -63,5 +63,7 @@ public class WebSocketFrameEncoder extends ChannelOutboundHandlerAdapter {
byteBuf.readBytes(fragment, length);
ctx.writeAndFlush(new ContinuationWebSocketFrame(finalFragment, 0, fragment), promise);
}
byteBuf.release();
}
}

View File

@ -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<WebSocketFrame> 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;