25
25
import opengrok .auth .plugin .ldap .LdapServer ;
26
26
import org .junit .jupiter .api .Test ;
27
27
import org .mockito .Mockito ;
28
- import org .opengrok .web .api .v1 .controller .PortChecker ;
29
28
30
29
import java .io .IOException ;
31
30
import java .net .InetAddress ;
32
31
import java .net .ServerSocket ;
33
32
import java .net .Socket ;
34
33
import java .net .URISyntaxException ;
35
34
import java .net .UnknownHostException ;
36
- import java .util .Random ;
37
35
38
36
import static org .junit .jupiter .api .Assertions .assertEquals ;
39
37
import static org .junit .jupiter .api .Assertions .assertFalse ;
@@ -81,26 +79,9 @@ public void testSetGetUsername() {
81
79
82
80
@ Test
83
81
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
-
101
82
// Start simple TCP server on test port.
102
83
InetAddress localhostAddr = InetAddress .getLocalHost ();
103
- ServerSocket serverSocket = new ServerSocket (testPort , 1 , localhostAddr );
84
+ ServerSocket serverSocket = new ServerSocket (0 , 1 , localhostAddr );
104
85
Thread thread = new Thread (() -> {
105
86
try {
106
87
while (true ) {
@@ -112,6 +93,7 @@ public void testIsReachable() throws IOException, InterruptedException, URISynta
112
93
}
113
94
});
114
95
96
+ int testPort = serverSocket .getLocalPort ();
115
97
thread .start ();
116
98
Socket socket = null ;
117
99
for (int i = 0 ; i < 3 ; i ++) {
0 commit comments