From 56e886bf70669befd7b9e7380e68751fe67f05b2 Mon Sep 17 00:00:00 2001
From: YASH PATEL <121890726+yp969803@users.noreply.github.com>
Date: Wed, 29 Nov 2023 10:02:38 +0530
Subject: [PATCH] =?UTF-8?q?[ISSUE=20#7592]=20testCleanBuffer=20unit=20test?=
 =?UTF-8?q?=20modifies,=20changed=20non-direct=20=E2=80=A6=20(#7593)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* [ISSUE #7592] testCleanBuffer unit test modifies, changed non-direct to direct buffer allocation

* fix: consolidate UtilAll#cleanBuffer by checking if the given buffer is direct or not

Signed-off-by: Li Zhanhui <lizhanhui@gmail.com>

---------

Signed-off-by: Li Zhanhui <lizhanhui@gmail.com>
Co-authored-by: Li Zhanhui <lizhanhui@gmail.com>
---
 .../main/java/org/apache/rocketmq/common/UtilAll.java    | 9 +++++++++
 .../java/org/apache/rocketmq/common/UtilAllTest.java     | 3 ++-
 .../rocketmq/store/timer/TimerMessageStoreTest.java      | 2 +-
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/common/src/main/java/org/apache/rocketmq/common/UtilAll.java b/common/src/main/java/org/apache/rocketmq/common/UtilAll.java
index 2808f106a..19efa9aa9 100644
--- a/common/src/main/java/org/apache/rocketmq/common/UtilAll.java
+++ b/common/src/main/java/org/apache/rocketmq/common/UtilAll.java
@@ -699,10 +699,19 @@ public class UtilAll {
         }
     }
 
+    /**
+     * Free direct-buffer's memory actively.
+     * @param buffer Direct buffer to free.
+     */
     public static void cleanBuffer(final ByteBuffer buffer) {
         if (null == buffer) {
             return;
         }
+
+        if (!buffer.isDirect()) {
+            return;
+        }
+
         PlatformDependent.freeDirectBuffer(buffer);
     }
 
diff --git a/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java b/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java
index cb288578c..2d22d5254 100644
--- a/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java
+++ b/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java
@@ -215,8 +215,9 @@ public class UtilAllTest {
     @Test
     public void testCleanBuffer() {
         UtilAll.cleanBuffer(null);
+        UtilAll.cleanBuffer(ByteBuffer.allocateDirect(10));
+        UtilAll.cleanBuffer(ByteBuffer.allocateDirect(0));
         UtilAll.cleanBuffer(ByteBuffer.allocate(10));
-        UtilAll.cleanBuffer(ByteBuffer.allocate(0));
     }
 
     @Test
diff --git a/store/src/test/java/org/apache/rocketmq/store/timer/TimerMessageStoreTest.java b/store/src/test/java/org/apache/rocketmq/store/timer/TimerMessageStoreTest.java
index 63ec97cdb..02ff35681 100644
--- a/store/src/test/java/org/apache/rocketmq/store/timer/TimerMessageStoreTest.java
+++ b/store/src/test/java/org/apache/rocketmq/store/timer/TimerMessageStoreTest.java
@@ -387,7 +387,7 @@ public class TimerMessageStoreTest {
             assertEquals(PutMessageStatus.PUT_OK, putMessageResult.getPutMessageStatus());
         }
 
-        // Wait until messages have wrote to TimerLog and currReadTimeMs catches up current time.
+        // Wait until messages have written to TimerLog and currReadTimeMs catches up current time.
         await().atMost(5000, TimeUnit.MILLISECONDS).until(new Callable<Boolean>() {
             @Override
             public Boolean call() {
-- 
2.32.0.windows.2

