This is an automated email from the ASF dual-hosted git repository.

ShannonDing pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git


The following commit(s) were added to refs/heads/develop by this push:
     new b281d4c0a7 [ISSUE #11628] Add unit tests for FutureHolder task 
cancellation lifecycle (#11629)
b281d4c0a7 is described below

commit b281d4c0a7aeda690d7fe5f8ac0b04a304547843
Author: coO-OO-O-co <[email protected]>
AuthorDate: Fri Nov 27 15:37:26 2026 +0800

    [ISSUE #11628] Add unit tests for FutureHolder task cancellation lifecycle 
(#11629)
    
    * [Test] Cover FutureHolder task cancellation lifecycle
    
    * [Test] Isolate manual index compaction from background worker
---
 .../common/statistics/FutureHolderTest.java        | 101 +++++++++++++++++++++
 .../tieredstore/index/IndexStoreServiceTest.java   |  77 ++++++++++------
 2 files changed, 150 insertions(+), 28 deletions(-)

diff --git 
a/common/src/test/java/org/apache/rocketmq/common/statistics/FutureHolderTest.java
 
b/common/src/test/java/org/apache/rocketmq/common/statistics/FutureHolderTest.java
new file mode 100644
index 0000000000..f2fe7837e0
--- /dev/null
+++ 
b/common/src/test/java/org/apache/rocketmq/common/statistics/FutureHolderTest.java
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.rocketmq.common.statistics;
+
+import java.util.concurrent.Future;
+import org.junit.Test;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+
+public class FutureHolderTest {
+
+    @Test
+    public void testRemoveAllFutureCancelsAllFuturesWithoutInterrupting() {
+        FutureHolder<String> holder = new FutureHolder<>();
+        Future<?> first = mock(Future.class);
+        Future<?> second = mock(Future.class);
+
+        holder.addFuture("item", first);
+        holder.addFuture("item", second);
+        verifyNoInteractions(first, second);
+
+        holder.removeAllFuture("item");
+
+        verify(first).cancel(false);
+        verify(second).cancel(false);
+        verifyNoMoreInteractions(first, second);
+    }
+
+    @Test
+    public void testRemoveAllFutureDoesNotAffectOtherKeys() {
+        FutureHolder<String> holder = new FutureHolder<>();
+        Future<?> first = mock(Future.class);
+        Future<?> second = mock(Future.class);
+
+        holder.addFuture("first", first);
+        holder.addFuture("second", second);
+
+        holder.removeAllFuture("first");
+
+        verify(first).cancel(false);
+        verifyNoInteractions(second);
+
+        holder.removeAllFuture("second");
+
+        verify(second).cancel(false);
+        verifyNoMoreInteractions(first, second);
+    }
+
+    @Test
+    public void testRemoveAllFutureIgnoresMissingAndRemovedKeys() {
+        FutureHolder<String> holder = new FutureHolder<>();
+        Future<?> future = mock(Future.class);
+
+        holder.removeAllFuture("missing");
+
+        holder.addFuture("item", future);
+        holder.removeAllFuture("missing");
+        verifyNoInteractions(future);
+
+        holder.removeAllFuture("item");
+        holder.removeAllFuture("item");
+
+        verify(future).cancel(false);
+        verifyNoMoreInteractions(future);
+    }
+
+    @Test
+    public void testAddFutureAfterRemovalUsesNewLifecycle() {
+        FutureHolder<String> holder = new FutureHolder<>();
+        Future<?> previous = mock(Future.class);
+        Future<?> current = mock(Future.class);
+
+        holder.addFuture("item", previous);
+        holder.removeAllFuture("item");
+
+        holder.addFuture("item", current);
+        verifyNoInteractions(current);
+        holder.removeAllFuture("item");
+
+        verify(previous).cancel(false);
+        verify(current).cancel(false);
+        verifyNoMoreInteractions(previous, current);
+    }
+}
diff --git 
a/tieredstore/src/test/java/org/apache/rocketmq/tieredstore/index/IndexStoreServiceTest.java
 
b/tieredstore/src/test/java/org/apache/rocketmq/tieredstore/index/IndexStoreServiceTest.java
index 0d849d5927..e84501b098 100644
--- 
a/tieredstore/src/test/java/org/apache/rocketmq/tieredstore/index/IndexStoreServiceTest.java
+++ 
b/tieredstore/src/test/java/org/apache/rocketmq/tieredstore/index/IndexStoreServiceTest.java
@@ -134,11 +134,15 @@ public class IndexStoreServiceTest {
 
     @Test
     public void concurrentPutTest() throws InterruptedException {
+        putKeysConcurrently(new IndexStoreService(fileAllocator, filePath));
+    }
+
+    private void putKeysConcurrently(IndexStoreService service) throws 
InterruptedException {
         ExecutorService executorService = Executors.newFixedThreadPool(
             4, new ThreadFactoryImpl("ConcurrentPutTest"));
         storeConfig.setTieredStoreIndexFileMaxHashSlotNum(500);
         storeConfig.setTieredStoreIndexFileMaxIndexNum(2000);
-        indexService = new IndexStoreService(fileAllocator, filePath);
+        indexService = service;
         indexService.start();
         long timestamp = System.currentTimeMillis();
 
@@ -170,33 +174,50 @@ public class IndexStoreServiceTest {
 
     @Test
     public void doCompactionTest() throws InterruptedException {
-        concurrentPutTest();
-        IndexFile indexFile = indexService.getNextSealedFile();
-        Assert.assertEquals(IndexFile.IndexStatusEnum.SEALED, 
indexFile.getFileStatus());
-
-        indexService.doCompactThenUploadFile(indexFile);
-        indexService.setCompactTimestamp(indexFile.getTimestamp());
-        indexFile.destroy();
-
-        List<IndexFile> files = new 
ArrayList<>(indexService.getTimeStoreTable().values());
-        Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, 
files.get(0).getFileStatus());
-        Assert.assertEquals(IndexFile.IndexStatusEnum.SEALED, 
files.get(1).getFileStatus());
-        Assert.assertEquals(IndexFile.IndexStatusEnum.UNSEALED, 
files.get(2).getFileStatus());
-
-        indexFile = indexService.getNextSealedFile();
-        indexService.doCompactThenUploadFile(indexFile);
-        indexService.setCompactTimestamp(indexFile.getTimestamp());
-        files = new ArrayList<>(indexService.getTimeStoreTable().values());
-        Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, 
files.get(0).getFileStatus());
-        Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, 
files.get(1).getFileStatus());
-        Assert.assertEquals(IndexFile.IndexStatusEnum.UNSEALED, 
files.get(2).getFileStatus());
-
-        indexFile = indexService.getNextSealedFile();
-        Assert.assertNull(indexFile);
-        files = new ArrayList<>(indexService.getTimeStoreTable().values());
-        Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, 
files.get(0).getFileStatus());
-        Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, 
files.get(1).getFileStatus());
-        Assert.assertEquals(IndexFile.IndexStatusEnum.UNSEALED, 
files.get(2).getFileStatus());
+        Object backgroundRunLock = new Object();
+        IndexStoreService controlledService = new 
IndexStoreService(fileAllocator, filePath) {
+            @Override
+            public void run() {
+                synchronized (backgroundRunLock) {
+                    super.run();
+                }
+            }
+        };
+        // Keep background compaction out of the manual compaction assertions.
+        synchronized (backgroundRunLock) {
+            try {
+                putKeysConcurrently(controlledService);
+                IndexFile indexFile = indexService.getNextSealedFile();
+                Assert.assertEquals(IndexFile.IndexStatusEnum.SEALED, 
indexFile.getFileStatus());
+
+                
Assert.assertTrue(indexService.doCompactThenUploadFile(indexFile));
+                indexService.setCompactTimestamp(indexFile.getTimestamp());
+                indexFile.destroy();
+
+                List<IndexFile> files = new 
ArrayList<>(indexService.getTimeStoreTable().values());
+                Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, 
files.get(0).getFileStatus());
+                Assert.assertEquals(IndexFile.IndexStatusEnum.SEALED, 
files.get(1).getFileStatus());
+                Assert.assertEquals(IndexFile.IndexStatusEnum.UNSEALED, 
files.get(2).getFileStatus());
+
+                indexFile = indexService.getNextSealedFile();
+                
Assert.assertTrue(indexService.doCompactThenUploadFile(indexFile));
+                indexService.setCompactTimestamp(indexFile.getTimestamp());
+                files = new 
ArrayList<>(indexService.getTimeStoreTable().values());
+                Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, 
files.get(0).getFileStatus());
+                Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, 
files.get(1).getFileStatus());
+                Assert.assertEquals(IndexFile.IndexStatusEnum.UNSEALED, 
files.get(2).getFileStatus());
+
+                indexFile = indexService.getNextSealedFile();
+                Assert.assertNull(indexFile);
+                files = new 
ArrayList<>(indexService.getTimeStoreTable().values());
+                Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, 
files.get(0).getFileStatus());
+                Assert.assertEquals(IndexFile.IndexStatusEnum.UPLOAD, 
files.get(1).getFileStatus());
+                Assert.assertEquals(IndexFile.IndexStatusEnum.UNSEALED, 
files.get(2).getFileStatus());
+            } finally {
+                // Allow run() to perform its normal shutdown cleanup after 
releasing the lock.
+                controlledService.makeStop();
+            }
+        }
     }
 
     @Test

Reply via email to