Skip to content

Commit

Permalink
HBASE-26824 TestHBaseTestingUtil.testResolvePortConflict failing after
Browse files Browse the repository at this point in the history
…HBASE-26582 (#4203)

Signed-off-by: Duo Zhang <[email protected]>
Signed-off-by: Xiaolin Ha <[email protected]>
  • Loading branch information
apurtell authored and Apache9 committed Mar 11, 2022
1 parent e9711a6 commit 097e19e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -280,10 +281,11 @@ static class PortAllocator {

/** 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) {
Expand All @@ -298,10 +300,15 @@ public boolean available(int port) {
};
}

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/
Expand All @@ -328,8 +335,7 @@ public int randomFreePort() {
* 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ public Integer answer(InvocationOnMock invocation) {
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();
Expand Down

0 comments on commit 097e19e

Please sign in to comment.