Home / Function/ testReusedBuffer() — netty Function Reference

testReusedBuffer() — netty Function Reference

Architecture documentation for the testReusedBuffer() function in ByteToMessageDecoderTest.java from the netty codebase.

Function java Buffer Allocators calls 1 called by 8

Entity Profile

Dependency Diagram

graph TD
  fabcda1c_4a0f_6fc2_3971_58e0d1d79967["testReusedBuffer()"]
  b1c999fe_35fb_8b70_a958_296cffb0616a["ByteToMessageDecoderTest"]
  fabcda1c_4a0f_6fc2_3971_58e0d1d79967 -->|defined in| b1c999fe_35fb_8b70_a958_296cffb0616a
  84dfa896_717c_aea1_a482_a8c6157a7eab["testReuseInputBufferJustLargeEnoughToContainMessage_MergeCumulator()"]
  84dfa896_717c_aea1_a482_a8c6157a7eab -->|calls| fabcda1c_4a0f_6fc2_3971_58e0d1d79967
  768898a3_9769_c59a_0046_a5a274c05648["testReuseInputBufferJustLargeEnoughToContainMessagePartiallyReceived2x_MergeCumulator()"]
  768898a3_9769_c59a_0046_a5a274c05648 -->|calls| fabcda1c_4a0f_6fc2_3971_58e0d1d79967
  eec53dd9_0baa_21d6_2184_8079f757abb3["testReuseInputBufferSufficientlyLargeToContainDuplicateMessage_MergeCumulator()"]
  eec53dd9_0baa_21d6_2184_8079f757abb3 -->|calls| fabcda1c_4a0f_6fc2_3971_58e0d1d79967
  8ded5f02_dd9b_b911_c68f_682d6f34bdcd["testReuseInputBufferSufficientlyLargeToContainDuplicateMessagePartiallyReceived2x_MergeCumulator()"]
  8ded5f02_dd9b_b911_c68f_682d6f34bdcd -->|calls| fabcda1c_4a0f_6fc2_3971_58e0d1d79967
  7956a0b9_9b7d_3c18_fe1f_045bd1a83328["testReuseInputBufferJustLargeEnoughToContainMessage_CompositeCumulator()"]
  7956a0b9_9b7d_3c18_fe1f_045bd1a83328 -->|calls| fabcda1c_4a0f_6fc2_3971_58e0d1d79967
  d9bbad16_d9ae_234b_c400_62400c1d1ad9["testReuseInputBufferJustLargeEnoughToContainMessagePartiallyReceived2x_CompositeCumulator()"]
  d9bbad16_d9ae_234b_c400_62400c1d1ad9 -->|calls| fabcda1c_4a0f_6fc2_3971_58e0d1d79967
  8e57f567_7d9d_1771_33e3_45f1e6a1a9d6["testReuseInputBufferSufficientlyLargeToContainDuplicateMessage_CompositeCumulator()"]
  8e57f567_7d9d_1771_33e3_45f1e6a1a9d6 -->|calls| fabcda1c_4a0f_6fc2_3971_58e0d1d79967
  717b984f_1908_2865_88dc_376554fcba0a["testReuseInputBufferSufficientlyLargeToContainDuplicateMessagePartiallyReceived2x_CompositeCumulator()"]
  717b984f_1908_2865_88dc_376554fcba0a -->|calls| fabcda1c_4a0f_6fc2_3971_58e0d1d79967
  5082856a_b229_862d_0072_81f9b24f56a3["EmbeddedChannel()"]
  fabcda1c_4a0f_6fc2_3971_58e0d1d79967 -->|calls| 5082856a_b229_862d_0072_81f9b24f56a3
  style fabcda1c_4a0f_6fc2_3971_58e0d1d79967 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

