Files

147 lines
5.3 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
"""Regression check for asynchronous redraw after Wayland stops frame callbacks.
Requires Weston with its headless backend and --fake-seat support. Runs in a
private compositor/runtime; never sends input to the user's desktop or shells.
"""
import argparse
import os
from pathlib import Path
import re
import shutil
import socket
import struct
import subprocess
import tempfile
import time
PROTOCOL_VERSION = 6
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--binary", type=Path, default=Path("target/debug/psyterm"))
args = parser.parse_args()
binary = args.binary.resolve()
if not binary.is_file() or not shutil.which("weston"):
parser.error("build psyTerm first and install Weston")
runtime = Path(tempfile.mkdtemp(prefix="psyterm-idle-check-"))
server_socket = runtime / "server.sock"
gui_log = runtime / "gui.log"
env = {
**os.environ,
"XDG_RUNTIME_DIR": str(runtime),
"WAYLAND_DISPLAY": "psyterm-test",
"SHELL": "/bin/sh",
"RUST_LOG": "psyterm::timing=debug",
}
env.pop("DISPLAY", None)
def wait_for(predicate, description, timeout=12):
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
result = predicate()
if result:
return result
time.sleep(0.025)
raise AssertionError(f"Timed out waiting for {description}; logs: {runtime}")
def control(*arguments):
return subprocess.check_output(
[str(binary), "--socket", str(server_socket), *arguments],
env=env, cwd=runtime, text=True, timeout=10,
)
def read_exact(stream, count):
result = b""
while len(result) < count:
chunk = stream.recv(count - len(result))
if not chunk:
raise EOFError("server disconnected")
result += chunk
return result
def read_frame(stream):
return read_exact(stream, struct.unpack("<I", read_exact(stream, 4))[0])
def write_frame(stream, payload):
stream.sendall(struct.pack("<I", len(payload)) + payload)
def send_input(session_id, pane, text):
# These prefix commands have stable, regression-tested bincode wire tags.
with socket.socket(socket.AF_UNIX) as stream:
stream.settimeout(5)
stream.connect(str(server_socket))
write_frame(stream, struct.pack("<II", 0, PROTOCOL_VERSION)) # Hello
assert struct.unpack_from("<II", read_frame(stream)) == (0, PROTOCOL_VERSION)
# Join the GUI's session before sending pane commands.
write_frame(stream, struct.pack("<IIBQ", 1, 1, 1, session_id)) # Connect(Attach(Some(id)))
assert struct.unpack("<IQ", read_frame(stream)) == (6, session_id) # Connected
data = text.encode()
write_frame(stream, struct.pack("<IQQ", 2, pane, len(data)) + data)
write_frame(stream, struct.pack("<I", 15)) # Ping, after Input
while True:
reply = read_frame(stream)
tag = struct.unpack_from("<I", reply)[0]
if tag == 4: # Pong
return
if tag == 3:
raise AssertionError(f"server rejected test input: {reply!r}")
def prepared_frames():
return gui_log.read_text().count("prepare_us=") if gui_log.exists() else 0
weston = gui = None
try:
with (runtime / "weston.log").open("w") as log:
weston = subprocess.Popen(
["weston", "-B", "headless", "--renderer=gl", "--width=1200",
"--height=900", "--no-config", "--socket=psyterm-test",
"--fake-seat", "--idle-time=0"],
env=env, cwd=runtime, stdout=log, stderr=log,
)
wait_for(lambda: (runtime / "psyterm-test").exists(), "headless compositor")
with gui_log.open("w") as log:
gui = subprocess.Popen(
[str(binary), "--gui", "--socket", str(server_socket)],
env=env, cwd=runtime, stdout=log, stderr=log,
)
wait_for(prepared_frames, "first terminal frame")
listing = control("--list")
session_id = int(re.search(r"session (\d+)", listing).group(1))
pane = int(re.search(r"panes \[(\d+)", listing).group(1))
# No cursor animation, input event, resize, or screenshot may keep drawing
# alive or accidentally wake the output during the check.
send_input(session_id, pane, "printf '\\033[?25l'\n")
time.sleep(2)
idle_paints = gui_log.read_text().count("paint_us=")
time.sleep(1.25)
assert gui_log.read_text().count("paint_us=") == idle_paints, (
f"Hidden cursor keeps repainting an idle window; logs: {runtime}"
)
before = prepared_frames()
send_input(session_id, pane, "printf '\\033]2;IDLE-DRAW-CHECK\\007Fresh output after idle\\n'\n")
wait_for(lambda: "IDLE-DRAW-CHECK" in control("--list"), "updated server title")
wait_for(lambda: prepared_frames() > before, "updated terminal paint without compositor input", timeout=2)
assert gui.poll() is None, "GUI exited during redraw"
print(f"PASS: idle window stays quiet, then asynchronous output repaints. Logs: {runtime}")
finally:
if gui and gui.poll() is None:
gui.terminate()
gui.wait(timeout=5)
if server_socket.exists():
try:
control("--shutdown")
except (subprocess.SubprocessError, OSError) as error:
print(f"Private test server cleanup failed: {error}")
if weston and weston.poll() is None:
weston.terminate()
weston.wait(timeout=5)