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

tanxinyu pushed a commit to branch rel/1.1
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/rel/1.1 by this push:
     new 987f5b8d0fa [To rel/1.1][IOTDB-6061] Fix the instability failure 
caused by initServer in IoTConsensus UT not binding to the corresponding port  
(#10534)
987f5b8d0fa is described below

commit 987f5b8d0faf56e941ec82d05a975364dff6f0a0
Author: Potato <[email protected]>
AuthorDate: Fri Jul 28 17:06:20 2023 +0800

    [To rel/1.1][IOTDB-6061] Fix the instability failure caused by initServer 
in IoTConsensus UT not binding to the corresponding port  (#10534)
---
 .../apache/iotdb/consensus/iot/ReplicateTest.java  | 30 +++++++++++++++++++---
 .../apache/iotdb/consensus/iot/StabilityTest.java  | 13 ++++++----
 2 files changed, 35 insertions(+), 8 deletions(-)

diff --git 
a/consensus/src/test/java/org/apache/iotdb/consensus/iot/ReplicateTest.java 
b/consensus/src/test/java/org/apache/iotdb/consensus/iot/ReplicateTest.java
index a36db6e3ea4..0242cbc4e42 100644
--- a/consensus/src/test/java/org/apache/iotdb/consensus/iot/ReplicateTest.java
+++ b/consensus/src/test/java/org/apache/iotdb/consensus/iot/ReplicateTest.java
@@ -39,9 +39,11 @@ import org.slf4j.LoggerFactory;
 
 import java.io.File;
 import java.io.IOException;
+import java.net.ServerSocket;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 public class ReplicateTest {
   private static final long CHECK_POINT_GAP = 500;
@@ -49,6 +51,8 @@ public class ReplicateTest {
 
   private final ConsensusGroupId gid = new DataRegionId(1);
 
+  private static final long timeout = TimeUnit.SECONDS.toMillis(300);
+
   private final List<Peer> peers =
       Arrays.asList(
           new Peer(gid, 1, new TEndPoint("127.0.0.1", 6000)),
@@ -67,8 +71,8 @@ public class ReplicateTest {
 
   @Before
   public void setUp() throws Exception {
-    for (int i = 0; i < 3; i++) {
-      peersStorage.get(i).mkdirs();
+    for (File file : peersStorage) {
+      file.mkdirs();
       stateMachines.add(new TestStateMachine());
     }
     initServer();
@@ -83,7 +87,10 @@ public class ReplicateTest {
   }
 
   private void initServer() throws IOException {
-    for (int i = 0; i < 3; i++) {
+    for (Peer peer : peers) {
+      waitPortAvailable(peer.getEndpoint().port);
+    }
+    for (int i = 0; i < peers.size(); i++) {
       int finalI = i;
       servers.add(
           (IoTConsensus)
@@ -244,4 +251,21 @@ public class ReplicateTest {
     Assert.assertEquals(stateMachines.get(0).getData(), 
stateMachines.get(1).getData());
     Assert.assertEquals(stateMachines.get(2).getData(), 
stateMachines.get(1).getData());
   }
+
+  private static void waitPortAvailable(int port) {
+    long start = System.currentTimeMillis();
+    while (System.currentTimeMillis() - start < timeout) {
+      try (ServerSocket ignored = new ServerSocket(port)) {
+        return;
+      } catch (IOException e) {
+        // Port is already in use, wait and retry
+        try {
+          Thread.sleep(1000); // Wait for 1 second before retrying
+        } catch (InterruptedException ex) {
+          // Handle the interruption if needed
+        }
+      }
+    }
+    Assert.fail(String.format("can not bind port %d after 300s", port));
+  }
 }
diff --git 
a/consensus/src/test/java/org/apache/iotdb/consensus/iot/StabilityTest.java 
b/consensus/src/test/java/org/apache/iotdb/consensus/iot/StabilityTest.java
index ea7c15c8f1a..9ead1f2bf5d 100644
--- a/consensus/src/test/java/org/apache/iotdb/consensus/iot/StabilityTest.java
+++ b/consensus/src/test/java/org/apache/iotdb/consensus/iot/StabilityTest.java
@@ -48,13 +48,15 @@ public class StabilityTest {
 
   private IConsensus consensusImpl;
 
+  private final int basePort = 9000;
+
   public void constructConsensus() throws IOException {
     consensusImpl =
         ConsensusFactory.getConsensusImpl(
                 ConsensusFactory.IOT_CONSENSUS,
                 ConsensusConfig.newBuilder()
                     .setThisNodeId(1)
-                    .setThisNode(new TEndPoint("0.0.0.0", 9000))
+                    .setThisNode(new TEndPoint("0.0.0.0", basePort))
                     .setStorageDir(storageDir.getAbsolutePath())
                     .build(),
                 gid -> new TestStateMachine())
@@ -88,7 +90,7 @@ public class StabilityTest {
   public void peerTest() throws Exception {
     consensusImpl.createPeer(
         dataRegionId,
-        Collections.singletonList(new Peer(dataRegionId, 1, new 
TEndPoint("0.0.0.0", 9000))));
+        Collections.singletonList(new Peer(dataRegionId, 1, new 
TEndPoint("0.0.0.0", basePort))));
 
     consensusImpl.deletePeer(dataRegionId);
 
@@ -100,7 +102,8 @@ public class StabilityTest {
     ConsensusGenericResponse response =
         consensusImpl.createPeer(
             dataRegionId,
-            Collections.singletonList(new Peer(dataRegionId, 1, new 
TEndPoint("0.0.0.0", 9000))));
+            Collections.singletonList(
+                new Peer(dataRegionId, 1, new TEndPoint("0.0.0.0", 
basePort))));
     Assert.assertTrue(response.isSuccess());
     consensusImpl.deletePeer(dataRegionId);
   }
@@ -108,7 +111,7 @@ public class StabilityTest {
   public void snapshotTest() throws IOException {
     consensusImpl.createPeer(
         dataRegionId,
-        Collections.singletonList(new Peer(dataRegionId, 1, new 
TEndPoint("0.0.0.0", 9000))));
+        Collections.singletonList(new Peer(dataRegionId, 1, new 
TEndPoint("0.0.0.0", basePort))));
     consensusImpl.triggerSnapshot(dataRegionId);
 
     File dataDir = new File(IoTConsensus.buildPeerDir(storageDir, 
dataRegionId));
@@ -133,7 +136,7 @@ public class StabilityTest {
   public void snapshotUpgradeTest() throws Exception {
     consensusImpl.createPeer(
         dataRegionId,
-        Collections.singletonList(new Peer(dataRegionId, 1, new 
TEndPoint("0.0.0.0", 9000))));
+        Collections.singletonList(new Peer(dataRegionId, 1, new 
TEndPoint("0.0.0.0", basePort))));
     consensusImpl.triggerSnapshot(dataRegionId);
     long oldSnapshotIndex = System.currentTimeMillis();
     String oldSnapshotDirName =

Reply via email to