Skip to content

Commit

Permalink
fix: wait_for_logs can now fail early when the container stops (#682)
Browse files Browse the repository at this point in the history
Addresses my suggestion made in [issue
681](#681).

This PR adds a flag that checks is the status is not `running` and
raises a `RuntimeError` to avoid waiting for logs after the container
already has exited. The idea is to save wait time when there is a long
startup time in case the container fails early.


```python
from testcontainers.core import container, waiting_utils

if __name__ == "__main__":
    waiting_utils.wait_for_logs(
        container.DockerContainer("flyway/flyway").start(),
        r"Successfully applied \d+ migrations to schema",
        timeout=10,
        raise_on_exit=True,
    )

# > RuntimeError(f"Container exited before emitting logs satisfying predicate")
# ( Raised almost immediately ) 
```
  • Loading branch information
ArthurBook authored Aug 14, 2024
1 parent c7d9b81 commit 925329d
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/testcontainers/core/waiting_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def wait_for_logs(
timeout: float = config.timeout,
interval: float = 1,
predicate_streams_and: bool = False,
raise_on_exit: bool = False,
#
) -> float:
"""
Expand Down Expand Up @@ -117,4 +118,6 @@ 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")
time.sleep(interval)

0 comments on commit 925329d

Please sign in to comment.