Skip to content

feat: allow to wait for log line on either stdout or stderr #795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions testcontainers/src/core/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ impl Client {
.into_stderr()
}

pub(crate) fn both_std_logs(&self, id: &str, follow: bool) -> RawLogStream {
self.logs_stream(id, Some(LogSource::BothStd), follow)
.into_both_std()
}

pub(crate) fn logs(&self, id: &str, follow: bool) -> LogStream {
self.logs_stream(id, None, follow)
}
Expand Down Expand Up @@ -266,8 +271,12 @@ impl Client {
) -> LogStream {
let options = LogsOptions {
follow,
stdout: source_filter.map(LogSource::is_stdout).unwrap_or(true),
stderr: source_filter.map(LogSource::is_stderr).unwrap_or(true),
stdout: source_filter
.map(LogSource::includes_stdout)
.unwrap_or(true),
stderr: source_filter
.map(LogSource::includes_stderr)
.unwrap_or(true),
tail: "all".to_owned(),
..Default::default()
};
Expand Down
9 changes: 5 additions & 4 deletions testcontainers/src/core/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ pub enum WaitLogError {
pub enum LogSource {
StdOut,
StdErr,
BothStd,
}

impl LogSource {
pub(super) fn is_stdout(self) -> bool {
matches!(self, Self::StdOut)
pub(super) fn includes_stdout(self) -> bool {
matches!(self, Self::StdOut | Self::BothStd)
}

pub(super) fn is_stderr(self) -> bool {
matches!(self, Self::StdErr)
pub(super) fn includes_stderr(self) -> bool {
matches!(self, Self::StdErr | Self::BothStd)
}
}

Expand Down
12 changes: 11 additions & 1 deletion testcontainers/src/core/logs/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
};

use bytes::Bytes;
use futures::{stream::BoxStream, Stream, StreamExt};
use futures::{stream::BoxStream, Stream, StreamExt, TryStreamExt};
use tokio_stream::wrappers::UnboundedReceiverStream;

use crate::core::logs::LogFrame;
Expand Down Expand Up @@ -51,6 +51,16 @@ impl LogStream {
.boxed()
}

/// Log stream with messages from bith stdout and stderr.
pub(crate) fn into_both_std(self) -> RawLogStream {
self.inner
.map_ok(|frame| match frame {
LogFrame::StdErr(bytes) => bytes,
LogFrame::StdOut(bytes) => bytes,
})
.boxed()
}

/// Splits the log stream into two streams, one for stdout and one for stderr.
pub(crate) async fn split(self) -> (RawLogStream, RawLogStream) {
let (stdout_tx, stdout_rx) = tokio::sync::mpsc::unbounded_channel();
Expand Down
8 changes: 8 additions & 0 deletions testcontainers/src/core/wait/log_strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ impl LogWaitStrategy {
Self::new(LogSource::StdErr, message)
}

/// Create a new [`LogWaitStrategy`] that waits for the given message to appear in either
/// standard output logs or standard error logs.
/// Shortcut for `LogWaitStrategy::new(LogSource::BothStd, message)`.
pub fn stdout_or_stderr(message: impl AsRef<[u8]>) -> Self {
Self::new(LogSource::BothStd, message)
}

/// Create a new `LogWaitStrategy` with the given log source and message.
/// The message is expected to appear in the logs exactly once by default.
pub fn new(source: LogSource, message: impl AsRef<[u8]>) -> Self {
Expand All @@ -56,6 +63,7 @@ impl WaitStrategy for LogWaitStrategy {
let log_stream = match self.source {
LogSource::StdOut => client.stdout_logs(container.id(), true),
LogSource::StdErr => client.stderr_logs(container.id(), true),
LogSource::BothStd => client.both_std_logs(container.id(), true),
};

WaitingStreamWrapper::new(log_stream)
Expand Down
5 changes: 5 additions & 0 deletions testcontainers/src/core/wait/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ impl WaitFor {
Self::log(LogWaitStrategy::new(LogSource::StdErr, message))
}

/// Wait for the message to appear on either container's stdout or stderr.
pub fn message_on_either_std(message: impl AsRef<[u8]>) -> WaitFor {
Self::log(LogWaitStrategy::new(LogSource::BothStd, message))
}

/// Wait for the message to appear on the container's stdout.
pub fn log(log_strategy: LogWaitStrategy) -> WaitFor {
WaitFor::Log(log_strategy)
Expand Down
Loading