1717from redis .asyncio import Redis as asyncRedis
1818from testcontainers .core .container import DockerContainer
1919from 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
2323class 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
7574class AsyncRedisContainer (RedisContainer ):
0 commit comments