crates/nordisk-alloc/src/selftest.rs
//! # Functional-status emit bridge — `nordisk-alloc` → test matrix
//!
//! The single internal call site this crate uses to push a *functional* row into the
//! [`nornir_testmatrix`] matrix, so `nornir test --features testmatrix` records whether
//! `nordisk-alloc`'s real surfaces (the env ladder, the per-product override, the
//! option-row fingerprint, the actual write) ACTUALLY behave — without eyeballing
//! anything.
//!
//! ## Release strips everything
//! The `testmatrix` cargo feature gates the whole bridge: with it OFF (the release
//! default) [`emit`] is an `#[inline]` no-op and `nornir-testmatrix` is not even
//! linked. Lit ONLY under `cargo … --features testmatrix`.
/// The aspect tag every self-emitted row carries (matches `functional_row`).
pub const ASPECT_FUNCTIONAL: &str = "functional";
/// Push ONE functional-status row for a single real check (feature ON).
///
/// * `component` — the unit under test (becomes the row's `suite`).
/// * `check` — the specific assertion's name (becomes the row's `test_name`).
/// * `ok` — the **real** verdict.
/// * `detail` — the **measured** value.
#[cfg(feature = "testmatrix")]
#[inline]
pub fn emit(component: &str, check: &str, ok: bool, detail: &str) {
nornir_testmatrix::functional_status(component, check, ok, detail);
}
/// No-op stub when the `testmatrix` feature is OFF (release builds). Inlined and empty
/// so the call — and, with the wrap-the-call-site pattern, the detail string-building —
/// is stripped entirely.
#[cfg(not(feature = "testmatrix"))]
#[inline]
pub fn emit(_component: &str, _check: &str, _ok: bool, _detail: &str) {}
/// `assert!`-with-emit: assert on the real return value AND record the verdict as a
/// matrix row. The `assert!` is the test's actual gate; the emit is the matrix
/// observation.
#[macro_export]
macro_rules! assert_emit {
($component:expr, $check:expr, $ok:expr, $($detail:tt)+) => {{
let __ok: bool = $ok;
let __detail = ::std::format!($($detail)+);
$crate::selftest::emit($component, $check, __ok, &__detail);
::std::assert!(__ok, "{}::{} — {}", $component, $check, __detail);
}};
}
// ─── inject-and-assert wiring test (feature ON only) ─────────────────────────
#[cfg(all(test, feature = "testmatrix"))]
mod wiring {
use crate::{
current_purge_delay_ms, product_purge_delay_env, purge_delay_plan, purge_delay_plan_from,
PurgeDelayPlan, DEFAULT_PURGE_DELAY_MS, MIMALLOC_V3_DEFAULT_PURGE_DELAY_MS,
};
/// Exercise `nordisk-alloc`'s real surfaces, emit an honest row per surface, then
/// drain the process-global functional buffer and assert the rows landed — proving
/// the emit bridge is wired end to end.
#[test]
fn emits_honest_rows_for_allocator_surfaces() {
let shipped = current_purge_delay_ms();
assert_emit!(
"nordisk-alloc",
"shipped_default_is_v3s_1000ms",
shipped == MIMALLOC_V3_DEFAULT_PURGE_DELAY_MS,
"linked mimalloc reports purge_delay={shipped} ms"
);
let d = purge_delay_plan(None);
assert_emit!(
"nordisk-alloc",
"unset_installs_the_safe_default",
d == PurgeDelayPlan::Set(DEFAULT_PURGE_DELAY_MS),
"plan(None)={d:?}"
);
let typo = purge_delay_plan(Some("ten"));
assert_emit!(
"nordisk-alloc",
"a_typo_falls_forward_not_back",
typo == PurgeDelayPlan::Set(DEFAULT_PURGE_DELAY_MS),
"plan(\"ten\")={typo:?}"
);
let name = product_purge_delay_env("dwarves");
assert_emit!(
"nordisk-alloc",
"the_products_deployed_variable_survives_the_lift",
name == "DWARVES_MIMALLOC_PURGE_DELAY_MS",
"product_purge_delay_env(dwarves)={name}"
);
let shadow = purge_delay_plan_from(Some(""), Some("off"));
assert_emit!(
"nordisk-alloc",
"an_empty_product_value_does_not_shadow_the_fleet",
shadow == PurgeDelayPlan::LeaveAlone,
"plan_from(\"\", \"off\")={shadow:?}"
);
// RED surface: installing v3's own default would be a fix that fixes nothing.
crate::selftest::emit(
"nordisk-alloc",
"the_installed_value_is_not_v3s_default",
DEFAULT_PURGE_DELAY_MS != MIMALLOC_V3_DEFAULT_PURGE_DELAY_MS,
&format!("installs {DEFAULT_PURGE_DELAY_MS} ms, v3 ships {MIMALLOC_V3_DEFAULT_PURGE_DELAY_MS} ms"),
);
let rows = nornir_testmatrix::drain_functional_rows();
let has = |t: &str, pass: bool| {
rows.iter().any(|r| {
r.suite == "nordisk-alloc" && r.test_name == t && (r.status == "pass") == pass
})
};
assert!(has("shipped_default_is_v3s_1000ms", true), "rows={rows:?}");
assert!(
has("unset_installs_the_safe_default", true),
"rows={rows:?}"
);
assert!(has("a_typo_falls_forward_not_back", true), "rows={rows:?}");
assert!(
has("the_products_deployed_variable_survives_the_lift", true),
"rows={rows:?}"
);
assert!(
has("an_empty_product_value_does_not_shadow_the_fleet", true),
"rows={rows:?}"
);
assert!(
has("the_installed_value_is_not_v3s_default", true),
"rows={rows:?}"
);
assert!(
rows.iter().all(|r| r.aspect == "functional"),
"rows={rows:?}"
);
}
}