Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions docs/developers-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -1774,14 +1774,6 @@ automates pull-request housekeeping rather than building, and cancelling an
auto-merge mid-flight is a hazard with no minutes to win. `release.yml` keeps
its own `release-${{ github.ref }}` group, which queues rather than cancels.

`tests/workflow_contracts/test_pull_request_concurrency.py` discovers every
workflow declaring a `pull_request` trigger and asserts that its concurrency
block is exactly the one above, by whole-value equality, so a run id anywhere
but the fallback position, a `github.ref` fallback, a constant group, and a
literal `true` all fail. A floor of the five known workflows keeps discovery
from emptying into a vacuous pass, and unit cases drive the judgement with each
refused shape, since the workflows as they stand exercise only the accepted one.

### A callee may not request more than its caller grants

GitHub checks a reusable-workflow call before it starts any job. If a job of
Expand Down
135 changes: 135 additions & 0 deletions test-util/src/server/launch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//! Spawns the server process on an ephemeral port and waits for readiness.

use std::{
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, TcpListener},
process::Child,
};

use tracing::{debug, info, warn};

use super::{DbUrl, ServerTarget, build_server_command, wait_for_server};
use crate::AnyError;

const MAX_SERVER_LAUNCH_ATTEMPTS: u8 = 3;

/// Maps an unspecified bind address to the loopback address that reaches it.
const fn readiness_address(addr: SocketAddr) -> SocketAddr {
match addr.ip() {
IpAddr::V4(ip) if ip.is_unspecified() => {
SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), addr.port())
}
IpAddr::V6(ip) if ip.is_unspecified() => {
SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), addr.port())
}
_ => addr,
}
}

/// Picks a free port on `bind_host` and releases it for the server to claim.
fn reserve_ephemeral_addr(bind_host: &str) -> Result<SocketAddr, AnyError> {
let socket = TcpListener::bind((bind_host, 0))?;
let addr = socket.local_addr()?;
drop(socket);
Ok(addr)
}

/// Kills and reaps a server that never became ready.
#[expect(
clippy::let_underscore_must_use,
reason = "best-effort cleanup; error already being propagated"
)]
fn abandon_child(child: &mut Child) {
let _ = child.kill();
let _ = child.wait();
}

/// The result of one launch attempt that got as far as spawning the server.
enum LaunchOutcome {
/// The server accepted a connection.
Ready(Child, SocketAddr),
/// The server never became ready, and has been killed and reaped.
NotReady(AnyError),
}

/// Starts the server process listening on `addr`.
#[expect(
clippy::cognitive_complexity,
reason = "the tracing macros expand to branches that the lint counts"
)]
fn spawn_server(
target: &ServerTarget,
addr: SocketAddr,
db_url: &DbUrl,
attempt: u8,
) -> Result<Child, AnyError> {
info!(
port = addr.port(),
db_url = %db_url,
attempt,
"launching server"
);
let child = build_server_command(target, addr, db_url).spawn()?;
debug!("spawned server process, waiting for readiness");
Ok(child)
}

/// Waits for `child` to accept connections, killing and reaping it on failure.
#[expect(
clippy::cognitive_complexity,
reason = "the tracing macros expand to branches that the lint counts"
)]
fn await_readiness(mut child: Child, addr: SocketAddr, attempt: u8) -> LaunchOutcome {
match wait_for_server(&mut child, readiness_address(addr)) {
Ok(()) => {
info!(port = addr.port(), attempt, "server ready");
LaunchOutcome::Ready(child, addr)
}
Err(error) => {
warn!(error = %error, attempt, "wait_for_server failed");
abandon_child(&mut child);
LaunchOutcome::NotReady(error)
}
}
}

/// Spawns the server once and waits for its socket to accept connections.
///
/// A failure to reserve a port or spawn the process is returned as an error and
/// is not retried; a server that spawns but never becomes ready is reported as
/// [`LaunchOutcome::NotReady`] so the caller can retry.
fn launch_server_once(
target: &ServerTarget,
bind_host: &str,
db_url: &DbUrl,
attempt: u8,
) -> Result<LaunchOutcome, AnyError> {
let addr = reserve_ephemeral_addr(bind_host)?;
let child = spawn_server(target, addr, db_url, attempt)?;
Ok(await_readiness(child, addr, attempt))
}

