Files
prometeu-runtime/crates/host/prometeu-host-desktop-winit/tests/studio_process.rs
T
bquarkz a32f834b2b
Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
feat(graphics): migrate native target to 640x360
Centralize the fixed 640x360 RGBA8888/60 Hz contract in the existing HAL.
Preserve 8/16/32 tiles and composition, cover partial edges and mixed-size
camera movement, and adapt firmware, fixtures, hosts and native tests.

Share nearest-neighbor presentation geometry with pointer input. Measure
actual framebuffer publications and successful host presentations separately
from VM ticks, with bounded opt-in diagnostics and release harnesses.

Validated with the complete tooling verify gate, native rendering tests,
GPU readback, headless/process integration, formatting and strict clippy.
The existing synchronous session path is documented; no worker/session
architecture replacement or format compatibility layer is introduced.

Work: WRK-0016 / DEC-0001 / PLN-0001
Commit and push explicitly authorized by the user after completion.
2026-11-20 10:34:45 +00:00

216 lines
7.9 KiB
Rust

//! Real process qualification: no window, slow consumer, pause and broken IPC.
use prometeu_host_desktop_winit::studio::wire::*;
use std::io::{self, BufRead, Write};
use std::net::{Shutdown, TcpListener, TcpStream};
use std::process::{Child, Command, ExitStatus, Stdio};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
struct Session {
child: Child,
control: Reader<TcpStream>,
writer: Writer<TcpStream>,
video: Reader<TcpStream>,
control_socket: TcpStream,
video_socket: TcpStream,
logs: Arc<Mutex<Vec<String>>>,
}
impl Session {
fn start() -> Self {
let control = TcpListener::bind("127.0.0.1:0").unwrap();
let video = TcpListener::bind("127.0.0.1:0").unwrap();
let cart = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../../test-cartridges/dummy-boy");
let child = Command::new(env!("CARGO_BIN_EXE_prometeu-host-desktop-winit"))
.args(["--studio", "--run"])
.arg(cart)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();
let logs = Arc::new(Mutex::new(Vec::new()));
let mut session_child = child;
for stream in [
Box::new(session_child.stdout.take().unwrap()) as Box<dyn io::Read + Send>,
Box::new(session_child.stderr.take().unwrap()) as Box<dyn io::Read + Send>,
] {
let log = logs.clone();
std::thread::spawn(move || {
for line in io::BufReader::new(stream).lines().map_while(Result::ok) {
let mut lines = log.lock().unwrap();
if lines.len() == 200 {
lines.remove(0);
}
lines.push(line);
}
});
}
let mut p = vec![3; 16];
p.extend_from_slice(&[7; 32]);
p.extend_from_slice(&control.local_addr().unwrap().port().to_le_bytes());
p.extend_from_slice(&video.local_addr().unwrap().port().to_le_bytes());
p.push(1);
Writer::new(session_child.stdin.take().unwrap()).send(BOOTSTRAP, 0, &p).unwrap();
fn accept(listener: TcpListener) -> TcpStream {
listener.set_nonblocking(true).unwrap();
let deadline = Instant::now() + Duration::from_secs(10);
loop {
match listener.accept() {
Ok((s, _)) => {
s.set_nonblocking(false).unwrap();
s.set_read_timeout(Some(Duration::from_secs(10))).unwrap();
return s;
}
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
assert!(Instant::now() < deadline, "child did not connect");
std::thread::sleep(Duration::from_millis(5));
}
Err(e) => panic!("accept: {e}"),
}
}
}
let control = accept(control);
let video = accept(video);
let mut c = Reader::new(control.try_clone().unwrap());
let mut v = Reader::new(video.try_clone().unwrap());
for (message, role) in [(c.receive().unwrap(), 1), (v.receive().unwrap(), 2)] {
assert_eq!(message.header.kind, HELLO);
assert_eq!(&message.payload[..16], &[3; 16]);
assert_eq!(&message.payload[16..48], &[7; 32]);
assert_eq!(message.payload[48], role);
}
let control_socket = control.try_clone().unwrap();
let mut session = Self {
child: session_child,
control: c,
writer: Writer::new(control),
video: v,
control_socket,
video_socket: video,
logs,
};
let ready = session.control.receive().unwrap();
assert_eq!(ready.header.kind, READY, "{:?}", session.logs.lock().unwrap());
assert_eq!(u32::from_le_bytes(ready.payload[..4].try_into().unwrap()), 640);
session
}
fn command(&mut self, kind: u16, request: u64) -> Message {
self.writer.send(kind, request, &[]).unwrap();
let r = self.control.receive().unwrap();
assert_eq!(r.header.kind, RESULT, "{:?}", self.logs.lock().unwrap());
assert_eq!(r.header.request, request);
assert_eq!(u16::from_le_bytes(r.payload[2..4].try_into().unwrap()), 0, "{:?}", r.payload);
r
}
fn ping(&mut self) -> (u64, u64, u32) {
self.writer.send(PING, 0, &[]).unwrap();
let m = self.control.receive().unwrap();
assert_eq!(m.header.kind, PONG);
(
u64::from_le_bytes(m.payload[..8].try_into().unwrap()),
u64::from_le_bytes(m.payload[8..16].try_into().unwrap()),
u32::from_le_bytes(m.payload[16..20].try_into().unwrap()),
)
}
fn frame(&mut self) -> Message {
let m = self
.video
.receive()
.unwrap_or_else(|e| panic!("video: {e}; logs: {:?}", self.logs.lock().unwrap()));
assert_eq!(m.header.kind, FRAME);
assert_eq!(m.payload.len(), 48 + 640 * 360 * 4);
m
}
fn ack(&mut self, frame: &Message) {
self.writer.send(FRAME_RECEIVED, 0, &frame.payload[16..32]).unwrap();
}
fn exit(&mut self) -> ExitStatus {
let deadline = Instant::now() + Duration::from_secs(4);
loop {
if let Some(status) = self.child.try_wait().unwrap() {
return status;
}
assert!(
Instant::now() < deadline,
"runtime did not exit: {:?}",
self.logs.lock().unwrap()
);
std::thread::sleep(Duration::from_millis(10));
}
}
}
impl Drop for Session {
fn drop(&mut self) {
let _ = self.child.kill();
let _ = self.child.wait();
}
}
#[test]
fn slow_video_does_not_stop_ticks_and_pause_resumes_same_process() {
let mut s = Session::start();
let pid = s.child.id();
s.command(START, 1);
let first = s.frame();
let initial = s.ping();
std::thread::sleep(Duration::from_millis(150));
let later = s.ping();
assert!(later.1 > initial.1);
// No credit was returned while the runtime kept publishing. The next frame skips old presentations.
s.ack(&first);
let recent = s.frame();
assert!(
u64::from_le_bytes(recent.payload[24..32].try_into().unwrap())
> u64::from_le_bytes(first.payload[24..32].try_into().unwrap()) + 1
);
s.command(PAUSE, 2);
let paused = s.ping();
assert_eq!(paused.2, 2);
std::thread::sleep(Duration::from_millis(80));
assert_eq!(s.ping().1, paused.1);
s.command(RESUME, 3);
std::thread::sleep(Duration::from_millis(60));
assert!(s.ping().1 > paused.1);
assert_eq!(s.child.id(), pid);
s.command(STOP, 4);
assert_eq!(s.control.receive().unwrap().header.kind, STOPPED);
assert!(s.exit().success());
}
#[test]
fn broken_video_ends_runtime_even_when_paused() {
let mut s = Session::start();
s.command(START, 1);
s.command(PAUSE, 2);
s.video_socket.shutdown(Shutdown::Both).unwrap();
assert!(!s.exit().success());
}
#[test]
fn startup_focus_loss_arms_game_without_advancing_until_manual_resume() {
let mut s = Session::start();
let initial = s.ping().1;
s.command(PAUSE, 1);
s.command(START, 2);
std::thread::sleep(Duration::from_millis(50));
let paused = s.ping();
assert_eq!(paused.1, initial);
assert_eq!(paused.2, 2);
s.command(RESUME, 3);
let _ = s.frame();
assert!(s.ping().1 > initial);
s.command(STOP, 4);
assert_eq!(s.control.receive().unwrap().header.kind, STOPPED);
assert!(s.exit().success());
}
#[test]
fn truncated_control_ends_only_its_owned_runtime() {
let mut s = Session::start();
s.command(START, 1);
s.control_socket.write_all(b"PST").unwrap();
s.control_socket.shutdown(Shutdown::Write).unwrap();
assert!(!s.exit().success());
}