Netty has various ways to check the network stack.
ChannelHandlers Testing
You can use the Netty EmbeddedChannel to make fun of a network connection for testing, an example of this would be:
@Test public void nettyTest() { EmbeddedChannel channel = new EmbeddedChannel(new StringDecoder(StandardCharsets.UTF_8)); channel.writeInbound(Unpooled.wrappedBuffer(new byte[]{(byte)0xE2,(byte)0x98,(byte)0xA2})); String myObject = channel.readInbound();
This test is above tests for StringDecoder's ability to decode unicode correctly ( example from this error sent by me )
You can also check the encoding direction with the EmbeddedChannel , for this you have to use writeOutBound and readInbound .
Additional examples:
DelimiterBasedFrameDecoderTest.java :
@Test public void testIncompleteLinesStrippedDelimiters() { EmbeddedChannel ch = new EmbeddedChannel(new DelimiterBasedFrameDecoder(8192, true, Delimiters.lineDelimiter())); ch.writeInbound(Unpooled.copiedBuffer("Test", Charset.defaultCharset())); assertNull(ch.readInbound()); ch.writeInbound(Unpooled.copiedBuffer("Line\r\ng\r\n", Charset.defaultCharset())); assertEquals("TestLine", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset())); assertEquals("g", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset())); assertNull(ch.readInbound()); ch.finish(); }
Additional examples on github.
Bytebuf
To check if you are bytebuf s, you can set a JVM parameter that checks for ByteBuf leak, for this you need to add -Dio.netty.leakDetectionLevel=PARANOID to the startup parameters or call the ResourceLeakDetector.setLevel(PARANOID) method.
source share