testcontainers / testcontainers-java

Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
https://testcontainers.org
MIT License
8.05k stars 1.66k forks source link

[Enhancement]: Reduce exceptions caused by Unreliables.retryUntilTrue #6833

Open dreis2211 opened 1 year ago

dreis2211 commented 1 year ago

Module

Core

Proposal

Hi 👋

I've been profiling test pipelines in our in-house projects and noticed on several Spring-Boot related projects that use DockerComposeContainer that a large amount of exceptions is thrown when waiting for the containers to start.

image

Throwing RuntimeExceptions with the message "Not ready yet" is not really exceptional here - on the contrary. It's rather the norm. I can't imagine this to be super efficient. It would be great if an alternative solution could be implemented that isn't based on exceptions. Or short-term alternative: throwing a dedicate exception with fillInStackTrace overridden to at least avoid the overhead of generating the stacktrace.

    class NotReadyYetException extends RuntimeException {

        public NotReadyYetException() {
            super("Not ready yet");
        }

        @Override
        public synchronized Throwable fillInStackTrace() {
            return this;
        }

    }

Or caching the exception....

Because duct-tape seems to be read only, I hope this is the correct place for this report. Please lead me to the correct place if not. I'm also open to provide the short term solution somewhere if you want me to and you think this is worthwhile.

Let me know what you think.

Cheers, Christoph

dreis2211 commented 1 year ago

Example usage

public static DockerComposeContainer<?> container = new DockerComposeContainer<>(
    new File("../../docker/configs/docker-compose-integration-test.yml")
)
    .withLocalCompose(true)
    .withPull(true)
    .withExposedService("serviceA", 8086, Wait.forHealthcheck())
    .withExposedService("serviceB", 5432, Wait.forHealthcheck())
    .withExposedService("redis", 6379, Wait.forHealthcheck())
    .withExposedService("serviceC", 14268, Wait.forHttp("/").forStatusCode(404))
    .withExposedService("serviceD", 8090, Wait.defaultWaitStrategy().withStartupTimeout(Duration.ofMinutes(3)));

protected void start() {
    container.start();
}