Skip to content

Commit fb76cb2

Browse files
authored
Set DEBUG_PORT to bind explicitly with "0.0.0.0" for debug compatibility (#260)
closes #259 Signed-off-by: Niko Köbler <niko@n-k.de>
1 parent 85e4f74 commit fb76cb2

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@
185185
<version>${shrinkwrap-resolver.version}</version>
186186
<scope>test</scope>
187187
</dependency>
188+
<dependency>
189+
<groupId>org.awaitility</groupId>
190+
<artifactId>awaitility</artifactId>
191+
<version>4.3.0</version>
192+
<scope>test</scope>
193+
</dependency>
188194
</dependencies>
189195

190196
<build>

src/main/java/dasniko/testcontainers/keycloak/ExtendableKeycloakContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ protected void configure() {
260260

261261
if (debugEnabled) {
262262
withEnv("DEBUG", Boolean.toString(Boolean.TRUE));
263-
withEnv("DEBUG_PORT", String.valueOf(KEYCLOAK_PORT_DEBUG));
263+
withEnv("DEBUG_PORT", "0.0.0.0:" + KEYCLOAK_PORT_DEBUG);
264264
if (debugHostPort > 0) {
265265
addFixedExposedPort(debugHostPort, KEYCLOAK_PORT_DEBUG);
266266
} else {

src/test/java/dasniko/testcontainers/keycloak/KeycloakContainerTest.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.restassured.response.ValidatableResponse;
44
import jakarta.ws.rs.NotAuthorizedException;
5+
import org.awaitility.Awaitility;
56
import org.junit.jupiter.api.Test;
67
import org.junit.jupiter.params.ParameterizedTest;
78
import org.junit.jupiter.params.provider.ValueSource;
@@ -10,9 +11,12 @@
1011
import org.testcontainers.containers.ContainerLaunchException;
1112

1213
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.io.OutputStream;
1316
import java.net.InetSocketAddress;
1417
import java.net.ServerSocket;
1518
import java.net.Socket;
19+
import java.nio.charset.StandardCharsets;
1620
import java.time.Duration;
1721
import java.time.Instant;
1822

@@ -238,13 +242,30 @@ private static int findFreePort() {
238242
}
239243
}
240244

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"));
248269
}
249270
}
250271
}

0 commit comments

Comments
 (0)