Skip to content

Commit e25713a

Browse files
fix(redis): Use wait strategy instead of deprecated decorator (#914)
Another part of fixing #874 (cf. #899). --------- Co-authored-by: Dave Ankin <alexanderankin@gmail.com>
1 parent c8a5bbd commit e25713a

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

modules/mqtt/testcontainers/mqtt/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def start(self, configfile: Optional[str] = None) -> Self:
121121
# default config file
122122
configfile = Path(__file__).parent / MosquittoContainer.CONFIG_FILE
123123
self.with_volume_mapping(configfile, "/mosquitto/config/mosquitto.conf")
124-
# since version 2.1.1 - 2026-02-04, which fixed a PUID/PGID issue, the container needs to write to the data directory,
124+
# since version 2.1.1 - 2026-02-04, which fixed a PUID/PGID issue, the container needs to write to the data directory,
125125
# so we mount it as tmpfs for better performance in tests
126126
self.with_tmpfs_mount("/data")
127127

modules/redis/testcontainers/redis/__init__.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from redis.asyncio import Redis as asyncRedis
1818
from testcontainers.core.container import DockerContainer
1919
from testcontainers.core.utils import raise_for_deprecated_parameter
20-
from testcontainers.core.waiting_utils import wait_container_is_ready
20+
from testcontainers.core.waiting_utils import WaitStrategy, WaitStrategyTarget
2121

2222

2323
class RedisContainer(DockerContainer):
@@ -36,19 +36,13 @@ class RedisContainer(DockerContainer):
3636

3737
def __init__(self, image: str = "redis:latest", port: int = 6379, password: Optional[str] = None, **kwargs) -> None:
3838
raise_for_deprecated_parameter(kwargs, "port_to_expose", "port")
39-
super().__init__(image, **kwargs)
39+
super().__init__(image, _wait_strategy=PingWaitStrategy(), **kwargs)
4040
self.port = port
4141
self.password = password
4242
self.with_exposed_ports(self.port)
4343
if self.password:
4444
self.with_command(f"redis-server --requirepass {self.password}")
4545

46-
@wait_container_is_ready(redis.exceptions.ConnectionError)
47-
def _connect(self) -> None:
48-
client = self.get_client()
49-
if not client.ping():
50-
raise redis.exceptions.ConnectionError("Could not connect to Redis")
51-
5246
def get_client(self, **kwargs) -> redis.Redis:
5347
"""
5448
Get a redis client.
@@ -66,10 +60,15 @@ def get_client(self, **kwargs) -> redis.Redis:
6660
**kwargs,
6761
)
6862

69-
def start(self) -> "RedisContainer":
70-
super().start()
71-
self._connect()
72-
return self
63+
64+
class PingWaitStrategy(WaitStrategy):
65+
def __init__(self) -> None:
66+
super().__init__()
67+
self.with_transient_exceptions(redis.exceptions.ConnectionError)
68+
69+
def wait_until_ready(self, container: WaitStrategyTarget) -> None:
70+
if not self._poll(lambda: container.get_client().ping()):
71+
raise redis.exceptions.ConnectionError("Could not connect to Redis")
7372

7473

7574
class AsyncRedisContainer(RedisContainer):

modules/redis/tests/test_redis.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from testcontainers.redis import RedisContainer, AsyncRedisContainer
44
import pytest
5+
import redis
56

67

78
def test_docker_run_redis():
@@ -24,6 +25,16 @@ def test_docker_run_redis_with_password():
2425
assert client.get("hello") == "world"
2526

2627

28+
def test_docker_run_start_fails(monkeypatch: pytest.MonkeyPatch):
29+
# Patch config to speed up the test.
30+
monkeypatch.setattr("testcontainers.core.config.testcontainers_config.max_tries", 0.3)
31+
monkeypatch.setattr("testcontainers.core.config.testcontainers_config.sleep_time", 0.02)
32+
# Use a bogus image to make the startup check fail.
33+
config = RedisContainer(image="hello-world")
34+
with pytest.raises(redis.exceptions.ConnectionError, match="Could not connect"):
35+
config.start()
36+
37+
2738
pytest.mark.usefixtures("anyio_backend")
2839

2940

0 commit comments

Comments
 (0)