Skip to content

Commit bafba81

Browse files
committed
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 b1f2585 commit bafba81

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
@@ -258,7 +258,7 @@ protected void configure() {
258258

259259
if (debugEnabled) {
260260
withEnv("DEBUG", Boolean.toString(Boolean.TRUE));
261-
withEnv("DEBUG_PORT", String.valueOf(KEYCLOAK_PORT_DEBUG));
261+
withEnv("DEBUG_PORT", "0.0.0.0:" + KEYCLOAK_PORT_DEBUG);
262262
if (debugHostPort > 0) {
263263
addFixedExposedPort(debugHostPort, KEYCLOAK_PORT_DEBUG);
264264
} 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

@@ -229,13 +233,30 @@ private static int findFreePort() {
229233
}
230234
}
231235

232-
private void testDebugPortAvailable(final String debugHost, final int debugPort) throws IOException {
233-
try (var debugSocket = new Socket()) {
234-
try {
235-
debugSocket.connect(new InetSocketAddress(debugHost, debugPort));
236-
} catch (IOException e) {
237-
fail(String.format("Debug port %d cannot be reached.", debugPort));
238-
}
236+
private void testDebugPortAvailable(String debugHost, int debugPort) {
237+
Awaitility.await()
238+
.atMost(Duration.ofSeconds(30))
239+
.pollInterval(Duration.ofMillis(500))
240+
.pollDelay(Duration.ofSeconds(1))
241+
.untilAsserted(() -> assertJdwpHandshake(debugHost, debugPort));
242+
}
243+
244+
private void assertJdwpHandshake(String debugHost, int debugPort) throws IOException {
245+
try (Socket socket = new Socket()) {
246+
socket.connect(new InetSocketAddress(debugHost, debugPort), 2000);
247+
socket.setSoTimeout(2000);
248+
249+
// send JDWP Handshake
250+
OutputStream out = socket.getOutputStream();
251+
out.write("JDWP-Handshake".getBytes(StandardCharsets.US_ASCII));
252+
out.flush();
253+
254+
InputStream in = socket.getInputStream();
255+
byte[] response = new byte[14];
256+
int bytesRead = in.read(response);
257+
258+
assertThat(bytesRead, is(14));
259+
assertThat(new String(response, StandardCharsets.US_ASCII), equalTo("JDWP-Handshake"));
239260
}
240261
}
241262
}

0 commit comments

Comments
 (0)