0

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?

riccardo.cardin
  • 7,971
  • 5
  • 57
  • 106

2 Answers2

2

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!

Scafer
  • 36
  • 1
  • Thanks for the response. I realized that my question was not focused. I'm sorry. The real problem is that I need to wait for the log `STARTED`, for example, to appear _n_ times in the log. – riccardo.cardin Sep 08 '21 at 10:11
  • I don't know if there's a function that does it out of the box. Maybe you should create a method that consumes the log output and verify that specific condition. – Scafer Sep 08 '21 at 13:19
0

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.

rfel
  • 11
  • 3