2525import opengrok .auth .plugin .ldap .LdapServer ;
2626import org .junit .jupiter .api .Test ;
2727import org .mockito .Mockito ;
28- import org .opengrok .web .api .v1 .controller .PortChecker ;
2928
3029import java .io .IOException ;
3130import java .net .InetAddress ;
3231import java .net .ServerSocket ;
3332import java .net .Socket ;
3433import java .net .URISyntaxException ;
3534import java .net .UnknownHostException ;
36- import java .util .Random ;
3735
3836import static org .junit .jupiter .api .Assertions .assertEquals ;
3937import 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 ++) {
0 commit comments