Skip to content

Commit 129bfc6

Browse files
author
Jon Miller
committed
Migrate ServerContainer from deprecated decorator to HttpWaitStrategy
1 parent 44dd40b commit 129bfc6

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
# This comment can be removed (Used for testing)
1311

@@ -42,19 +40,9 @@ def __init__(self, port: int, image: Union[str, DockerImage]) -> None:
4240
self.internal_port = port
4341
self.with_exposed_ports(self.internal_port)
4442

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

5947
def get_api_url(self) -> str:
6048
raise NotImplementedError

0 commit comments

Comments
 (0)