diff --git a/crates/scylla-graphql/src/main.rs b/crates/scylla-graphql/src/main.rs index 19e03d3..0077cbe 100644 --- a/crates/scylla-graphql/src/main.rs +++ b/crates/scylla-graphql/src/main.rs @@ -134,6 +134,11 @@ fn main() -> ExitCode { return ExitCode::FAILURE; } }; + // Announce the address actually bound, so `host:0` (an OS-picked port) is usable. + let addr = server + .server_addr() + .to_ip() + .map_or(addr, |bound| bound.to_string()); let context = Context::new(session); let root = schema(); diff --git a/crates/scylla-graphql/tests/api.rs b/crates/scylla-graphql/tests/api.rs index 4b76c52..f546403 100644 --- a/crates/scylla-graphql/tests/api.rs +++ b/crates/scylla-graphql/tests/api.rs @@ -3,47 +3,19 @@ //! introspection, and fail-closed error surfacing (a rejected mutation is a GraphQL `errors`, not a //! silent `ok: false`). -use std::net::TcpListener; -use std::process::{Child, Command, Stdio}; +mod common; + +use std::ffi::OsStr; use std::time::{Duration, Instant}; use scylla_port::{Session, Zoom}; -const ARTIFACT: &str = concat!( - env!("CARGO_MANIFEST_DIR"), - "/../scylla-wasm/web/mathlib.scylla" -); - -fn free_port() -> u16 { - TcpListener::bind("127.0.0.1:0") - .unwrap() - .local_addr() - .unwrap() - .port() -} - -struct Server(Child); -impl Drop for Server { - fn drop(&mut self) { - let _ = self.0.kill(); - let _ = self.0.wait(); - } -} +use common::ARTIFACT; #[test] fn token_gates_and_the_write_path_round_trips() { - let port_num = free_port(); - let addr = format!("127.0.0.1:{port_num}"); + let (_srv, addr) = common::spawn_server(&[("SCYLLA_GRAPHQL_TOKEN", OsStr::new("secret"))]); let base = format!("http://{addr}"); - let _srv = Server( - Command::new(env!("CARGO_BIN_EXE_scylla-graphql")) - .args([ARTIFACT, &addr]) - .env("SCYLLA_GRAPHQL_TOKEN", "secret") - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn() - .expect("spawn scylla-graphql"), - ); // Wait until serving — readiness probe must carry the bearer, since every request is gated. let deadline = Instant::now() + Duration::from_secs(10); diff --git a/crates/scylla-graphql/tests/common/mod.rs b/crates/scylla-graphql/tests/common/mod.rs new file mode 100644 index 0000000..a4b8973 --- /dev/null +++ b/crates/scylla-graphql/tests/common/mod.rs @@ -0,0 +1,58 @@ +//! Shared spawn helper for the `scylla-graphql` integration tests. +//! +//! The server is started on `127.0.0.1:0`, so the OS hands it a port nothing else holds, and the +//! test reads the address it actually bound from its startup line. Picking a "free" port in the +//! test and passing it in raced: the port was released before the server bound it, so a parallel +//! test's server could take it and answer in its place. + +use std::ffi::OsStr; +use std::io::{BufRead, BufReader}; +use std::net::SocketAddr; +use std::process::{Child, Command, Stdio}; + +pub const ARTIFACT: &str = concat!( + env!("CARGO_MANIFEST_DIR"), + "/../scylla-wasm/web/mathlib.scylla" +); + +/// A spawned server, killed on drop (pass or fail). +pub struct Server(Child); +impl Drop for Server { + fn drop(&mut self) { + let _ = self.0.kill(); + let _ = self.0.wait(); + } +} + +/// Start `scylla-graphql` on `ARTIFACT` with these extra environment variables, and return it +/// with the `host:port` it bound. The server is listening by the time this returns. +pub fn spawn_server(envs: &[(&str, &OsStr)]) -> (Server, String) { + let mut child = Command::new(env!("CARGO_BIN_EXE_scylla-graphql")) + .args([ARTIFACT, "127.0.0.1:0"]) + .envs(envs.iter().copied()) + .stdout(Stdio::null()) + .stderr(Stdio::piped()) + .spawn() + .expect("spawn scylla-graphql"); + let stderr = child.stderr.take().expect("piped stderr"); + let server = Server(child); + + // The startup line reads `scylla-graphql: on :///graphql — ...`. + let mut lines = BufReader::new(stderr).lines(); + let addr = loop { + let line = lines + .next() + .expect("scylla-graphql exited before announcing its address") + .expect("read scylla-graphql stderr"); + let announced = line + .split_once("/graphql — ") + .and_then(|(head, _)| head.rsplit_once("://")) + .and_then(|(_, addr)| addr.parse::().ok()); + if let Some(addr) = announced { + break addr.to_string(); + } + }; + // Keep draining stderr so the server never blocks on a full pipe. + std::thread::spawn(move || lines.for_each(drop)); + (server, addr) +} diff --git a/crates/scylla-graphql/tests/conformance.rs b/crates/scylla-graphql/tests/conformance.rs index 1b977a9..5e7a8f1 100644 --- a/crates/scylla-graphql/tests/conformance.rs +++ b/crates/scylla-graphql/tests/conformance.rs @@ -4,39 +4,21 @@ //! verb. Expectations are derived from the port in-process; no frozen golden numbers. If the head //! drifts from the body, these fail. -use std::net::TcpListener; -use std::process::{Child, Command, Stdio}; +mod common; + use std::time::{Duration, Instant}; use base64::engine::general_purpose::STANDARD as B64; use base64::Engine; use scylla_port::{Session, Zoom}; -const ARTIFACT: &str = concat!( - env!("CARGO_MANIFEST_DIR"), - "/../scylla-wasm/web/mathlib.scylla" -); +use common::ARTIFACT; + const PATCHED: &str = concat!( env!("CARGO_MANIFEST_DIR"), "/../scylla-wasm/web/mathlib_patched.scylla" ); -fn free_port() -> u16 { - TcpListener::bind("127.0.0.1:0") - .unwrap() - .local_addr() - .unwrap() - .port() -} - -struct Server(Child); -impl Drop for Server { - fn drop(&mut self) { - let _ = self.0.kill(); - let _ = self.0.wait(); - } -} - /// The port, loaded the same way the head loads it — the source of truth for the contract. fn port(path: &str) -> Session { Session::from_artifact(&std::fs::read(path).expect("read artifact")).expect("load artifact") @@ -47,17 +29,8 @@ fn graphql_head_conforms_to_the_port() { let p = port(ARTIFACT); let prog = p.program(); - let port_num = free_port(); - let addr = format!("127.0.0.1:{port_num}"); + let (_srv, addr) = common::spawn_server(&[]); let base = format!("http://{addr}"); - let _srv = Server( - Command::new(env!("CARGO_BIN_EXE_scylla-graphql")) - .args([ARTIFACT, &addr]) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn() - .expect("spawn scylla-graphql"), - ); // Wait until it's serving. let deadline = Instant::now() + Duration::from_secs(10); diff --git a/crates/scylla-rpc/tests/common/mod.rs b/crates/scylla-rpc/tests/common/mod.rs new file mode 100644 index 0000000..c0ad5e6 --- /dev/null +++ b/crates/scylla-rpc/tests/common/mod.rs @@ -0,0 +1,58 @@ +//! Shared spawn helper for the `scylla-rpc` integration tests. +//! +//! The server is started on `127.0.0.1:0`, so the OS hands it a port nothing else holds, and the +//! test reads the address it actually bound from its startup line. Picking a "free" port in the +//! test and passing it in raced: the port was released before the server bound it, so a parallel +//! test's server could take it and answer in its place. + +use std::ffi::OsStr; +use std::io::{BufRead, BufReader}; +use std::net::SocketAddr; +use std::process::{Child, Command, Stdio}; + +pub const ARTIFACT: &str = concat!( + env!("CARGO_MANIFEST_DIR"), + "/../scylla-wasm/web/mathlib.scylla" +); + +/// A spawned server, killed on drop (pass or fail). +pub struct Server(Child); +impl Drop for Server { + fn drop(&mut self) { + let _ = self.0.kill(); + let _ = self.0.wait(); + } +} + +/// Start `scylla-rpc-serve` on `ARTIFACT` with these extra environment variables, and return it +/// with the `host:port` it bound. The server is listening by the time this returns. +pub fn spawn_server(envs: &[(&str, &OsStr)]) -> (Server, String) { + let mut child = Command::new(env!("CARGO_BIN_EXE_scylla-rpc-serve")) + .args([ARTIFACT, "127.0.0.1:0"]) + .envs(envs.iter().copied()) + .stdout(Stdio::null()) + .stderr(Stdio::piped()) + .spawn() + .expect("spawn scylla-rpc-serve"); + let stderr = child.stderr.take().expect("piped stderr"); + let server = Server(child); + + // The startup line reads `scylla-rpc-serve: on — , ...`. + let mut lines = BufReader::new(stderr).lines(); + let addr = loop { + let line = lines + .next() + .expect("scylla-rpc-serve exited before announcing its address") + .expect("read scylla-rpc-serve stderr"); + let announced = line + .split_once(" — ") + .and_then(|(head, _)| head.rsplit_once(" on ")) + .and_then(|(_, addr)| addr.parse::().ok()); + if let Some(addr) = announced { + break addr.to_string(); + } + }; + // Keep draining stderr so the server never blocks on a full pipe. + std::thread::spawn(move || lines.for_each(drop)); + (server, addr) +} diff --git a/crates/scylla-rpc/tests/conformance.rs b/crates/scylla-rpc/tests/conformance.rs index 8e9ad78..2af150e 100644 --- a/crates/scylla-rpc/tests/conformance.rs +++ b/crates/scylla-rpc/tests/conformance.rs @@ -4,37 +4,20 @@ //! the same artifact — verb by verb. Expectations are derived from the port in-process; no frozen //! golden numbers (those live in `remote.rs`). A head drifting from the body fails here. -use std::net::TcpListener; -use std::process::{Child, Command, Stdio}; +mod common; + +use std::process::Command; use std::time::{Duration, Instant}; use scylla_port::{Session, Zoom}; -const ARTIFACT: &str = concat!( - env!("CARGO_MANIFEST_DIR"), - "/../scylla-wasm/web/mathlib.scylla" -); +use common::ARTIFACT; + const PATCHED: &str = concat!( env!("CARGO_MANIFEST_DIR"), "/../scylla-wasm/web/mathlib_patched.scylla" ); -fn free_port() -> u16 { - TcpListener::bind("127.0.0.1:0") - .unwrap() - .local_addr() - .unwrap() - .port() -} - -struct Server(Child); -impl Drop for Server { - fn drop(&mut self) { - let _ = self.0.kill(); - let _ = self.0.wait(); - } -} - /// Run the remote client (no token — the server runs open here), returning `(exit, stdout)`. fn connect(addr: &str, args: &[&str]) -> (i32, String) { let mut full = vec![addr]; @@ -80,16 +63,7 @@ fn rpc_head_conforms_to_the_port() { let p = port(ARTIFACT); let prog = p.program(); - let pnum = free_port(); - let addr = format!("127.0.0.1:{pnum}"); - let _srv = Server( - Command::new(env!("CARGO_BIN_EXE_scylla-rpc-serve")) - .args([ARTIFACT, &addr]) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn() - .expect("spawn scylla-rpc-serve"), - ); + let (_srv, addr) = common::spawn_server(&[]); // Wait until the server accepts (info round-trips). let deadline = Instant::now() + Duration::from_secs(10); diff --git a/crates/scylla-rpc/tests/remote.rs b/crates/scylla-rpc/tests/remote.rs index 0acbb5c..323ee69 100644 --- a/crates/scylla-rpc/tests/remote.rs +++ b/crates/scylla-rpc/tests/remote.rs @@ -2,36 +2,17 @@ //! `scylla-rpc-connect` remote head — a consumer not co-located with the core, navigating the //! client port over a Cap'n Proto RPC wire. Zero deps (std only). -use std::net::TcpListener; -use std::process::{Child, Command, Stdio}; +mod common; + +use std::ffi::OsStr; +use std::process::Command; use std::time::{Duration, Instant}; -const ARTIFACT: &str = concat!( - env!("CARGO_MANIFEST_DIR"), - "/../scylla-wasm/web/mathlib.scylla" -); const PATCHED: &str = concat!( env!("CARGO_MANIFEST_DIR"), "/../scylla-wasm/web/mathlib_patched.scylla" ); -fn free_port() -> u16 { - TcpListener::bind("127.0.0.1:0") - .unwrap() - .local_addr() - .unwrap() - .port() -} - -/// A spawned server, killed on drop. -struct Server(Child); -impl Drop for Server { - fn drop(&mut self) { - let _ = self.0.kill(); - let _ = self.0.wait(); - } -} - /// Run the remote client against `addr` with `args` and an optional `SCYLLA_RPC_TOKEN`, returning /// `(exit_code, stdout)`. fn connect_with(addr: &str, args: &[&str], token: Option<&str>) -> (i32, String) { @@ -61,16 +42,7 @@ fn connect(addr: &str, args: &[&str]) -> (i32, String) { #[test] fn remote_head_drives_the_port_over_tcp() { - let port = free_port(); - let addr = format!("127.0.0.1:{port}"); - let _srv = Server( - Command::new(env!("CARGO_BIN_EXE_scylla-rpc-serve")) - .args([ARTIFACT, &addr]) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn() - .expect("spawn scylla-rpc-serve"), - ); + let (_srv, addr) = common::spawn_server(&[]); // Wait until the server is accepting (the client `info` round-trips cleanly). let deadline = Instant::now() + Duration::from_secs(10); @@ -124,17 +96,8 @@ fn remote_head_drives_the_port_over_tcp() { #[test] fn token_gated_server_denies_without_the_token_over_tcp() { - let port = free_port(); - let addr = format!("127.0.0.1:{port}"); - let _srv = Server( - Command::new(env!("CARGO_BIN_EXE_scylla-rpc-serve")) - .args([ARTIFACT, &addr]) - .env("SCYLLA_RPC_TOKEN", "s3cret") // gate access (DD-035) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn() - .expect("spawn scylla-rpc-serve"), - ); + // Gate access (DD-035). + let (_srv, addr) = common::spawn_server(&[("SCYLLA_RPC_TOKEN", OsStr::new("s3cret"))]); // Ready when the RIGHT token logs in. let deadline = Instant::now() + Duration::from_secs(10); @@ -171,17 +134,8 @@ fn token_gated_server_denies_without_the_token_over_tcp() { #[test] fn connection_cap_refuses_the_surplus_connection() { use std::net::TcpStream; - let port = free_port(); - let addr = format!("127.0.0.1:{port}"); - let _srv = Server( - Command::new(env!("CARGO_BIN_EXE_scylla-rpc-serve")) - .args([ARTIFACT, &addr]) - .env("SCYLLA_RPC_MAX_CONN", "1") // a single slot - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn() - .expect("spawn scylla-rpc-serve"), - ); + // A single slot. + let (_srv, addr) = common::spawn_server(&[("SCYLLA_RPC_MAX_CONN", OsStr::new("1"))]); // Ready (a normal client connects, completes, frees its slot). let deadline = Instant::now() + Duration::from_secs(10); @@ -218,18 +172,11 @@ fn connection_cap_refuses_the_surplus_connection() { #[test] fn handshake_timeout_frees_a_slot_from_a_silent_connection() { use std::net::TcpStream; - let port = free_port(); - let addr = format!("127.0.0.1:{port}"); - let _srv = Server( - Command::new(env!("CARGO_BIN_EXE_scylla-rpc-serve")) - .args([ARTIFACT, &addr]) - .env("SCYLLA_RPC_MAX_CONN", "1") // a single slot… - .env("SCYLLA_RPC_HANDSHAKE_SEC", "1") // …dropped if not authed within 1s - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn() - .expect("spawn scylla-rpc-serve"), - ); + // A single slot, dropped if not authed within 1s. + let (_srv, addr) = common::spawn_server(&[ + ("SCYLLA_RPC_MAX_CONN", OsStr::new("1")), + ("SCYLLA_RPC_HANDSHAKE_SEC", OsStr::new("1")), + ]); let deadline = Instant::now() + Duration::from_secs(10); loop { if connect(&addr, &["info"]).0 == 0 { @@ -260,17 +207,8 @@ fn handshake_timeout_frees_a_slot_from_a_silent_connection() { #[test] fn whitespace_only_token_keeps_server_open() { - let port = free_port(); - let addr = format!("127.0.0.1:{port}"); - let _srv = Server( - Command::new(env!("CARGO_BIN_EXE_scylla-rpc-serve")) - .args([ARTIFACT, &addr]) - .env("SCYLLA_RPC_TOKEN", " \t ") // blank/whitespace-only value treated as unset (OPEN) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn() - .expect("spawn scylla-rpc-serve"), - ); + // A blank/whitespace-only value is treated as unset (OPEN). + let (_srv, addr) = common::spawn_server(&[("SCYLLA_RPC_TOKEN", OsStr::new(" \t "))]); let deadline = Instant::now() + Duration::from_secs(10); loop { diff --git a/crates/scylla-rpc/tests/tls.rs b/crates/scylla-rpc/tests/tls.rs index 945d06a..5c4464b 100644 --- a/crates/scylla-rpc/tests/tls.rs +++ b/crates/scylla-rpc/tests/tls.rs @@ -2,30 +2,10 @@ //! `scylla-rpc-serve` with TLS, and drive it with `scylla-rpc-connect` over TLS — so the auth token //! and the model never cross the wire in the clear. A plaintext client is rejected. -use std::net::TcpListener; -use std::process::{Child, Command, Stdio}; -use std::time::{Duration, Instant}; - -const ARTIFACT: &str = concat!( - env!("CARGO_MANIFEST_DIR"), - "/../scylla-wasm/web/mathlib.scylla" -); - -fn free_port() -> u16 { - TcpListener::bind("127.0.0.1:0") - .unwrap() - .local_addr() - .unwrap() - .port() -} +mod common; -struct Server(Child); -impl Drop for Server { - fn drop(&mut self) { - let _ = self.0.kill(); - let _ = self.0.wait(); - } -} +use std::process::Command; +use std::time::{Duration, Instant}; #[test] fn remote_head_works_over_tls_and_rejects_plaintext() { @@ -38,18 +18,10 @@ fn remote_head_works_over_tls_and_rejects_plaintext() { std::fs::write(&cert_path, ck.cert.pem()).unwrap(); std::fs::write(&key_path, ck.key_pair.serialize_pem()).unwrap(); - let port = free_port(); - let addr = format!("127.0.0.1:{port}"); - let _srv = Server( - Command::new(env!("CARGO_BIN_EXE_scylla-rpc-serve")) - .args([ARTIFACT, &addr]) - .env("SCYLLA_RPC_TLS_CERT", &cert_path) - .env("SCYLLA_RPC_TLS_KEY", &key_path) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn() - .expect("spawn scylla-rpc-serve"), - ); + let (_srv, addr) = common::spawn_server(&[ + ("SCYLLA_RPC_TLS_CERT", cert_path.as_os_str()), + ("SCYLLA_RPC_TLS_KEY", key_path.as_os_str()), + ]); // A TLS client trusts the cert (as the CA) and verifies the "localhost" name. let tls_connect = |args: &[&str]| -> (i32, String) { diff --git a/crates/scylla-serve/src/main.rs b/crates/scylla-serve/src/main.rs index 4cd9fad..fd731cb 100644 --- a/crates/scylla-serve/src/main.rs +++ b/crates/scylla-serve/src/main.rs @@ -63,6 +63,8 @@ fn main() { std::process::exit(1); } }; + // Announce the port actually bound, so port 0 (an OS-picked port) is usable. + let port = listener.local_addr().map_or(port, |bound| bound.port()); match (&compare_path, &compare) { (Some(cp), Some(c)) => println!( "scylla-serve: {} ({} bytes) vs compare {} ({} bytes) → http://127.0.0.1:{port}/ (Ctrl-C to stop)", diff --git a/crates/scylla-serve/tests/common/mod.rs b/crates/scylla-serve/tests/common/mod.rs new file mode 100644 index 0000000..cc47e00 --- /dev/null +++ b/crates/scylla-serve/tests/common/mod.rs @@ -0,0 +1,52 @@ +//! Shared spawn helper for the `scylla-serve` integration tests. +//! +//! The server is started on port 0, so the OS hands it a port nothing else holds, and the test +//! reads the address it actually bound from its startup line. Picking a "free" port in the test +//! and passing it in raced: the port was released before the server bound it, so a parallel +//! test's server could take it and answer in its place. + +use std::io::{BufRead, BufReader}; +use std::net::SocketAddr; +use std::process::{Child, Command, Stdio}; + +/// A spawned server, killed on drop (pass or fail). +pub struct Server(Child); +impl Drop for Server { + fn drop(&mut self) { + let _ = self.0.kill(); + let _ = self.0.wait(); + } +} + +/// Start `scylla-serve` with these artifact arguments on port 0, and return it with the +/// `host:port` it bound. The server is listening by the time this returns. +pub fn spawn_server(artifacts: &[&str]) -> (Server, String) { + let mut child = Command::new(env!("CARGO_BIN_EXE_scylla-serve")) + .args(artifacts) + .arg("0") + .stdout(Stdio::piped()) + .stderr(Stdio::null()) + .spawn() + .expect("spawn scylla-serve"); + let stdout = child.stdout.take().expect("piped stdout"); + let server = Server(child); + + // The startup line (on stdout) reads `scylla-serve: ... → http:/// ...`. + let mut lines = BufReader::new(stdout).lines(); + let addr = loop { + let line = lines + .next() + .expect("scylla-serve exited before announcing its address") + .expect("read scylla-serve stdout"); + let announced = line + .rsplit_once("→ http://") + .and_then(|(_, rest)| rest.split_once('/')) + .and_then(|(addr, _)| addr.parse::().ok()); + if let Some(addr) = announced { + break addr.to_string(); + } + }; + // Keep draining stdout so the server never blocks on a full pipe. + std::thread::spawn(move || lines.for_each(drop)); + (server, addr) +} diff --git a/crates/scylla-serve/tests/serve.rs b/crates/scylla-serve/tests/serve.rs index c9eeb4f..be072ce 100644 --- a/crates/scylla-serve/tests/serve.rs +++ b/crates/scylla-serve/tests/serve.rs @@ -2,9 +2,10 @@ //! ephemeral port and exercise every route over raw HTTP/1.1 — so arg parsing + serving are //! CI-protected, not just hand-smoke-tested. Zero deps (std only), matching the crate. +mod common; + use std::io::{Read, Write}; -use std::net::{TcpListener, TcpStream}; -use std::process::{Child, Command, Stdio}; +use std::net::TcpStream; use std::time::{Duration, Instant}; // The committed sample artifacts (this crate's manifest dir is crates/scylla-serve). @@ -17,19 +18,9 @@ const COMPARE: &str = concat!( "/../scylla-wasm/web/mathlib_rebuilt.scylla" ); -/// Ask the OS for a free port, then drop the listener so the child can bind it. (Tiny race window; -/// fine for a test — if the child loses the race it exits and `wait_ready` fails fast.) -fn free_port() -> u16 { - TcpListener::bind("127.0.0.1:0") - .unwrap() - .local_addr() - .unwrap() - .port() -} - /// One raw HTTP/1.1 `GET` (Connection: close) → `(status_line, header_block, body_bytes)`. -fn get(port: u16, path: &str) -> (String, String, Vec) { - let mut s = TcpStream::connect(("127.0.0.1", port)).unwrap(); +fn get(addr: &str, path: &str) -> (String, String, Vec) { + let mut s = TcpStream::connect(addr).unwrap(); let req = format!("GET {path} HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"); s.write_all(req.as_bytes()).unwrap(); let mut buf = Vec::new(); @@ -44,84 +35,63 @@ fn get(port: u16, path: &str) -> (String, String, Vec) { (status, head, body) } -/// A spawned server that is killed on drop. -struct Server(Child); -impl Drop for Server { - fn drop(&mut self) { - let _ = self.0.kill(); - let _ = self.0.wait(); - } -} - -fn spawn(args: &[&str]) -> Server { - let child = Command::new(env!("CARGO_BIN_EXE_scylla-serve")) - .args(args) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn() - .expect("spawn scylla-serve"); - Server(child) -} - -fn wait_ready(port: u16) { +fn wait_ready(addr: &str) { let deadline = Instant::now() + Duration::from_secs(5); while Instant::now() < deadline { - if TcpStream::connect(("127.0.0.1", port)).is_ok() { + if TcpStream::connect(addr).is_ok() { return; } std::thread::sleep(Duration::from_millis(25)); } - panic!("scylla-serve did not become ready on port {port}"); + panic!("scylla-serve did not become ready on {addr}"); } #[test] fn serves_head_artifact_and_compare() { - let port = free_port(); - let _srv = spawn(&[BASE, COMPARE, &port.to_string()]); - wait_ready(port); + let (_srv, addr) = common::spawn_server(&[BASE, COMPARE]); + wait_ready(&addr); let base = std::fs::read(BASE).unwrap(); let compare = std::fs::read(COMPARE).unwrap(); // the head itself - let (status, head, body) = get(port, "/"); + let (status, head, body) = get(&addr, "/"); assert!(status.contains("200"), "GET / → {status}"); assert!(head.contains("text/html"), "GET / content-type: {head}"); assert!(String::from_utf8_lossy(&body).contains("Scylla — WASM head")); // the wasm module (magic bytes \0asm) - let (status, head, body) = get(port, "/scylla_wasm.wasm"); + let (status, head, body) = get(&addr, "/scylla_wasm.wasm"); assert!(status.contains("200")); assert!(head.contains("application/wasm")); assert!(body.len() >= 4 && &body[..4] == b"\0asm", "wasm magic"); // the base artifact, exact bytes, where the head fetches it - let (status, _h, body) = get(port, "/mathlib.scylla"); + let (status, _h, body) = get(&addr, "/mathlib.scylla"); assert!(status.contains("200")); assert_eq!(body, base, "base artifact bytes"); // the compare build, exact bytes - let (status, _h, body) = get(port, "/compare.scylla"); + let (status, _h, body) = get(&addr, "/compare.scylla"); assert!(status.contains("200")); assert_eq!(body, compare, "compare artifact bytes"); // anything else - let (status, _h, _b) = get(port, "/nope"); + let (status, _h, _b) = get(&addr, "/nope"); assert!(status.contains("404"), "GET /nope → {status}"); } #[test] fn compare_404s_without_a_second_artifact() { - let port = free_port(); - let _srv = spawn(&[BASE, &port.to_string()]); - wait_ready(port); + let (_srv, addr) = common::spawn_server(&[BASE]); + wait_ready(&addr); - let (status, _h, _b) = get(port, "/compare.scylla"); + let (status, _h, _b) = get(&addr, "/compare.scylla"); assert!( status.contains("404"), "single-artifact /compare.scylla → {status}" ); // sanity: the base artifact is still served in single-artifact mode - let (s2, _h, _b) = get(port, "/mathlib.scylla"); + let (s2, _h, _b) = get(&addr, "/mathlib.scylla"); assert!(s2.contains("200")); }