/// Spawns the configured server process on an ephemeral port and waits for the
/// socket to accept connections before returning the child handle and chosen
/// port.
pub(super) fn launch_server_process(
target: &ServerTarget,
bind_host: &str,
db_url: &DbUrl,
) -> Result<(Child, SocketAddr), AnyError> {
for attempt in 1..=MAX_SERVER_LAUNCH_ATTEMPTS {
match launch_server_once(target, bind_host, db_url, attempt)? {
LaunchOutcome::Ready(child, addr) => return Ok((child, addr)),
LaunchOutcome::NotReady(error) if attempt == MAX_SERVER_LAUNCH_ATTEMPTS => {
return Err(error);
}
LaunchOutcome::NotReady(_) => warn!(
attempt,
max_attempts = MAX_SERVER_LAUNCH_ATTEMPTS,
"retrying server launch after readiness failure"
),
}
}
Err(anyhow::anyhow!(
"exhausted server launch attempts without readiness success"
))
}
70 changes: 4 additions & 66 deletions test-util/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@

use std::{
ffi::OsString,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, TcpListener},
net::SocketAddr,
path::{Path, PathBuf},
process::{Child, Command, Stdio},
};

mod binary;
mod env;
mod launch;
mod readiness;
mod target;

pub use env::{DbUrl, ManifestPath};
use launch::launch_server_process;
#[cfg(unix)]
use nix::{
sys::signal::{Signal, kill},
Expand All @@ -24,7 +26,7 @@ use nix::{
use readiness::wait_for_server;
use target::ServerTarget;
use tempfile::TempDir;
use tracing::{debug, info, warn};
use tracing::debug;

use crate::AnyError;
#[cfg(feature = "postgres")]
Expand All @@ -37,7 +39,6 @@ use crate::postgres::PostgresTestDb;
const SERVER_BINARY_NAME: &str = "mxd-wireframe-server";
const DEFAULT_BIND_HOST: &str = "127.0.0.1";
const TEST_BIND_HOST_ENV: &str = "MXD_TEST_BIND_HOST";
const MAX_SERVER_LAUNCH_ATTEMPTS: u8 = 3;

#[cfg(not(any(feature = "sqlite", feature = "postgres")))]
compile_error!("Either feature 'sqlite' or 'postgres' must be enabled");
Expand Down Expand Up @@ -144,69 +145,6 @@ fn resolve_bind_host() -> Result<String, AnyError> {
.map_err(|_| anyhow::anyhow!("{TEST_BIND_HOST_ENV} must be valid UTF-8"))
}

/// Spawns the configured server process on an ephemeral port and waits for the
/// socket to accept connections before returning the child handle and chosen
/// port.
#[expect(
clippy::cognitive_complexity,
reason = "process spawning with cleanup on failure has inherent complexity"
)]
#[expect(
clippy::let_underscore_must_use,
reason = "best-effort cleanup; error already being propagated"
)]
fn launch_server_process(
target: &ServerTarget,
bind_host: &str,
db_url: &DbUrl,
) -> Result<(Child, SocketAddr), AnyError> {
for attempt in 1..=MAX_SERVER_LAUNCH_ATTEMPTS {
let socket = TcpListener::bind((bind_host, 0))?;
let addr = socket.local_addr()?;
let readiness_addr = match addr.ip() {
IpAddr::V4(ip) if ip.is_unspecified() => {
SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), addr.port())
}
IpAddr::V6(ip) if ip.is_unspecified() => {
SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), addr.port())
}
_ => addr,
};
drop(socket);

info!(
port = addr.port(),
db_url = %db_url,
attempt,
"launching server"
);
let mut child = build_server_command(target, addr, db_url).spawn()?;
debug!("spawned server process, waiting for readiness");
match wait_for_server(&mut child, readiness_addr) {
Ok(()) => {
info!(port = addr.port(), attempt, "server ready");
return Ok((child, addr));
}
Err(error) => {
warn!(error = %error, attempt, "wait_for_server failed");
let _ = child.kill();
let _ = child.wait();
if attempt == MAX_SERVER_LAUNCH_ATTEMPTS {
return Err(error);
}
warn!(
attempt,
max_attempts = MAX_SERVER_LAUNCH_ATTEMPTS,
"retrying server launch after readiness failure"
);
}
}
}
Err(anyhow::anyhow!(
"exhausted server launch attempts without readiness success"
))
}

/// Integration test server wrapper that spawns the `mxd` process with the
/// selected backend, waits for readiness, and tears it down automatically on
/// drop.
Expand Down
Loading
Loading