Skip to content

Commit

Permalink
feat(core): add ability to do OR & AND for waitforlogs (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderankin authored Aug 3, 2024
1 parent e02c1b3 commit b1453e8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
16 changes: 14 additions & 2 deletions core/testcontainers/core/waiting_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ def wait_for(condition: Callable[..., bool]) -> bool:


def wait_for_logs(
container: "DockerContainer", predicate: Union[Callable, str], timeout: float = config.timeout, interval: float = 1
container: "DockerContainer",
predicate: Union[Callable, str],
timeout: float = config.timeout,
interval: float = 1,
predicate_streams_and: bool = False,
#
) -> float:
"""
Wait for the container to emit logs satisfying the predicate.
Expand All @@ -90,6 +95,7 @@ def wait_for_logs(
timeout: Number of seconds to wait for the predicate to be satisfied. Defaults to wait
indefinitely.
interval: Interval at which to poll the logs.
predicate_streams_and: should the predicate be applied to both
Returns:
duration: Number of seconds until the predicate was satisfied.
Expand All @@ -101,7 +107,13 @@ def wait_for_logs(
duration = time.time() - start
stdout = container.get_logs()[0].decode()
stderr = container.get_logs()[1].decode()
if predicate(stdout) or predicate(stderr):
predicate_result = (
predicate(stdout) or predicate(stderr)
if predicate_streams_and is False
else predicate(stdout) and predicate(stderr)
#
)
if predicate_result:
return duration
if duration > timeout:
raise TimeoutError(f"Container did not emit logs satisfying predicate in {timeout:.3f} " "seconds")
Expand Down
32 changes: 31 additions & 1 deletion modules/postgres/testcontainers/postgres/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,37 @@ def get_connection_url(self, host: Optional[str] = None, driver: Optional[str] =

@wait_container_is_ready()
def _connect(self) -> None:
wait_for_logs(self, ".*database system is ready to accept connections.*", c.max_tries, c.sleep_time)
# postgres itself logs these messages to the standard error stream:
#
# $ /opt/homebrew/opt/postgresql@14/bin/postgres -D /opt/homebrew/var/postgresql@14 \
# > | grep -o -a -m 1 -h 'database system is ready to accept connections'
# 2024-08-03 00:13:02.799 EDT [70226] LOG: starting PostgreSQL 14.11 (Homebrew) ....
# 2024-08-03 00:13:02.804 EDT [70226] LOG: listening on IPv4 address "127.0.0.1", port 5432
# ...
# ^C2024-08-03 00:13:04.226 EDT [70226] LOG: received fast shutdown request
# ...
#
# $ /opt/homebrew/opt/postgresql@14/bin/postgres -D /opt/homebrew/var/postgresql@14 2>&1 \
# > | grep -o -a -m 1 -h 'database system is ready to accept connections'
# database system is ready to accept connections
#
# and the setup script inside docker library postgres
# uses pg_ctl:
# https://github.com/docker-library/postgres/blob/66da3846b40396249936938ee17e9684e6968a57/16/alpine3.20/docker-entrypoint.sh#L261-L282
# which prints logs to stdout:
# https://www.postgresql.org/docs/current/app-pg-ctl.html#:~:text=the%20server%27s%20standard%20output%20and%20standard%20error%20are%20sent%20to%20pg_ctl%27s%20standard%20output
#
# so we must wait for both the setup and real startup:
predicate_streams_and = True

wait_for_logs(
self,
".*database system is ready to accept connections.*",
c.max_tries,
c.sleep_time,
predicate_streams_and=predicate_streams_and,
#
)

count = 0
while count < c.max_tries:
Expand Down

0 comments on commit b1453e8

Please sign in to comment.