use of org.testcontainers.containers.ContainerLaunchException in project testcontainers-java by testcontainers.
the class HttpWaitStrategy method waitUntilReady.
@Override
protected void waitUntilReady() {
final String containerName = waitStrategyTarget.getContainerInfo().getName();
final Set<Integer> livenessCheckPorts = getLivenessCheckPorts();
if (livenessCheckPorts == null || livenessCheckPorts.isEmpty()) {
log.warn("{}: No exposed ports or mapped ports - cannot wait for status", containerName);
return;
}
final Integer livenessCheckPort = livenessCheckPorts.iterator().next();
final String uri = buildLivenessUri(livenessCheckPort).toString();
log.info("{}: Waiting for {} seconds for URL: {}", containerName, startupTimeout.getSeconds(), uri);
// try to connect to the URL
try {
retryUntilSuccess((int) startupTimeout.getSeconds(), TimeUnit.SECONDS, () -> {
getRateLimiter().doWhenReady(() -> {
try {
final HttpURLConnection connection = (HttpURLConnection) new URL(uri).openConnection();
// authenticate
if (!Strings.isNullOrEmpty(username)) {
connection.setRequestProperty(HEADER_AUTHORIZATION, buildAuthString(username, password));
connection.setUseCaches(false);
}
connection.setRequestMethod("GET");
connection.connect();
if (statusCode != connection.getResponseCode()) {
throw new RuntimeException(String.format("HTTP response code was: %s", connection.getResponseCode()));
}
if (responsePredicate != null) {
String responseBody = getResponseBody(connection);
if (!responsePredicate.test(responseBody)) {
throw new RuntimeException(String.format("Response: %s did not match predicate", responseBody));
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
});
return true;
});
} catch (TimeoutException e) {
throw new ContainerLaunchException(String.format("Timed out waiting for URL to be accessible (%s should return HTTP %s)", uri, statusCode));
}
}
use of org.testcontainers.containers.ContainerLaunchException in project testcontainers-java by testcontainers.
the class HostPortWaitStrategy method waitUntilReady.
@Override
protected void waitUntilReady() {
final Set<Integer> externalLivenessCheckPorts = getLivenessCheckPorts();
if (externalLivenessCheckPorts.isEmpty()) {
log.debug("Liveness check ports of {} is empty. Not waiting.", waitStrategyTarget.getContainerInfo().getName());
return;
}
@SuppressWarnings("unchecked") List<Integer> exposedPorts = waitStrategyTarget.getExposedPorts();
final Set<Integer> internalPorts = getInternalPorts(externalLivenessCheckPorts, exposedPorts);
Callable<Boolean> internalCheck = new InternalCommandPortListeningCheck(waitStrategyTarget, internalPorts);
Callable<Boolean> externalCheck = new ExternalPortListeningCheck(waitStrategyTarget, externalLivenessCheckPorts);
try {
Unreliables.retryUntilTrue((int) startupTimeout.getSeconds(), TimeUnit.SECONDS, () -> getRateLimiter().getWhenReady(() -> internalCheck.call() && externalCheck.call()));
} catch (TimeoutException e) {
throw new ContainerLaunchException("Timed out waiting for container port to open (" + waitStrategyTarget.getContainerIpAddress() + " ports: " + externalLivenessCheckPorts + " should be listening)");
}
}
use of org.testcontainers.containers.ContainerLaunchException in project testcontainers-java by testcontainers.
the class LogMessageWaitStrategy method waitUntilReady.
@Override
protected void waitUntilReady() {
WaitingConsumer waitingConsumer = new WaitingConsumer();
LogUtils.followOutput(DockerClientFactory.instance().client(), waitStrategyTarget.getContainerId(), waitingConsumer);
Predicate<OutputFrame> waitPredicate = outputFrame -> outputFrame.getUtf8String().matches(regEx);
try {
waitingConsumer.waitUntil(waitPredicate, startupTimeout.getSeconds(), TimeUnit.SECONDS, times);
} catch (TimeoutException e) {
throw new ContainerLaunchException("Timed out waiting for log output matching '" + regEx + "'");
}
}
Aggregations