|
1 | 1 | import asyncio |
| 2 | +import time |
2 | 3 | from collections.abc import Callable |
3 | 4 | from multiprocessing import get_context |
4 | 5 |
|
@@ -39,24 +40,38 @@ def start(this: 'LocalHttpServer'): |
39 | 40 | async def start_local_http_server(attach_handlers: AttachHandlers) -> tuple[Client, StopServer]: |
40 | 41 | local_http_server = LocalHttpServer(attach_handlers) |
41 | 42 |
|
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 | | - |
54 | 43 | multiprocessing = get_context('fork') |
55 | 44 | server = multiprocessing.Process(target=LocalHttpServer.start, args=(local_http_server, )) |
56 | 45 | server.start() |
57 | 46 |
|
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}") |
60 | 75 |
|
61 | 76 | def stop_server(): |
62 | 77 | server.terminate() |
|
0 commit comments