Skip to main content

nordisk_alloc/
selftest.rs

1//! # Functional-status emit bridge — `nordisk-alloc` → test matrix
2//!
3//! The single internal call site this crate uses to push a *functional* row into the
4//! [`nornir_testmatrix`] matrix, so `nornir test --features testmatrix` records whether
5//! `nordisk-alloc`'s real surfaces (the env ladder, the per-product override, the
6//! option-row fingerprint, the actual write) ACTUALLY behave — without eyeballing
7//! anything.
8//!
9//! ## Release strips everything
10//! The `testmatrix` cargo feature gates the whole bridge: with it OFF (the release
11//! default) [`emit`] is an `#[inline]` no-op and `nornir-testmatrix` is not even
12//! linked. Lit ONLY under `cargo … --features testmatrix`.
13
14/// The aspect tag every self-emitted row carries (matches `functional_row`).
15pub const ASPECT_FUNCTIONAL: &str = "functional";
16
17/// Push ONE functional-status row for a single real check (feature ON).
18///
19/// * `component` — the unit under test (becomes the row's `suite`).
20/// * `check` — the specific assertion's name (becomes the row's `test_name`).
21/// * `ok` — the **real** verdict.
22/// * `detail` — the **measured** value.
23#[cfg(feature = "testmatrix")]
24#[inline]
25pub fn emit(component: &str, check: &str, ok: bool, detail: &str) {
26    nornir_testmatrix::functional_status(component, check, ok, detail);
27}
28
29/// No-op stub when the `testmatrix` feature is OFF (release builds). Inlined and empty
30/// so the call — and, with the wrap-the-call-site pattern, the detail string-building —
31/// is stripped entirely.
32#[cfg(not(feature = "testmatrix"))]
33#[inline]
34pub fn emit(_component: &str, _check: &str, _ok: bool, _detail: &str) {}
35
36/// `assert!`-with-emit: assert on the real return value AND record the verdict as a
37/// matrix row. The `assert!` is the test's actual gate; the emit is the matrix
38/// observation.
39#[macro_export]
40macro_rules! assert_emit {
41    ($component:expr, $check:expr, $ok:expr, $($detail:tt)+) => {{
42        let __ok: bool = $ok;
43        let __detail = ::std::format!($($detail)+);
44        $crate::selftest::emit($component, $check, __ok, &__detail);
45        ::std::assert!(__ok, "{}::{} — {}", $component, $check, __detail);
46    }};
47}
48
49// ─── inject-and-assert wiring test (feature ON only) ─────────────────────────
50#[cfg(all(test, feature = "testmatrix"))]
51mod wiring {
52    use crate::{
53        current_purge_delay_ms, product_purge_delay_env, purge_delay_plan, purge_delay_plan_from,
54        PurgeDelayPlan, DEFAULT_PURGE_DELAY_MS, MIMALLOC_V3_DEFAULT_PURGE_DELAY_MS,
55    };
56
57    /// Exercise `nordisk-alloc`'s real surfaces, emit an honest row per surface, then
58    /// drain the process-global functional buffer and assert the rows landed — proving
59    /// the emit bridge is wired end to end.
60    #[test]
61    fn emits_honest_rows_for_allocator_surfaces() {
62        let shipped = current_purge_delay_ms();
63        assert_emit!(
64            "nordisk-alloc",
65            "shipped_default_is_v3s_1000ms",
66            shipped == MIMALLOC_V3_DEFAULT_PURGE_DELAY_MS,
67            "linked mimalloc reports purge_delay={shipped} ms"
68        );
69
70        let d = purge_delay_plan(None);
71        assert_emit!(
72            "nordisk-alloc",
73            "unset_installs_the_safe_default",
74            d == PurgeDelayPlan::Set(DEFAULT_PURGE_DELAY_MS),
75            "plan(None)={d:?}"
76        );
77
78        let typo = purge_delay_plan(Some("ten"));
79        assert_emit!(
80            "nordisk-alloc",
81            "a_typo_falls_forward_not_back",
82            typo == PurgeDelayPlan::Set(DEFAULT_PURGE_DELAY_MS),
83            "plan(\"ten\")={typo:?}"
84        );
85
86        let name = product_purge_delay_env("dwarves");
87        assert_emit!(
88            "nordisk-alloc",
89            "the_products_deployed_variable_survives_the_lift",
90            name == "DWARVES_MIMALLOC_PURGE_DELAY_MS",
91            "product_purge_delay_env(dwarves)={name}"
92        );
93
94        let shadow = purge_delay_plan_from(Some(""), Some("off"));
95        assert_emit!(
96            "nordisk-alloc",
97            "an_empty_product_value_does_not_shadow_the_fleet",
98            shadow == PurgeDelayPlan::LeaveAlone,
99            "plan_from(\"\", \"off\")={shadow:?}"
100        );
101
102        // RED surface: installing v3's own default would be a fix that fixes nothing.
103        crate::selftest::emit(
104            "nordisk-alloc",
105            "the_installed_value_is_not_v3s_default",
106            DEFAULT_PURGE_DELAY_MS != MIMALLOC_V3_DEFAULT_PURGE_DELAY_MS,
107            &format!("installs {DEFAULT_PURGE_DELAY_MS} ms, v3 ships {MIMALLOC_V3_DEFAULT_PURGE_DELAY_MS} ms"),
108        );
109
110        let rows = nornir_testmatrix::drain_functional_rows();
111        let has = |t: &str, pass: bool| {
112            rows.iter().any(|r| {
113                r.suite == "nordisk-alloc" && r.test_name == t && (r.status == "pass") == pass
114            })
115        };
116        assert!(has("shipped_default_is_v3s_1000ms", true), "rows={rows:?}");
117        assert!(
118            has("unset_installs_the_safe_default", true),
119            "rows={rows:?}"
120        );
121        assert!(has("a_typo_falls_forward_not_back", true), "rows={rows:?}");
122        assert!(
123            has("the_products_deployed_variable_survives_the_lift", true),
124            "rows={rows:?}"
125        );
126        assert!(
127            has("an_empty_product_value_does_not_shadow_the_fleet", true),
128            "rows={rows:?}"
129        );
130        assert!(
131            has("the_installed_value_is_not_v3s_default", true),
132            "rows={rows:?}"
133        );
134        assert!(
135            rows.iter().all(|r| r.aspect == "functional"),
136            "rows={rows:?}"
137        );
138    }
139}