Search in sources :

Example 1 with TimeoutException

use of org.rnorth.ducttape.TimeoutException 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));
    }
}
Also used : ContainerLaunchException(org.testcontainers.containers.ContainerLaunchException) HttpURLConnection(java.net.HttpURLConnection) IOException(java.io.IOException) URL(java.net.URL) TimeoutException(org.rnorth.ducttape.TimeoutException)

Example 2 with TimeoutException

use of org.rnorth.ducttape.TimeoutException 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)");
    }
}
Also used : ExternalPortListeningCheck(org.testcontainers.containers.wait.internal.ExternalPortListeningCheck) ContainerLaunchException(org.testcontainers.containers.ContainerLaunchException) InternalCommandPortListeningCheck(org.testcontainers.containers.wait.internal.InternalCommandPortListeningCheck) TimeoutException(org.rnorth.ducttape.TimeoutException)

Aggregations

TimeoutException (org.rnorth.ducttape.TimeoutException)2 ContainerLaunchException (org.testcontainers.containers.ContainerLaunchException)2 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 ExternalPortListeningCheck (org.testcontainers.containers.wait.internal.ExternalPortListeningCheck)1 InternalCommandPortListeningCheck (org.testcontainers.containers.wait.internal.InternalCommandPortListeningCheck)1