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

JackieTien97 pushed a commit to branch ty/fix-stability-test-restart-port
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit 7f4759d5a98163cd336848d506e574d32599da82
Author: JackieTien97 <[email protected]>
AuthorDate: Tue Nov 24 11:28:03 2026 +0800

    Fix flaky StabilityTest.recordAndResetPeerListTest restart and port 
collision
---
 .../apache/iotdb/consensus/iot/StabilityTest.java  | 103 +++++++++------------
 1 file changed, 42 insertions(+), 61 deletions(-)

diff --git 
a/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/StabilityTest.java
 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/StabilityTest.java
index 9844b2f1e7e..692cefc3524 100644
--- 
a/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/StabilityTest.java
+++ 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/StabilityTest.java
@@ -23,7 +23,6 @@ import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
 import org.apache.iotdb.common.rpc.thrift.TEndPoint;
 import org.apache.iotdb.commons.consensus.ConsensusGroupId;
 import org.apache.iotdb.commons.consensus.DataRegionId;
-import org.apache.iotdb.commons.exception.StartupException;
 import org.apache.iotdb.consensus.ConsensusFactory;
 import org.apache.iotdb.consensus.common.Peer;
 import org.apache.iotdb.consensus.config.ConsensusConfig;
@@ -39,8 +38,6 @@ import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import java.io.File;
 import java.io.IOException;
@@ -55,8 +52,6 @@ import static org.junit.Assert.assertTrue;
 
 public class StabilityTest {
 
-  private static Logger LOGGER = LoggerFactory.getLogger(StabilityTest.class);
-
   private final ConsensusGroupId dataRegionId = new DataRegionId(1);
 
   private final File storageDir = new File("target" + java.io.File.separator + 
"stability");
@@ -69,27 +64,30 @@ public class StabilityTest {
 
   private IoTConsensus consensusImpl;
 
-  private final int basePort = 6667;
+  private final int basePort = 0;
+
+  private IoTConsensus createConsensus() {
+    return (IoTConsensus)
+        ConsensusFactory.getConsensusImpl(
+                ConsensusFactory.IOT_CONSENSUS,
+                ConsensusConfig.newBuilder()
+                    .setThisNodeId(1)
+                    .setThisNode(new TEndPoint("0.0.0.0", basePort))
+                    .setStorageDir(storageDir.getAbsolutePath())
+                    .setRecvSnapshotDirs(recvSnapshotDirs)
+                    .setConsensusGroupType(TConsensusGroupType.DataRegion)
+                    .build(),
+                gid -> new TestStateMachine())
+            .orElseThrow(
+                () ->
+                    new IllegalArgumentException(
+                        String.format(
+                            ConsensusFactory.CONSTRUCT_FAILED_MSG,
+                            ConsensusFactory.IOT_CONSENSUS)));
+  }
 
   public void constructConsensus() throws IOException {
-    consensusImpl =
-        (IoTConsensus)
-            ConsensusFactory.getConsensusImpl(
-                    ConsensusFactory.IOT_CONSENSUS,
-                    ConsensusConfig.newBuilder()
-                        .setThisNodeId(1)
-                        .setThisNode(new TEndPoint("0.0.0.0", basePort))
-                        .setStorageDir(storageDir.getAbsolutePath())
-                        .setRecvSnapshotDirs(recvSnapshotDirs)
-                        .setConsensusGroupType(TConsensusGroupType.DataRegion)
-                        .build(),
-                    gid -> new TestStateMachine())
-                .orElseThrow(
-                    () ->
-                        new IllegalArgumentException(
-                            String.format(
-                                ConsensusFactory.CONSTRUCT_FAILED_MSG,
-                                ConsensusFactory.IOT_CONSENSUS)));
+    consensusImpl = createConsensus();
     consensusImpl.start();
   }
 
@@ -101,7 +99,10 @@ public class StabilityTest {
 
   @After
   public void tearDown() throws IOException {
-    consensusImpl.stop();
+    if (consensusImpl != null) {
+      consensusImpl.stop();
+      consensusImpl = null;
+    }
     FileUtils.deleteFully(storageDir);
   }
 
@@ -255,6 +256,7 @@ public class StabilityTest {
       Assert.fail();
     }
     consensusImpl.stop();
+    consensusImpl = null;
 
     // test add sync channel
     Map<ConsensusGroupId, List<Peer>> correctPeers = new HashMap<>();
@@ -262,54 +264,33 @@ public class StabilityTest {
     peerList1And2.add(new Peer(dataRegionId, 1, new TEndPoint("0.0.0.0", 
basePort)));
     peerList1And2.add(new Peer(dataRegionId, 2, new TEndPoint("0.0.0.0", 
basePort)));
     correctPeers.put(dataRegionId, peerList1And2);
+    consensusImpl = createConsensus();
     consensusImpl.recordCorrectPeerListBeforeStarting(correctPeers);
-    try {
-      consensusImpl.start();
-      Assert.assertEquals(2, 
consensusImpl.getImpl(dataRegionId).getConfiguration().size());
-      consensusImpl.stop();
-    } catch (IOException e) {
-      if (e.getCause() instanceof StartupException) {
-        LOGGER.info("Cannot start IoTConsensus because", e);
-      } else {
-        LOGGER.error("Failed because", e);
-        Assert.fail(e.getMessage());
-      }
-    }
+    consensusImpl.start();
+    Assert.assertEquals(2, 
consensusImpl.getImpl(dataRegionId).getConfiguration().size());
+    consensusImpl.stop();
+    consensusImpl = null;
 
     // test remove sync channel
     List<Peer> peerList1 = new ArrayList<>();
     peerList1.add(new Peer(dataRegionId, 1, new TEndPoint("0.0.0.0", 
basePort)));
     correctPeers.put(dataRegionId, peerList1);
+    consensusImpl = createConsensus();
     consensusImpl.recordCorrectPeerListBeforeStarting(correctPeers);
-    try {
-      consensusImpl.start();
-      Assert.assertEquals(1, 
consensusImpl.getImpl(dataRegionId).getConfiguration().size());
-      consensusImpl.stop();
-    } catch (IOException e) {
-      if (e.getCause() instanceof StartupException) {
-        LOGGER.info("Cannot start IoTConsensus because", e);
-      } else {
-        LOGGER.error("Failed because", e);
-        Assert.fail(e.getMessage());
-      }
-    }
+    consensusImpl.start();
+    Assert.assertEquals(1, 
consensusImpl.getImpl(dataRegionId).getConfiguration().size());
+    consensusImpl.stop();
+    consensusImpl = null;
 
     // test remove invalid peer
     List<Peer> peerList2 = new ArrayList<>();
     peerList2.add(new Peer(dataRegionId, 2, new TEndPoint("0.0.0.0", 
basePort)));
     correctPeers.put(dataRegionId, peerList2);
+    consensusImpl = createConsensus();
     consensusImpl.recordCorrectPeerListBeforeStarting(correctPeers);
-    try {
-      consensusImpl.start();
-      Assert.assertNull(consensusImpl.getImpl(dataRegionId));
-      consensusImpl.stop();
-    } catch (IOException e) {
-      if (e.getCause() instanceof StartupException) {
-        LOGGER.info("Cannot start IoTConsensus because", e);
-      } else {
-        LOGGER.error("Failed because", e);
-        Assert.fail(e.getMessage());
-      }
-    }
+    consensusImpl.start();
+    Assert.assertNull(consensusImpl.getImpl(dataRegionId));
+    consensusImpl.stop();
+    consensusImpl = null;
   }
 }

Reply via email to