Skip to content

Commit

Permalink
fix: update wait_for_logs to not throw on 'created', and an optimizat…
Browse files Browse the repository at this point in the history
…ion (#719)

Attempt to fix another part of #715 , plus an optimization.

Based on #716 by @alexanderankin .

---------

Co-authored-by: David Ankin <daveankin@gmail.com>
  • Loading branch information
mschmitzer and alexanderankin authored Oct 24, 2024
1 parent fe489d7 commit 271ca9a
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions core/testcontainers/core/waiting_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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
Expand All @@ -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)

0 comments on commit 271ca9a

Please sign in to comment.