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...
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!
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.