Copilot commented on code in PR #16027:
URL: https://github.com/apache/dubbo/pull/16027#discussion_r2715592202


##########
dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/NettyBackedChannelBufferTest.java:
##########
@@ -60,4 +62,49 @@ void testBufferTransfer() {
         assertEquals(1, actual[0]);
         assertEquals(2, actual[1]);
     }
+
+    @Test
+    void testBufferTransfer_directToDirect() {
+        ByteBuf srcDirect = Unpooled.directBuffer(4);
+        ByteBuf dstDirect = Unpooled.directBuffer(4);
+
+        ChannelBuffer source = new NettyBackedChannelBuffer(srcDirect);
+        ChannelBuffer target = new NettyBackedChannelBuffer(dstDirect);
+
+        byte[] data = {10, 20, 30, 40};
+        source.writeBytes(data);
+
+        target.setBytes(0, source, 0, 4);
+
+        byte[] actual = new byte[4];
+        target.getBytes(0, actual);
+
+        assertArrayEquals(data, actual);
+        assertEquals(0, target.readerIndex(), "setBytes should not move 
readerIndex");

Review Comment:
   `testBufferTransfer_directToDirect` allocates two direct `ByteBuf`s via 
`Unpooled.directBuffer(4)` but never releases them, which leaks off-heap memory 
(Netty direct buffers are reference-counted and must be released). Even though 
each buffer is small, it's better to call `release()` on both `srcDirect` and 
`dstDirect` (e.g., in a `try/finally`) to avoid leaks if this test is run 
repeatedly or adapted for larger buffers.
   ```suggestion
           try {
               ChannelBuffer source = new NettyBackedChannelBuffer(srcDirect);
               ChannelBuffer target = new NettyBackedChannelBuffer(dstDirect);
   
               byte[] data = {10, 20, 30, 40};
               source.writeBytes(data);
   
               target.setBytes(0, source, 0, 4);
   
               byte[] actual = new byte[4];
               target.getBytes(0, actual);
   
               assertArrayEquals(data, actual);
               assertEquals(0, target.readerIndex(), "setBytes should not move 
readerIndex");
           } finally {
               srcDirect.release();
               dstDirect.release();
           }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to