From 271ca9a0fef2e5f2b216457bfee44318e93990bf Mon Sep 17 00:00:00 2001 From: Marc Schmitzer Date: Thu, 24 Oct 2024 10:41:49 +0200 Subject: [PATCH] fix: update wait_for_logs to not throw on 'created', and an optimization (#719) Attempt to fix another part of #715 , plus an optimization. Based on #716 by @alexanderankin . --------- Co-authored-by: David Ankin --- core/testcontainers/core/waiting_utils.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/core/testcontainers/core/waiting_utils.py b/core/testcontainers/core/waiting_utils.py index 2bd7806b..8966e5f9 100644 --- a/core/testcontainers/core/waiting_utils.py +++ b/core/testcontainers/core/waiting_utils.py @@ -77,6 +77,9 @@ def wait_for(condition: Callable[..., bool]) -> bool: return condition() +_NOT_EXITED_STATUSES = {"running", "created"} + + def wait_for_logs( container: "DockerContainer", predicate: Union[Callable, str], @@ -103,11 +106,13 @@ def wait_for_logs( """ if isinstance(predicate, str): predicate = re.compile(predicate, re.MULTILINE).search + wrapped = container.get_wrapped_container() start = time.time() while True: duration = time.time() - start - stdout = container.get_logs()[0].decode() - stderr = container.get_logs()[1].decode() + stdout, stderr = container.get_logs() + stdout = stdout.decode() + stderr = stderr.decode() predicate_result = ( predicate(stdout) or predicate(stderr) if predicate_streams_and is False @@ -118,6 +123,8 @@ def wait_for_logs( return duration if duration > timeout: raise TimeoutError(f"Container did not emit logs satisfying predicate in {timeout:.3f} " "seconds") - if raise_on_exit and container.get_wrapped_container().status != "running": - raise RuntimeError("Container exited before emitting logs satisfying predicate") + if raise_on_exit: + wrapped.reload() + if wrapped.status not in _NOT_EXITED_STATUSES: + raise RuntimeError("Container exited before emitting logs satisfying predicate") time.sleep(interval)