HBASE-26824 TestHBaseTestingUtil.testResolvePortConflict failing after HBASE-26582 (#4203)
Signed-off-by: Duo Zhang <zhangduo@apache.org> Signed-off-by: Xiaolin Ha <haxiaolin@apache.org>
This commit is contained in:
parent
dbf56819c3
commit
f3faa26047
|
@ -23,6 +23,7 @@ import java.net.ServerSocket;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
@ -266,10 +267,11 @@ public class HBaseCommonTestingUtil {
|
||||||
|
|
||||||
/** A set of ports that have been claimed using {@link #randomFreePort()}. */
|
/** A set of ports that have been claimed using {@link #randomFreePort()}. */
|
||||||
private final Set<Integer> takenRandomPorts = new HashSet<>();
|
private final Set<Integer> takenRandomPorts = new HashSet<>();
|
||||||
|
private final Random random;
|
||||||
private final AvailablePortChecker portChecker;
|
private final AvailablePortChecker portChecker;
|
||||||
|
|
||||||
public PortAllocator() {
|
public PortAllocator() {
|
||||||
|
this.random = new Random();
|
||||||
this.portChecker = new AvailablePortChecker() {
|
this.portChecker = new AvailablePortChecker() {
|
||||||
@Override
|
@Override
|
||||||
public boolean available(int port) {
|
public boolean available(int port) {
|
||||||
|
@ -284,10 +286,15 @@ public class HBaseCommonTestingUtil {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public PortAllocator(AvailablePortChecker portChecker) {
|
public PortAllocator(Random random, AvailablePortChecker portChecker) {
|
||||||
|
this.random = random;
|
||||||
this.portChecker = portChecker;
|
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
|
* Returns a random free port and marks that port as taken. Not thread-safe. Expected to be
|
||||||
* called from single-threaded test setup code/
|
* called from single-threaded test setup code/
|
||||||
|
@ -314,8 +321,7 @@ public class HBaseCommonTestingUtil {
|
||||||
* dynamic allocation (see http://bit.ly/dynports).
|
* dynamic allocation (see http://bit.ly/dynports).
|
||||||
*/
|
*/
|
||||||
private int randomPort() {
|
private int randomPort() {
|
||||||
return MIN_RANDOM_PORT +
|
return MIN_RANDOM_PORT + random.nextInt(MAX_RANDOM_PORT - MIN_RANDOM_PORT);
|
||||||
ThreadLocalRandom.current().nextInt(MAX_RANDOM_PORT - MIN_RANDOM_PORT);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface AvailablePortChecker {
|
interface AvailablePortChecker {
|
||||||
|
|
|
@ -429,7 +429,7 @@ public class TestHBaseTestingUtil {
|
||||||
when(portChecker.available(anyInt())).thenReturn(true);
|
when(portChecker.available(anyInt())).thenReturn(true);
|
||||||
|
|
||||||
HBaseTestingUtil.PortAllocator portAllocator =
|
HBaseTestingUtil.PortAllocator portAllocator =
|
||||||
new HBaseTestingUtil.PortAllocator(portChecker);
|
new HBaseTestingUtil.PortAllocator(random, portChecker);
|
||||||
|
|
||||||
int port1 = portAllocator.randomFreePort();
|
int port1 = portAllocator.randomFreePort();
|
||||||
int port2 = portAllocator.randomFreePort();
|
int port2 = portAllocator.randomFreePort();
|
||||||
|
|
Loading…
Reference in New Issue