Keep trying ports until you get one lower than 55535 (#83)

* Keep trying ports until you get one lower than 55535

* Rename method and change to do...while

* Limit attempts to 11,000 to find an open redis cluster port
This commit is contained in:
Ehren Kret 2020-07-07 10:12:31 -05:00 committed by GitHub
parent 42c797ee97
commit 0ee7a66033
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 5 deletions

View File

@ -42,7 +42,7 @@ public abstract class AbstractRedisClusterTest {
clusterNodes = new RedisServer[NODE_COUNT];
for (int i = 0; i < NODE_COUNT; i++) {
clusterNodes[i] = buildClusterNode(getNextPort());
clusterNodes[i] = buildClusterNode(getNextRedisClusterPort());
clusterNodes[i].start();
}
@ -143,10 +143,18 @@ public abstract class AbstractRedisClusterTest {
}
}
private static int getNextPort() throws IOException {
try (ServerSocket socket = new ServerSocket(0)) {
socket.setReuseAddress(false);
return socket.getLocalPort();
private static int getNextRedisClusterPort() throws IOException {
final int MAX_ITERATIONS = 11_000;
int port;
for (int i = 0; i < MAX_ITERATIONS; i++) {
try (ServerSocket socket = new ServerSocket(0)) {
socket.setReuseAddress(false);
port = socket.getLocalPort();
}
if (port < 55535) {
return port;
}
}
throw new IOException("Couldn't find an open port below 55,535 in " + MAX_ITERATIONS + " tries");
}
}