Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
Add the windowless Studio host with authenticated binary control/frame channels, managed input, pause/resume/stop, frame ownership and save-aware shutdown. Preserve deterministic headless execution and add protocol/process fixtures. Separate unit CI and coverage from opt-in integration/socket qualification, retaining the prior combined coverage floors in the configuration history. Validation: tooling make verify passed runtime ci-domains and integration.
216 lines
7.9 KiB
Rust
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()), 480);
|
|
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 + 480 * 270 * 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());
|
|
}
|