Skip to content

Commit f19c466

Browse files
committed
Always consume logs
Signed-off-by: Anuraag Agrawal <[email protected]>
1 parent 736582e commit f19c466

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

conformance/test/server.py

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
UnaryResponseDefinition,
4141
)
4242
from google.protobuf.any_pb2 import Any
43-
from hypercorn.config import Config as HypercornConfig
44-
from hypercorn.logging import Logger
4543

4644
from connectrpc.code import Code
4745
from connectrpc.errors import ConnectError
@@ -389,22 +387,6 @@ def bidi_stream(
389387
)
390388

391389

392-
class PortCapturingLogger(Logger):
393-
"""In-memory logger for Hypercorn, useful for testing."""
394-
395-
port = -1
396-
397-
def __init__(self, conf: HypercornConfig) -> None:
398-
super().__init__(conf)
399-
400-
async def info(self, message: str, *args: Any, **kwargs: Any) -> None:
401-
if "Running on" in message:
402-
_, _, rest = message.partition("//127.0.0.1:")
403-
port, _, _ = rest.partition(" ")
404-
self.port = int(port)
405-
await super().info(message, *args, **kwargs)
406-
407-
408390
read_max_bytes = os.getenv("READ_MAX_BYTES")
409391
if read_max_bytes is not None:
410392
read_max_bytes = int(read_max_bytes)
@@ -432,6 +414,23 @@ def _server_env(request: ServerCompatRequest) -> dict[str, str]:
432414
_port_regex = re.compile(r".*://[^:]+:(\d+).*")
433415

434416

417+
async def _tee_to_stderr(stream: asyncio.StreamReader) -> AsyncIterator[bytes]:
418+
try:
419+
while True:
420+
line = await stream.readline()
421+
if not line:
422+
break
423+
print(line.decode("utf-8"), end="", file=sys.stderr) # noqa: T201
424+
yield line
425+
except asyncio.CancelledError:
426+
pass
427+
428+
429+
async def _consume_log(stream: AsyncIterator[bytes]) -> None:
430+
async for _ in stream:
431+
pass
432+
433+
435434
async def serve_granian(
436435
request: ServerCompatRequest,
437436
mode: Literal["sync", "async"],
@@ -470,12 +469,13 @@ async def serve_granian(
470469
)
471470
stdout = proc.stdout
472471
assert stdout is not None # noqa: S101
472+
stdout = _tee_to_stderr(stdout)
473473
try:
474-
for _ in range(100):
475-
line = await stdout.readline()
474+
async for line in stdout:
476475
if b"Started worker-1 runtime-1" in line:
477476
break
478477
port_future.set_result(port)
478+
await _consume_log(stdout)
479479
await proc.wait()
480480
except asyncio.CancelledError:
481481
proc.terminate()
@@ -510,21 +510,14 @@ async def serve_gunicorn(
510510
)
511511
stdout = proc.stdout
512512
assert stdout is not None # noqa: S101
513-
port = None
513+
stdout = _tee_to_stderr(stdout)
514514
try:
515-
for _ in range(100):
516-
line = await stdout.readline()
515+
async for line in stdout:
517516
match = _port_regex.match(line.decode("utf-8"))
518517
if match:
519-
port = int(match.group(1))
520-
break
521-
if b"Booting worker with pid" in line:
518+
port_future.set_result(int(match.group(1)))
522519
break
523-
if port is None:
524-
msg = "Could not determine port from gunicorn output"
525-
raise RuntimeError(msg)
526-
port_future.set_result(port)
527-
await proc.wait()
520+
await _consume_log(stdout)
528521
except asyncio.CancelledError:
529522
proc.terminate()
530523
await proc.wait()
@@ -562,14 +555,14 @@ async def serve_hypercorn(
562555
)
563556
stdout = proc.stdout
564557
assert stdout is not None # noqa: S101
558+
stdout = _tee_to_stderr(stdout)
565559
try:
566-
for _ in range(100):
567-
line = await stdout.readline()
560+
async for line in stdout:
568561
match = _port_regex.match(line.decode("utf-8"))
569562
if match:
570563
port_future.set_result(int(match.group(1)))
571564
break
572-
await proc.wait()
565+
await _consume_log(stdout)
573566
except asyncio.CancelledError:
574567
proc.terminate()
575568
await proc.wait()
@@ -603,14 +596,14 @@ async def serve_uvicorn(
603596
)
604597
stdout = proc.stdout
605598
assert stdout is not None # noqa: S101
599+
stdout = _tee_to_stderr(stdout)
606600
try:
607-
for _ in range(100):
608-
line = await stdout.readline()
601+
async for line in stdout:
609602
match = _port_regex.match(line.decode("utf-8"))
610603
if match:
611604
port_future.set_result(int(match.group(1)))
612605
break
613-
await proc.wait()
606+
await _consume_log(stdout)
614607
except asyncio.CancelledError:
615608
proc.terminate()
616609
await proc.wait()

0 commit comments

Comments
 (0)