Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions testcontainers/core/waiting_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
# under the License.


from time import sleep
import re
import time

import blindspin
import crayons
Expand Down Expand Up @@ -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.
Expand All @@ -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)
15 changes: 15 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -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!")
3 changes: 2 additions & 1 deletion tests/test_google.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down