codec-base/src/test/java/io/netty/handler/codec/ByteToMessageDecoderTest.java lines 638–686

    static void testReusedBuffer(ByteBuf buffer, boolean secondPartial, ByteToMessageDecoder.Cumulator cumulator) {
        ByteToMessageDecoder decoder = new ByteToMessageDecoder() {
            @Override
            protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
                while (in.readableBytes() >= 4) {
                    int index = in.readerIndex();
                    int len = in.readInt();
                    assert len < (1 << 30) : "In-plausibly long message: " + len;
                    if (in.readableBytes() >= len) {
                        byte[] bytes = new byte[len];
                        in.readBytes(bytes);
                        String message = new String(bytes, CharsetUtil.UTF_8);
                        out.add(message);
                    } else {
                        in.readerIndex(index);
                        return;
                    }
                }
            }
        };
        decoder.setCumulator(cumulator);
        EmbeddedChannel channel = new EmbeddedChannel(decoder);

        buffer.retain(); // buffer is allocated from the pool, the pool would call retain()
        buffer.writeInt(11); // total length of message
        buffer.writeByte('h').writeByte('e').writeByte('l').writeByte('l');
        if (secondPartial) {
            assertFalse(channel.writeInbound(buffer)); // try reading incomplete message
            assertTrue(channel.inboundMessages().isEmpty());
            assertEquals(0, buffer.readerIndex(), "Incomplete message should still be readable in buffer");
            buffer.retain(); // buffer is allocated from the pool - reusing same buffer, the pool would call retain()
        }
        buffer.writeByte('o').writeByte(' ');
        assertFalse(channel.writeInbound(buffer)); // try reading incomplete message
        assertTrue(channel.inboundMessages().isEmpty());
        assertEquals(0, buffer.readerIndex(), "Incomplete message should still be readable in buffer");

        buffer.retain(); // buffer is allocated from the pool - reusing same buffer, the pool would call retain()
        buffer.writeByte('w').writeByte('o').writeByte('r').writeByte('l').writeByte('d');
        assertTrue(channel.writeInbound(buffer));
        assertFalse(channel.inboundMessages().isEmpty(), "Message should be received");
        assertEquals("hello world", channel.inboundMessages().poll(), "Message should be received correctly");
        assertTrue(channel.inboundMessages().isEmpty(), "Only a single message should be received");
        assertFalse(buffer.isReadable(), "Buffer should not have remaining data after reading complete message");

        buffer.release(); // we are done with the buffer - release it from the pool
        assertEquals(0, buffer.refCnt(), "Buffer should be released");
        assertFalse(channel.finish());
    }

Domain

Subdomains

Frequently Asked Questions

What does testReusedBuffer() do?
testReusedBuffer() is a function in the netty codebase, defined in codec-base/src/test/java/io/netty/handler/codec/ByteToMessageDecoderTest.java.
Where is testReusedBuffer() defined?
testReusedBuffer() is defined in codec-base/src/test/java/io/netty/handler/codec/ByteToMessageDecoderTest.java at line 638.
What does testReusedBuffer() call?
testReusedBuffer() calls 1 function(s): EmbeddedChannel.
What calls testReusedBuffer()?
testReusedBuffer() is called by 8 function(s): testReuseInputBufferJustLargeEnoughToContainMessagePartiallyReceived2x_CompositeCumulator, testReuseInputBufferJustLargeEnoughToContainMessagePartiallyReceived2x_MergeCumulator, testReuseInputBufferJustLargeEnoughToContainMessage_CompositeCumulator, testReuseInputBufferJustLargeEnoughToContainMessage_MergeCumulator, testReuseInputBufferSufficientlyLargeToContainDuplicateMessagePartiallyReceived2x_CompositeCumulator, testReuseInputBufferSufficientlyLargeToContainDuplicateMessagePartiallyReceived2x_MergeCumulator, testReuseInputBufferSufficientlyLargeToContainDuplicateMessage_CompositeCumulator, testReuseInputBufferSufficientlyLargeToContainDuplicateMessage_MergeCumulator.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free