Skip to content

Commit 476443e

Browse files
author
Vladimir Kotal
committed
use port 0
1 parent cb97e87 commit 476443e

File tree

2 files changed

+2
-95
lines changed

2 files changed

+2
-95
lines changed

plugins/src/test/java/opengrok/auth/plugin/LdapServerTest.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@
2525
import opengrok.auth.plugin.ldap.LdapServer;
2626
import org.junit.jupiter.api.Test;
2727
import org.mockito.Mockito;
28-
import org.opengrok.web.api.v1.controller.PortChecker;
2928

3029
import java.io.IOException;
3130
import java.net.InetAddress;
3231
import java.net.ServerSocket;
3332
import java.net.Socket;
3433
import java.net.URISyntaxException;
3534
import java.net.UnknownHostException;
36-
import java.util.Random;
3735

3836
import static org.junit.jupiter.api.Assertions.assertEquals;
3937
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -81,26 +79,9 @@ public void testSetGetUsername() {
8179

8280
@Test
8381
public void testIsReachable() throws IOException, InterruptedException, URISyntaxException {
84-
final int testPortBase = 6336; // It has to be > 1024 to avoid BindException due to permission denied.
85-
final int randomRange = 49152;
86-
int triesCount = 0;
87-
final int MAX_PORT_TRIES = 20;
88-
Random rand = new Random();
89-
int testPort;
90-
while (true) {
91-
testPort = testPortBase + rand.nextInt(randomRange);
92-
if (PortChecker.available(testPort)) {
93-
break;
94-
}
95-
if (++triesCount > MAX_PORT_TRIES) {
96-
throw new RuntimeException("Could not find an available port after " +
97-
MAX_PORT_TRIES + " tries");
98-
}
99-
}
100-
10182
// Start simple TCP server on test port.
10283
InetAddress localhostAddr = InetAddress.getLocalHost();
103-
ServerSocket serverSocket = new ServerSocket(testPort, 1, localhostAddr);
84+
ServerSocket serverSocket = new ServerSocket(0, 1, localhostAddr);
10485
Thread thread = new Thread(() -> {
10586
try {
10687
while (true) {
@@ -112,6 +93,7 @@ public void testIsReachable() throws IOException, InterruptedException, URISynta
11293
}
11394
});
11495

96+
int testPort = serverSocket.getLocalPort();
11597
thread.start();
11698
Socket socket = null;
11799
for (int i = 0; i < 3; i++) {

plugins/src/test/java/opengrok/auth/plugin/PortChecker.java

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)