From 5b1ac8c1abad9f866d3c6b839af3522bc1217580 Mon Sep 17 00:00:00 2001 From: Grieve Date: Thu, 14 Nov 2024 16:36:22 +0800 Subject: [PATCH 1/2] fix(trino): add delay time to avoid Trino issue --- modules/trino/testcontainers/trino/__init__.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modules/trino/testcontainers/trino/__init__.py b/modules/trino/testcontainers/trino/__init__.py index 97e3f9de..c1b6d998 100644 --- a/modules/trino/testcontainers/trino/__init__.py +++ b/modules/trino/testcontainers/trino/__init__.py @@ -11,6 +11,7 @@ # License for the specific language governing permissions and limitations # under the License. import re +import time from testcontainers.core.config import testcontainers_config as c from testcontainers.core.generic import DbContainer @@ -24,12 +25,14 @@ def __init__( image="trinodb/trino:latest", user: str = "test", port: int = 8080, + delay: int = 5, **kwargs, ): super().__init__(image=image, **kwargs) self.user = user self.port = port self.with_exposed_ports(self.port) + self.delay = delay @wait_container_is_ready() def _connect(self) -> None: @@ -39,6 +42,8 @@ def _connect(self) -> None: c.max_tries, c.sleep_time, ) + # To avoid `TrinoQueryError(type=INTERNAL_ERROR, name=GENERIC_INTERNAL_ERROR, message="nodes is empty")` + time.sleep(self.delay) conn = connect( host=self.get_container_host_ip(), port=self.get_exposed_port(self.port), From 622d00a5d7138dda0e14bc38db57e8dc99ee433c Mon Sep 17 00:00:00 2001 From: Grieve Date: Tue, 19 Nov 2024 14:51:34 +0800 Subject: [PATCH 2/2] feat(trino): use waiting tpch instead of sleep delay time --- modules/trino/testcontainers/trino/__init__.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/modules/trino/testcontainers/trino/__init__.py b/modules/trino/testcontainers/trino/__init__.py index c1b6d998..b9197ba2 100644 --- a/modules/trino/testcontainers/trino/__init__.py +++ b/modules/trino/testcontainers/trino/__init__.py @@ -25,14 +25,12 @@ def __init__( image="trinodb/trino:latest", user: str = "test", port: int = 8080, - delay: int = 5, **kwargs, ): super().__init__(image=image, **kwargs) self.user = user self.port = port self.with_exposed_ports(self.port) - self.delay = delay @wait_container_is_ready() def _connect(self) -> None: @@ -42,17 +40,21 @@ def _connect(self) -> None: c.max_tries, c.sleep_time, ) - # To avoid `TrinoQueryError(type=INTERNAL_ERROR, name=GENERIC_INTERNAL_ERROR, message="nodes is empty")` - time.sleep(self.delay) conn = connect( host=self.get_container_host_ip(), port=self.get_exposed_port(self.port), user=self.user, ) - cur = conn.cursor() - cur.execute("SELECT 1") - cur.fetchall() - conn.close() + deadline = time.time() + c.max_tries + while time.time() < deadline: + try: + cur = conn.cursor() + cur.execute("SELECT * FROM tpch.tiny.nation LIMIT 1") + cur.fetchall() + return + except Exception: + time.sleep(c.sleep_time) + raise TimeoutError(f"Trino did not start within {c.max_tries:.3f} seconds") def get_connection_url(self): return f"trino://{self.user}@{self.get_container_host_ip()}:{self.port}"