Skip to content

Commit be4d09e

Browse files
fix: Allow passing in a custom wait strategy string in MySQL, Cassandra, Kafka and Trino (#953)
Hey, Just a small change in adding the ability to pass in a custom wait strategy message for MySQL. The current implementation had '.*: ready for connections.*: ready for connections.*' as a default regex with no option to change it. This PR moves it into a parameter. No new functionality has been added so I presume the tests can stay the same. If you find this useful, I can also go over the other implementations and make a similar change wherever this is a problem.
1 parent d7953b8 commit be4d09e

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

modules/cassandra/testcontainers/cassandra/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@ class CassandraContainer(DockerContainer):
3939
CQL_PORT = 9042
4040
DEFAULT_LOCAL_DATACENTER = "datacenter1"
4141

42-
def __init__(self, image: str = "cassandra:latest", **kwargs) -> None:
42+
def __init__(
43+
self, image: str = "cassandra:latest", wait_strategy_check_string: str = "Startup complete", **kwargs
44+
) -> None:
4345
super().__init__(image=image, **kwargs)
4446
self.with_exposed_ports(self.CQL_PORT)
4547
self.with_env("JVM_OPTS", "-Dcassandra.skip_wait_for_gossip_to_settle=0 -Dcassandra.initial_token=0")
4648
self.with_env("HEAP_NEWSIZE", "128M")
4749
self.with_env("MAX_HEAP_SIZE", "1024M")
4850
self.with_env("CASSANDRA_ENDPOINT_SNITCH", "GossipingPropertyFileSnitch")
4951
self.with_env("CASSANDRA_DC", self.DEFAULT_LOCAL_DATACENTER)
50-
self.waiting_for(LogMessageWaitStrategy("Startup complete"))
52+
self.waiting_for(LogMessageWaitStrategy(wait_strategy_check_string))
5153

5254
def get_contact_points(self) -> list[tuple[str, int]]:
5355
return [(self.get_container_host_ip(), int(self.get_exposed_port(self.CQL_PORT)))]

modules/kafka/testcontainers/kafka/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,18 @@ class KafkaContainer(DockerContainer):
5555
TC_START_SCRIPT = "/tc-start.sh"
5656
MIN_KRAFT_TAG = "7.0.0"
5757

58-
def __init__(self, image: str = "confluentinc/cp-kafka:7.6.0", port: int = 9093, **kwargs) -> None:
58+
def __init__(
59+
self,
60+
image: str = "confluentinc/cp-kafka:7.6.0",
61+
port: int = 9093,
62+
wait_strategy_check_string: str = r".*\[KafkaServer id=\d+\] started.*",
63+
**kwargs,
64+
) -> None:
5965
raise_for_deprecated_parameter(kwargs, "port_to_expose", "port")
6066
super().__init__(image, **kwargs)
6167
self.port = port
6268
self.kraft_enabled = False
63-
self.wait_for: re.Pattern[str] = re.compile(r".*\[KafkaServer id=\d+\] started.*")
69+
self.wait_for: re.Pattern[str] = re.compile(wait_strategy_check_string)
6470
self.boot_command = ""
6571
self.cluster_id = "MkU3OEVBNTcwNTJENDM2Qk"
6672
self.listeners = f"PLAINTEXT://0.0.0.0:{self.port},BROKER://0.0.0.0:9092"

modules/mysql/testcontainers/mysql/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def __init__(
7171
dbname: Optional[str] = None,
7272
port: int = 3306,
7373
seed: Optional[str] = None,
74+
wait_strategy_check_string: str = r".*: ready for connections.*: ready for connections.*",
7475
**kwargs,
7576
) -> None:
7677
if dialect is not None and dialect.startswith("mysql+"):
@@ -96,6 +97,7 @@ def __init__(
9697
if self.username == "root":
9798
self.root_password = self.password
9899
self.seed = seed
100+
self.wait_strategy_check_string = wait_strategy_check_string
99101

100102
def _configure(self) -> None:
101103
self.with_env("MYSQL_ROOT_PASSWORD", self.root_password)
@@ -107,7 +109,7 @@ def _configure(self) -> None:
107109

108110
def _connect(self) -> None:
109111
wait_strategy = LogMessageWaitStrategy(
110-
re.compile(r".*: ready for connections.*: ready for connections.*", flags=re.DOTALL | re.MULTILINE),
112+
re.compile(self.wait_strategy_check_string, flags=re.DOTALL | re.MULTILINE),
111113
)
112114
wait_strategy.wait_until_ready(self)
113115

modules/trino/testcontainers/trino/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ def __init__(
2424
user: str = "test",
2525
port: int = 8080,
2626
container_start_timeout: int = 30,
27+
wait_strategy_check_string: str = ".*======== SERVER STARTED ========.*",
2728
**kwargs,
2829
):
2930
super().__init__(image=image, **kwargs)
3031
self.user = user
3132
self.port = port
3233
self.with_exposed_ports(self.port)
3334
self.waiting_for(
34-
LogMessageWaitStrategy(re.compile(".*======== SERVER STARTED ========.*", re.MULTILINE))
35+
LogMessageWaitStrategy(re.compile(wait_strategy_check_string, re.MULTILINE))
3536
.with_poll_interval(c.sleep_time)
3637
.with_startup_timeout(container_start_timeout)
3738
)

0 commit comments

Comments
 (0)