You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by zh...@apache.org on 2022/03/11 07:09:06 UTC

[hbase] 02/02: HBASE-26824 TestHBaseTestingUtil.testResolvePortConflict failing after HBASE-26582 (#4203)

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

zhangduo pushed a commit to branch branch-2.5
in repository https://gitbox.apache.org/repos/asf/hbase.git

commit cc256bd78f799797f13c171731d8fb4874faaeca
Author: Andrew Purtell <ap...@apache.org>
AuthorDate: Thu Mar 10 22:43:00 2022 -0800

    HBASE-26824 TestHBaseTestingUtil.testResolvePortConflict failing after HBASE-26582 (#4203)
    
    Signed-off-by: Duo Zhang <zh...@apache.org>
    Signed-off-by: Xiaolin Ha <ha...@apache.org>
---
 .../org/apache/hadoop/hbase/HBaseCommonTestingUtility.java | 14 ++++++++++----
 .../org/apache/hadoop/hbase/TestHBaseTestingUtility.java   |  2 +-
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseCommonTestingUtility.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseCommonTestingUtility.java
index 71704ba..e0525fc 100644
--- a/hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseCommonTestingUtility.java
+++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseCommonTestingUtility.java
@@ -23,6 +23,7 @@ import java.net.ServerSocket;
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Random;
 import java.util.Set;
 import java.util.UUID;
 import java.util.concurrent.ThreadLocalRandom;
@@ -280,10 +281,11 @@ public class HBaseCommonTestingUtility {
 
     /** A set of ports that have been claimed using {@link #randomFreePort()}. */
     private final Set<Integer> takenRandomPorts = new HashSet<>();
-
+    private final Random random;
     private final AvailablePortChecker portChecker;
 
     public PortAllocator() {
+      this.random = new Random();
       this.portChecker = new AvailablePortChecker() {
         @Override
         public boolean available(int port) {
@@ -298,10 +300,15 @@ public class HBaseCommonTestingUtility {
       };
     }
 
-    public PortAllocator(AvailablePortChecker portChecker) {
+    public PortAllocator(Random random, AvailablePortChecker portChecker) {
+      this.random = random;
       this.portChecker = portChecker;
     }
 
+    public PortAllocator(AvailablePortChecker portChecker) {
+      this(new Random(), portChecker);
+    }
+
     /**
      * Returns a random free port and marks that port as taken. Not thread-safe. Expected to be
      * called from single-threaded test setup code/
@@ -328,8 +335,7 @@ public class HBaseCommonTestingUtility {
      * intended for dynamic allocation (see http://bit.ly/dynports).
      */
     private int randomPort() {
-      return MIN_RANDOM_PORT
-        + ThreadLocalRandom.current().nextInt(MAX_RANDOM_PORT - MIN_RANDOM_PORT);
+      return MIN_RANDOM_PORT + random.nextInt(MAX_RANDOM_PORT - MIN_RANDOM_PORT);
     }
 
     interface AvailablePortChecker {
diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/TestHBaseTestingUtility.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestHBaseTestingUtility.java
index 3182e62..993af21 100644
--- a/hbase-server/src/test/java/org/apache/hadoop/hbase/TestHBaseTestingUtility.java
+++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestHBaseTestingUtility.java
@@ -428,7 +428,7 @@ public class TestHBaseTestingUtility {
     when(portChecker.available(anyInt())).thenReturn(true);
 
     HBaseTestingUtility.PortAllocator portAllocator =
-      new HBaseTestingUtility.PortAllocator(portChecker);
+      new HBaseTestingUtility.PortAllocator(random, portChecker);
 
     int port1 = portAllocator.randomFreePort();
     int port2 = portAllocator.randomFreePort();