Skip to content

Commit 8f13e42

Browse files
aguibertrnorth
andauthored
Show port mappings in HttpWaitStrategy (#2341)
* Show port mappings in HttpWaitStrategy * Use stream to un-map exposed port * Catch and log failures when logging Co-authored-by: Richard North <[email protected]>
1 parent 140fd94 commit 8f13e42

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

core/src/main/java/org/testcontainers/containers/wait/strategy/HttpWaitStrategy.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,20 @@ protected void waitUntilReady() {
162162
if (null == livenessCheckPort || -1 == livenessCheckPort) {
163163
return;
164164
}
165-
final String uri = buildLivenessUri(livenessCheckPort).toString();
166-
log.info("{}: Waiting for {} seconds for URL: {}", containerName, startupTimeout.getSeconds(), uri);
165+
final URI rawUri = buildLivenessUri(livenessCheckPort);
166+
final String uri = rawUri.toString();
167+
168+
try {
169+
// Un-map the port for logging
170+
int originalPort = waitStrategyTarget.getExposedPorts().stream()
171+
.filter(exposedPort -> rawUri.getPort() == waitStrategyTarget.getMappedPort(exposedPort))
172+
.findFirst()
173+
.orElseThrow(() -> new IllegalStateException("Target port " + rawUri.getPort() + " is not exposed"));
174+
log.info("{}: Waiting for {} seconds for URL: {} (where port {} maps to container port {})", containerName, startupTimeout.getSeconds(), uri, rawUri.getPort(), originalPort);
175+
} catch (RuntimeException e) {
176+
// do not allow a failure in logging to prevent progress, but log for diagnosis
177+
log.warn("Unexpected error occurred - will proceed to try to wait anyway", e);
178+
}
167179

168180
// try to connect to the URL
169181
try {
@@ -243,7 +255,7 @@ private URI buildLivenessUri(int livenessCheckPort) {
243255
if ((tlsEnabled && 443 == livenessCheckPort) || (!tlsEnabled && 80 == livenessCheckPort)) {
244256
portSuffix = "";
245257
} else {
246-
portSuffix = ":" + String.valueOf(livenessCheckPort);
258+
portSuffix = ":" + livenessCheckPort;
247259
}
248260

249261
return URI.create(scheme + host + portSuffix + path);

0 commit comments

Comments
 (0)