Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HBASE-26824 TestHBaseTestingUtil.testResolvePortConflict failing after HBASE-26582 #4203

Merged
merged 1 commit into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -266,10 +267,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 @@ -284,10 +286,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 @@ -314,8 +321,7 @@ public int randomFreePort() {
* 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 @@ -429,7 +429,7 @@ public Integer answer(InvocationOnMock invocation) {
when(portChecker.available(anyInt())).thenReturn(true);

HBaseTestingUtil.PortAllocator portAllocator =
new HBaseTestingUtil.PortAllocator(portChecker);
new HBaseTestingUtil.PortAllocator(random, portChecker);

int port1 = portAllocator.randomFreePort();
int port2 = portAllocator.randomFreePort();
Expand Down