Skip to content

Commit d5c45b9

Browse files
committed
test start server modified
1 parent 5394abb commit d5c45b9

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

tests/shared/start_local_http_server.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import time
23
from collections.abc import Callable
34
from multiprocessing import get_context
45

@@ -39,24 +40,38 @@ def start(this: 'LocalHttpServer'):
3940
async def start_local_http_server(attach_handlers: AttachHandlers) -> tuple[Client, StopServer]:
4041
local_http_server = LocalHttpServer(attach_handlers)
4142

42-
async def ping_app():
43-
session = aiohttp.ClientSession(
44-
timeout=aiohttp.ClientTimeout(total=5.0) # Increase timeout to 5 seconds
45-
)
46-
47-
async with session:
48-
await session.get(
49-
f'http://localhost:{local_http_server.port}/__python_test__/api/v1/ping',
50-
timeout=aiohttp.ClientTimeout(total=5.0) # Explicit timeout here too
51-
)
52-
53-
5443
multiprocessing = get_context('fork')
5544
server = multiprocessing.Process(target=LocalHttpServer.start, args=(local_http_server, ))
5645
server.start()
5746

58-
# Increase number of retries for server to become available
59-
await ping_app()
47+
# Warte auf den Server, bis er bereit ist
48+
async def ping_app():
49+
retry_count = 0
50+
max_retries = 5
51+
retry_delay = 0.5
52+
53+
async with aiohttp.ClientSession() as session:
54+
while retry_count < max_retries:
55+
try:
56+
await session.get(
57+
f"http://localhost:{local_http_server.port}/__python_test__/api/v1/ping",
58+
timeout=2
59+
)
60+
# Wenn die Anfrage erfolgreich ist, brechen wir die Schleife ab
61+
return True
62+
except (aiohttp.ClientError, asyncio.TimeoutError):
63+
retry_count += 1
64+
await asyncio.sleep(retry_delay)
65+
66+
# Wenn alle Versuche fehlschlagen, geben wir False zurück
67+
return False
68+
69+
# Versuche, den Server zu erreichen
70+
server_ready = await ping_app()
71+
if not server_ready:
72+
server.terminate()
73+
server.join()
74+
raise RuntimeError(f"Failed to start HTTP server on port {local_http_server.port}")
6075

6176
def stop_server():
6277
server.terminate()

0 commit comments

Comments
 (0)