diff --git a/testcontainers/core/waiting_utils.py b/testcontainers/core/waiting_utils.py index af667c30d..1bb48ea3c 100644 --- a/testcontainers/core/waiting_utils.py +++ b/testcontainers/core/waiting_utils.py @@ -12,7 +12,8 @@ # under the License. -from time import sleep +import re +import time import blindspin import crayons @@ -40,7 +41,7 @@ def wrapper(wrapped, instance, args, kwargs): try: return wrapped(*args, **kwargs) except Exception as e: - sleep(config.SLEEP_TIME) + time.sleep(config.SLEEP_TIME) exception = e raise TimeoutException( """Wait time exceeded {0} sec. @@ -55,3 +56,37 @@ def wrapper(wrapped, instance, args, kwargs): @wait_container_is_ready() def wait_for(condition): return condition() + + +def wait_for_logs(container, predicate, timeout=None, interval=1): + """ + Wait for the container to emit logs satisfying the predicate. + + Parameters + ---------- + container : DockerContainer + Container whose logs to wait for. + predicate : callable or str + Predicate that should be satisfied by the logs. If a string, the it is used as the pattern + for a multiline regular expression search. + timeout : float or None + Number of seconds to wait for the predicate to be satisfied. Defaults to wait indefinitely. + interval : float + Interval at which to poll the logs. + + Returns + ------- + duration : float + Number of seconds until the predicate was satisfied. + """ + if isinstance(predicate, str): + predicate = re.compile(predicate, re.MULTILINE).search + start = time.time() + while True: + duration = time.time() - start + if predicate(container._container.logs().decode()): + return duration + if timeout and duration > timeout: + raise TimeoutError("container did not emit logs satisfying predicate in %.3f seconds" + % timeout) + time.sleep(interval) diff --git a/tests/test_core.py b/tests/test_core.py new file mode 100644 index 000000000..0b5aced4b --- /dev/null +++ b/tests/test_core.py @@ -0,0 +1,15 @@ +import pytest + +from testcontainers.core.container import DockerContainer +from testcontainers.core.waiting_utils import wait_for_logs + + +def test_raise_timeout(): + with pytest.raises(TimeoutError): + with DockerContainer("alpine").with_command("sleep 2") as container: + wait_for_logs(container, "Hello from Docker!", timeout=1e-3) + + +def test_wait_for_hello(): + with DockerContainer("hello-world") as container: + wait_for_logs(container, "Hello from Docker!") diff --git a/tests/test_google.py b/tests/test_google.py index eeff8b550..48c920181 100644 --- a/tests/test_google.py +++ b/tests/test_google.py @@ -1,10 +1,11 @@ from testcontainers.google import PubSubContainer +from testcontainers.core.waiting_utils import wait_for_logs from queue import Queue def test_pubsub_container(): with PubSubContainer() as pubsub: - import time; time.sleep(5) + wait_for_logs(pubsub, r"Server started, listening on \d+", timeout=10) # Create a new topic publisher = pubsub.get_publisher_client() topic_path = publisher.topic_path(pubsub.project, "my-topic")