|
2 | 2 |
|
3 | 3 | import io.restassured.response.ValidatableResponse; |
4 | 4 | import jakarta.ws.rs.NotAuthorizedException; |
| 5 | +import org.awaitility.Awaitility; |
5 | 6 | import org.junit.jupiter.api.Test; |
6 | 7 | import org.junit.jupiter.params.ParameterizedTest; |
7 | 8 | import org.junit.jupiter.params.provider.ValueSource; |
|
10 | 11 | import org.testcontainers.containers.ContainerLaunchException; |
11 | 12 |
|
12 | 13 | import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.io.OutputStream; |
13 | 16 | import java.net.InetSocketAddress; |
14 | 17 | import java.net.ServerSocket; |
15 | 18 | import java.net.Socket; |
| 19 | +import java.nio.charset.StandardCharsets; |
16 | 20 | import java.time.Duration; |
17 | 21 | import java.time.Instant; |
18 | 22 |
|
@@ -238,13 +242,30 @@ private static int findFreePort() { |
238 | 242 | } |
239 | 243 | } |
240 | 244 |
|
241 | | - private void testDebugPortAvailable(final String debugHost, final int debugPort) throws IOException { |
242 | | - try (var debugSocket = new Socket()) { |
243 | | - try { |
244 | | - debugSocket.connect(new InetSocketAddress(debugHost, debugPort)); |
245 | | - } catch (IOException e) { |
246 | | - fail(String.format("Debug port %d cannot be reached.", debugPort)); |
247 | | - } |
| 245 | + private void testDebugPortAvailable(String debugHost, int debugPort) { |
| 246 | + Awaitility.await() |
| 247 | + .atMost(Duration.ofSeconds(30)) |
| 248 | + .pollInterval(Duration.ofMillis(500)) |
| 249 | + .pollDelay(Duration.ofSeconds(1)) |
| 250 | + .untilAsserted(() -> assertJdwpHandshake(debugHost, debugPort)); |
| 251 | + } |
| 252 | + |
| 253 | + private void assertJdwpHandshake(String debugHost, int debugPort) throws IOException { |
| 254 | + try (Socket socket = new Socket()) { |
| 255 | + socket.connect(new InetSocketAddress(debugHost, debugPort), 2000); |
| 256 | + socket.setSoTimeout(2000); |
| 257 | + |
| 258 | + // send JDWP Handshake |
| 259 | + OutputStream out = socket.getOutputStream(); |
| 260 | + out.write("JDWP-Handshake".getBytes(StandardCharsets.US_ASCII)); |
| 261 | + out.flush(); |
| 262 | + |
| 263 | + InputStream in = socket.getInputStream(); |
| 264 | + byte[] response = new byte[14]; |
| 265 | + int bytesRead = in.read(response); |
| 266 | + |
| 267 | + assertThat(bytesRead, is(14)); |
| 268 | + assertThat(new String(response, StandardCharsets.US_ASCII), equalTo("JDWP-Handshake")); |
248 | 269 | } |
249 | 270 | } |
250 | 271 | } |
0 commit comments