Skip to main content

ulo_conformance_support/
lib.rs

1//! The harness pieces the conformance suites share (integrations DESIGN ยง9, I11): the runtime half
2//! a suite's scenarios run on, the budget a scenario waits within, startup failures written to
3//! files, the [`Slots`] that bound how many scenarios hold an environment at once, and the TCP
4//! relay. `ulo-rpc-conformance` and `ulo-db-conformance` depend on this crate and re-export what a
5//! link or host author names, so that author depends on the suite alone; a crate writing a suite
6//! depends on this one.
7//!
8//! The crate names no runtime at default features. A suite's [`Harness`] is its runtime half:
9//! the app's runtime, which every server and client in a scenario runs on and every wait and task
10//! of the scenario's own goes through, and a `block_on` each scenario runs inside, beside a
11//! one-thread `block_on` for the scenario that shows a connect does not stall its thread. `OnTokio`,
12//! behind the `tokio` feature, is the harness of every suite on tokio; `OnSmol`, behind `smol`,
13//! runs the scenarios on an `async-executor` `Executor`. The [`relay`] runs on threads of its own,
14//! on `std` sockets, so it carries a client on either.
15//!
16//! A host short of memory bounds how many scenarios hold an environment at once by setting
17//! [`PARALLEL_VAR`], `ULO_CONFORMANCE_PARALLEL`, to a positive integer: the bound is the smaller of
18//! it and the suite's own declared bound, or it alone when the suite declares none. Unset or empty,
19//! it bounds nothing, and libtest's `--test-threads` still applies on top of either.
20//!
21//! ```text
22//! ULO_CONFORMANCE_PARALLEL=6 cargo test -p ulo-rpc-rabbitmq --features integration --test conformance
23//! ```
24
25use std::error::Error;
26use std::future::Future;
27use std::time::Duration;
28
29use ulo::Runtime;
30
31mod failures;
32pub mod relay;
33mod slots;
34#[cfg(feature = "smol")]
35mod smol_harness;
36#[cfg(feature = "smol")]
37pub mod starvation;
38#[cfg(feature = "tokio")]
39mod tokio_harness;
40
41pub use failures::{failures_dir, startup_failed};
42pub use slots::{Slot, Slots};
43/// Defined in core, where a user's tests reach it; kept here so the suites' existing paths hold.
44pub use ulo::testing::ScriptedTimer;
45#[cfg(feature = "smol")]
46pub use smol_harness::OnSmol;
47#[cfg(feature = "tokio")]
48pub use tokio_harness::OnTokio;
49
50/// The environment variable through which a host bounds how many scenarios of one suite hold an
51/// environment at once. A value that is not a positive integer fails every scenario through
52/// [`startup_failed!`].
53pub const PARALLEL_VAR: &str = "ULO_CONFORMANCE_PARALLEL";
54
55/// `error` and every source under it, joined by `: `, for a failure message: a scenario that fails
56/// while its environment starts says what failed, the broker's container, the server's bind or the
57/// client's connect, and why. A source whose text the message already carries is not repeated.
58pub fn report(error: &(dyn Error + 'static)) -> String {
59    let mut text = error.to_string();
60    let mut source = error.source();
61    while let Some(cause) = source {
62        let line = cause.to_string();
63        if !text.contains(&line) {
64            text.push_str(": ");
65            text.push_str(&line);
66        }
67        source = cause.source();
68    }
69    text
70}
71
72/// Skips a scenario whose host or link declares the capability it needs absent: writes one line
73/// naming the scenario, the capability and the declared reason to the process's standard error,
74/// which libtest does not capture, so the line is in the run's output whether the test passes or
75/// fails. The scenario then returns, and libtest counts it as passed.
76pub fn skipped(scenario: &str, capability: &str, reason: &str) {
77    use std::io::Write;
78    // One write, so the line is not split by another test thread's output.
79    let line = format!("skipped `{scenario}`: declared without `{capability}`: {reason}\n");
80    let _ = std::io::stderr().write_all(line.as_bytes());
81}
82
83/// Fails a scenario whose environment did not start, as `panic!` with the same arguments would,
84/// after writing the message to a file of its own under [`failures_dir`]; the panic names the file.
85/// Every startup path in a suite fails through it, and a suite's environment implementation's own
86/// startup should too.
87#[macro_export]
88macro_rules! startup_failed {
89    ($($arg:tt)+) => {
90        $crate::startup_failed(::std::format!($($arg)+))
91    };
92}
93
94/// The runtime a suite's scenarios run on, as the suite drives it: the app's runtime and the
95/// executor each scenario is run to its end on.
96pub trait Harness: Send + Sync + 'static {
97    /// The app's runtime: every server and client a scenario starts runs on it, and the scenario's
98    /// own sleeps, timeouts and tasks go through it.
99    type Runtime: Runtime;
100
101    /// A runtime value, taken wherever a scenario needs one: on the thread `block_on` runs and on
102    /// every thread running the tasks the runtime spawns while it does.
103    fn runtime() -> Self::Runtime;
104
105    /// Runs `fut` to its end from a plain `#[test]` thread, on an executor that runs the tasks the
106    /// runtime spawns while `fut` waits. Each scenario calls it once.
107    fn block_on<F: Future>(fut: F) -> F::Output;
108
109    /// [`block_on`](Self::block_on) on the calling thread alone: `fut` and every task the runtime
110    /// spawns while it waits are polled there and nowhere else, so work that blocks the thread
111    /// polling it stalls every one of them. The scenario that shows a link's connect and bind do
112    /// not stall that thread runs through it, and fails on a harness that polls a task elsewhere.
113    fn block_on_one_thread<F: Future>(fut: F) -> F::Output;
114}
115
116/// How long a scenario waits on its environment.
117#[non_exhaustive]
118#[derive(Clone, Copy, Debug, PartialEq, Eq)]
119pub struct Budget {
120    /// From `start` until the server answers its first call.
121    pub boot: Duration,
122    /// For a one-way effect to show: an event handled, a subscription in place.
123    pub settle: Duration,
124    /// From `disrupt` until calls succeed again.
125    pub recovery: Duration,
126    /// The longest the scenario's thread may go without polling a task that is due, while a link
127    /// binds and connects on a harness's one thread: 50 ms unless [`with_stall`](Self::with_stall)
128    /// sets another. [`new`](Self::new) leaves it at 50 ms whatever the other three are: a broker
129    /// slow to answer is waited on, not blocked on, so it gives the link no reason to stall the
130    /// thread, and a librdkafka handle's drop, about 100 ms, has to fail it on the slowest broker.
131    pub stall: Duration,
132}
133
134/// [`Budget::stall`] unless a broker sets another.
135const STALL: Duration = Duration::from_millis(50);
136
137impl Budget {
138    pub const fn new(boot: Duration, settle: Duration, recovery: Duration) -> Self {
139        Budget { boot, settle, recovery, stall: STALL }
140    }
141
142    /// The budget with its [`stall`](Self::stall) set, for a host whose thread is descheduled for
143    /// longer than 50 ms while it is busy.
144    pub const fn with_stall(self, stall: Duration) -> Self {
145        Budget { stall, ..self }
146    }
147}
148
149impl Default for Budget {
150    fn default() -> Self {
151        Budget::new(Duration::from_secs(5), Duration::from_millis(500), Duration::from_secs(10))
152    }
153}