test(release): preserve PTY exit output and await durable admission

This commit is contained in:
Xubin Ren committed 2026-10-28 16:54:41 +08:00
1 parent 8ee81cc78a
commit e9532d4100
2 files changed
+28 -14

No files matched your search

+13 -10
View File
@@ -1176,22 +1176,25 @@ async def test_unified_websocket_followup_admits_effective_session(tmp_path):
pending = asyncio.Queue(maxsize=20) pending = asyncio.Queue(maxsize=20)
loop._pending_queues[UNIFIED_SESSION_KEY] = pending loop._pending_queues[UNIFIED_SESSION_KEY] = pending
run_task = asyncio.create_task(loop.run())
msg = InboundMessage( msg = InboundMessage(
channel="websocket", channel="websocket",
sender_id="u", sender_id="u",
chat_id="chat", chat_id="chat",
content="new request", content="new request",
) )
await loop.bus.publish_inbound(msg) try:
# Exercise the real routing/journaling boundary to completion. This is
queued_msg = await asyncio.wait_for(pending.get(), timeout=2) # a session-key contract, not a two-second SQLite fsync benchmark.
admitted_msg = admission.admit.await_args.args[0] await loop._dispatch_inbound(msg)
assert admitted_msg.session_key == UNIFIED_SESSION_KEY queued_msg = pending.get_nowait()
assert queued_msg.session_key == UNIFIED_SESSION_KEY admission.admit.assert_awaited_once()
admitted_msg = admission.admit.await_args.args[0]
loop.stop() assert admitted_msg.session_key == UNIFIED_SESSION_KEY
await asyncio.wait_for(run_task, timeout=2) assert queued_msg.session_key == UNIFIED_SESSION_KEY
assert queued_msg.content == msg.content
assert queued_msg.metadata["_recovery_followup_id"]
finally:
await loop.aclose()
@pytest.mark.asyncio @pytest.mark.asyncio
+15 -4
View File
@@ -16,6 +16,7 @@ import select
import shutil import shutil
import signal import signal
import struct import struct
import sys
import termios import termios
import time import time
from pathlib import Path from pathlib import Path
@@ -59,13 +60,15 @@ def _wait_for(fd: int, needle: bytes, timeout: float) -> bytes:
return bytes(output) return bytes(output)
def _wait_for_exit(pid: int, timeout: float) -> int: def _wait_for_exit(pid: int, fd: int, output: bytearray, timeout: float) -> int:
deadline = time.monotonic() + timeout deadline = time.monotonic() + timeout
while time.monotonic() < deadline: while time.monotonic() < deadline:
# Drain while waiting: a controlling terminal can hold child exit until
# the parent consumes its final output (notably on macOS).
output.extend(_read(fd, min(0.05, deadline - time.monotonic())))
child, status = os.waitpid(pid, os.WNOHANG) child, status = os.waitpid(pid, os.WNOHANG)
if child: if child:
return os.waitstatus_to_exitcode(status) return os.waitstatus_to_exitcode(status)
time.sleep(0.05)
os.kill(pid, signal.SIGKILL) os.kill(pid, signal.SIGKILL)
os.waitpid(pid, 0) os.waitpid(pid, 0)
raise AssertionError("TUI did not exit after the exit command") raise AssertionError("TUI did not exit after the exit command")
@@ -98,6 +101,11 @@ def smoke(executable: Path | None = None, *, cwd: Path = ROOT) -> float:
started = time.perf_counter() started = time.perf_counter()
pid, master = pty.fork() pid, master = pty.fork()
if pid == 0: if pid == 0:
if sys.platform == "darwin":
# forkpty alone can lose unread output when the last slave closes
# on macOS. Opening the controlling tty establishes S_CTTYREF so
# exit drains its output, as a real interactive shell would.
os.close(os.open("/dev/tty", os.O_RDWR))
os.chdir(cwd) os.chdir(cwd)
os.execvpe(command[0], command, env) os.execvpe(command[0], command, env)
@@ -131,7 +139,7 @@ def smoke(executable: Path | None = None, *, cwd: Path = ROOT) -> float:
output.extend(_read(master, 0.2)) output.extend(_read(master, 0.2))
os.write(master, b"exit\r") os.write(master, b"exit\r")
output.extend(_wait_for(master, LEAVE_ALT_SCREEN, 5)) output.extend(_wait_for(master, LEAVE_ALT_SCREEN, 5))
exit_code = _wait_for_exit(pid, 5) exit_code = _wait_for_exit(pid, master, output, 5)
output.extend(_read(master, 0.5)) output.extend(_read(master, 0.5))
reaped = True reaped = True
finally: finally:
@@ -154,7 +162,10 @@ def smoke(executable: Path | None = None, *, cwd: Path = ROOT) -> float:
if "Traceback" in text or "Task exception was never retrieved" in text: if "Traceback" in text or "Task exception was never retrieved" in text:
raise AssertionError("TUI emitted an exception during shutdown") raise AssertionError("TUI emitted an exception during shutdown")
if output.count(RESUME_COMMAND) != 1: if output.count(RESUME_COMMAND) != 1:
raise AssertionError("TUI did not print exactly one reusable session command") raise AssertionError(
f"TUI printed {output.count(RESUME_COMMAND)} reusable session commands; "
f"recent output: {output[-1000:]!r}"
)
if output.index(RESUME_COMMAND) < output.index(LEAVE_ALT_SCREEN): if output.index(RESUME_COMMAND) < output.index(LEAVE_ALT_SCREEN):
raise AssertionError("TUI printed the session command before restoring the terminal") raise AssertionError("TUI printed the session command before restoring the terminal")
# The UI must inherit the host terminal background. Fixed RGB/indexed # The UI must inherit the host terminal background. Fixed RGB/indexed