Skip to content

Commit 460b0d8

Browse files
jmillxyzJon MillerTranquility2
authored
fix(generic): Migrate ServerContainer from deprecated decorator to HttpWaitStrategy (#971)
Hi there, I've seen this deprecation warning in a generic container. This is my first PR in this project; I'm open to any feedback you may have! Related: #874 Also updates the docs, highlighted in [this comment](#874 (comment)) Co-authored-by: Jon Miller <jonmiller@netflix.com> Co-authored-by: Roy Moore <roy@moore.co.il>
1 parent 0e0bb24 commit 460b0d8

File tree

2 files changed

+7
-21
lines changed

2 files changed

+7
-21
lines changed

docs/features/wait_strategies.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@ Testcontainers-Python provides several strategies to wait for containers to be r
44

55
## Basic Wait Strategy
66

7-
The simplest way to wait for a container is using the `wait_container_is_ready` decorator:
7+
The simplest way to wait for a container is using a structured wait strategy:
88

99
```python
10-
from testcontainers.core.waiting_utils import wait_container_is_ready
10+
from testcontainers.core.wait_strategies import HttpWaitStrategy
1111

1212
class MyContainer(DockerContainer):
13-
@wait_container_is_ready()
1413
def _connect(self):
15-
# Your connection logic here
16-
pass
14+
HttpWaitStrategy(8080).wait_until_ready(self)
1715
```
1816

19-
This decorator will retry the method until it succeeds or times out. By default, it will retry for 120 seconds with a 1-second interval between attempts.
17+
The strategy will retry until it succeeds or times out. By default, it will retry for 120 seconds with a 1-second interval between attempts.
2018

2119
## Log-based Waiting
2220

modules/generic/testcontainers/generic/server.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
from typing import Union
2-
from urllib.error import HTTPError, URLError
3-
from urllib.request import urlopen
42

53
import httpx
64

75
from testcontainers.core.container import DockerContainer
86
from testcontainers.core.exceptions import ContainerStartException
97
from testcontainers.core.image import DockerImage
10-
from testcontainers.core.waiting_utils import wait_container_is_ready
8+
from testcontainers.core.wait_strategies import HttpWaitStrategy
119

1210

1311
class ServerContainer(DockerContainer):
@@ -40,19 +38,9 @@ def __init__(self, port: int, image: Union[str, DockerImage]) -> None:
4038
self.internal_port = port
4139
self.with_exposed_ports(self.internal_port)
4240

43-
@wait_container_is_ready(HTTPError, URLError)
4441
def _connect(self) -> None:
45-
# noinspection HttpUrlsUsage
46-
url = self._create_connection_url()
47-
try:
48-
with urlopen(url) as r:
49-
assert b"" in r.read()
50-
except HTTPError as e:
51-
# 404 is expected, as the server may not have the specific endpoint we are looking for
52-
if e.code == 404:
53-
pass
54-
else:
55-
raise
42+
strategy = HttpWaitStrategy(self.internal_port).for_status_code(404)
43+
strategy.wait_until_ready(self)
5644

5745
def get_api_url(self) -> str:
5846
raise NotImplementedError

0 commit comments

Comments
 (0)