@@ -75,6 +75,7 @@ class WaitStrategy(ABC):
7575 def __init__ (self ) -> None :
7676 self ._startup_timeout : float = testcontainers_config .timeout
7777 self ._poll_interval : float = testcontainers_config .sleep_time
78+ self ._transient_exceptions : list [type [Exception ]] = [* TRANSIENT_EXCEPTIONS ]
7879
7980 def with_startup_timeout (self , timeout : Union [int , timedelta ]) -> "WaitStrategy" :
8081 """Set the maximum time to wait for the container to be ready."""
@@ -92,12 +93,21 @@ def with_poll_interval(self, interval: Union[float, timedelta]) -> "WaitStrategy
9293 self ._poll_interval = interval
9394 return self
9495
96+ def with_transient_exceptions (self , * transient_exceptions : type [Exception ]) -> "WaitStrategy" :
97+ self ._transient_exceptions .extend (transient_exceptions )
98+ return self
99+
95100 @abstractmethod
96101 def wait_until_ready (self , container : WaitStrategyTarget ) -> None :
97102 """Wait until the container is ready."""
98103 pass
99104
100- def _poll (self , check : Callable [[], bool ]) -> bool :
105+ def _poll (self , check : Callable [[], bool ], transient_exceptions : list [type [Exception ]] = None ) -> bool :
106+ if not transient_exceptions :
107+ all_te_types = self ._transient_exceptions
108+ else :
109+ all_te_types = [* self ._transient_exceptions , * (transient_exceptions or [])]
110+
101111 start = time .time ()
102112 while True :
103113 start_attempt = time .time ()
@@ -112,8 +122,13 @@ def _poll(self, check: Callable[[], bool]) -> bool:
112122 return result
113123 except StopIteration :
114124 return False
115- except : # noqa: E722, RUF100
116- pass
125+ except Exception as e : # noqa: E722, RUF100
126+ is_transient = False
127+ for et in all_te_types :
128+ if isinstance (e , et ):
129+ is_transient = True
130+ if not is_transient :
131+ raise RuntimeError (f"exception while checking for strategy { self } " ) from e
117132
118133 seconds_left_until_next = self ._poll_interval - (time .time () - start_attempt )
119134 time .sleep (max (0.0 , seconds_left_until_next ))
0 commit comments