testcontainers-rs icon indicating copy to clipboard operation
testcontainers-rs copied to clipboard

`.wait_for()` log after container started

Open amab8901 opened this issue 5 months ago • 1 comments

Use case:

I disconnect container A (after both containers A and B were started) to see whether container B responds with expected error log, and I make assertion for it.

Current workaround

I use sleep to wait a fixed amount of time after which I expect the error log to be printed, and then the assertion gets run

Flaws with the workaround

  • If I run more integration tests simultaneously or run the integration tests on slower platform (lower-spec computer, CI), the amount of time sleeped might be insufficient for the error log to appear, which would fail the assertion. I'd have to revisit old integration tests and increase the sleep time
  • if I run the integration test on faster platform (high-end computer), or run it in isolation (instead of all integration tests simultaneously) the integration tests will take longer time than necessary to complete
  • Even if I can eventually find the right amount of time to sleep, it still takes a lot of tinkering and many runs to figure out what's the right amount of time to sleep.

Solution

Would be nice to have a method like .with_wait_for() but after the container has started. Implemented on ContainerAsync<I: Image>.

amab8901 avatar Sep 16 '25 07:09 amab8901

You can generally achieve this with stdout or stderr methods which returns an async reader of logs which can be wrapped into stream (e.g with ReaderStream), and simply use custom filtering e.g with filter

However, we can improve the user-experience here as we already have LogWaitStrategy and underlying WaitingStreamWrapper (called from WaitStrategy::wait_until_ready) - so we can expose something for sure, as it's not coupled with lifecycle of container

DDtKey avatar Oct 28 '25 03:10 DDtKey