waitForLogs function
Future<Duration>
waitForLogs(
- WaitStrategyTarget container,
- Pattern predicate, {
- Duration? timeout,
- Duration interval = const Duration(seconds: 1),
- bool predicateStreamsAnd = false,
Convenience function that waits for container log output to match a pattern.
Creates a LogMessageWaitStrategy internally and invokes it immediately. Returns the elapsed wait time as a Duration.
Parameters:
container— the container to watch.predicate— a String (compiled toRegExp), RegExp, or any Pattern to search for.timeout— optional startup-timeout override. Defaults to TestcontainersConfiguration.timeout (120 seconds).interval— poll interval. Default: 1 second.predicateStreamsAnd— whentrue,predicatemust match both stdout and stderr. Whenfalse(default) matching either stream is sufficient.
Example:
await waitForLogs(container, 'Server started on port');
Implementation
Future<Duration> waitForLogs(
WaitStrategyTarget container,
Pattern predicate, {
Duration? timeout,
Duration interval = const Duration(seconds: 1),
bool predicateStreamsAnd = false,
}) async {
final effectiveTimeout = timeout ??
Duration(milliseconds: (testcontainersConfig.timeout * 1000).toInt());
final strategy = LogMessageWaitStrategy(
predicate,
predicateStreamsAnd: predicateStreamsAnd,
)
..withStartupTimeout(effectiveTimeout)
..withPollInterval(interval);
final start = DateTime.now();
await strategy.waitUntilReady(container);
return DateTime.now().difference(start);
}