Testcontainers: Synchronizing on Logs After the Service Was Ready

I'm developing some component tests using Testcontainers. I can synchronize the tests on service readiness. However, once the service is ready, I need to prepare the fixture and run the tests until the fixture is fully loaded.

The container under test writes a log that I could use to understand when the fixture is fully loaded. However, I cannot find a method to synchronize on that log.

Does the Testcontainers library offer any utility for this?

2 Answers

If you are using a GenericContainer object type you can simply do something like this:

WaitingConsumer consumer = new WaitingConsumer();
container.followOutput(consumer, STDOUT);
consumer.waitUntil(frame -> 
    frame.getUtf8String().contains("STARTED"), 30, TimeUnit.SECONDS);

Else you can process the entire log with:

container.getLogs();

Hope this was helpful!

2

You can use a log message wait strategy to achieve it.

public GenericContainer containerWithLogWait = new GenericContainer(DockerImageName.parse("redis:5.0.3"))
    .withExposedPorts(6379)
    .waitingFor(
        Wait.forLogMessage(".*Ready to accept connections.*\\n", 3)
    );

You can also create your own strategy by extending it from AbstractWaitStrategy.

Check this page for more details.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest