nautilus_common/live/dst.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3// https://nautechsystems.io
4//
5// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6// You may not use this file except in compliance with the License.
7// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Deterministic simulation testing (DST) re-export module.
17//!
18//! Feature-switched re-exports of the tokio primitives that need deterministic
19//! behavior under simulation: `time`, `task`, `runtime`, and `signal`. Code on
20//! the DST path imports async primitives from here rather than directly from
21//! `tokio`, so that toggling the `simulation` feature switches the whole
22//! runtime in one place.
23//!
24//! # Feature-switched behavior
25//!
26//! The switch requires both the `simulation` Cargo feature (which enables the
27//! `madsim` dependency) and `RUSTFLAGS="--cfg madsim"` (which activates the
28//! simulation re-exports). Without both, all paths route to real tokio.
29//!
30//! Only four submodules switch: `time`, `task`, `runtime`, and `signal`. All
31//! other tokio modules (`sync`, `io`, `select!`, `fs`, `net`) use real tokio
32//! unconditionally. Transitive crates (`tokio-tungstenite`, `reqwest`, ...)
33//! are unaffected.
34//!
35//! # Surface
36//!
37//! The submodules re-export the following items. A compile-time probe at the
38//! bottom of this file keeps the list and the signatures of the non-generic
39//! free functions consistent across the tokio and madsim routes, and fires on
40//! `cargo build` (not only `cargo test`) so breakage surfaces upstream.
41//!
42//! - `time`: `Duration`, `Instant`, `Interval`, `MissedTickBehavior`, `Sleep`,
43//! `error` (submodule), `interval`, `interval_at`, `sleep`, `sleep_until`,
44//! `timeout`
45//! - `task`: `JoinHandle`, `spawn`, `spawn_local`, `yield_now`
46//! - `runtime`: `Builder`, `Handle`, `Runtime`
47//! - `signal`: `ctrl_c`
48//!
49//! # Related seam
50//!
51//! Monotonic time (`Instant`) goes through this module. Wall-clock time
52//! (`SystemTime` / Unix epoch) is a separate seam: see
53//! `nautilus_core::time::duration_since_unix_epoch`. Collapsing the two would
54//! lose epoch information and break order and fill timestamps.
55
56/// Deterministic time: virtual time under simulation, real time in production.
57///
58/// Under simulation (`simulation` + `cfg(madsim)`), `Instant` is
59/// `std::time::Instant` (madsim intercepts the system clock calls). `sleep`,
60/// `timeout`, `interval` advance in virtual time controlled by the
61/// deterministic scheduler.
62pub mod time {
63 pub use std::time::Duration;
64
65 #[cfg(all(feature = "simulation", madsim))]
66 pub use madsim::time::{
67 Instant, Interval, MissedTickBehavior, Sleep, error, interval, interval_at, sleep,
68 sleep_until, timeout,
69 };
70 #[cfg(not(all(feature = "simulation", madsim)))]
71 pub use tokio::time::{
72 Instant, Interval, MissedTickBehavior, Sleep, error, interval, interval_at, sleep,
73 sleep_until, timeout,
74 };
75}
76
77/// Deterministic task spawning: fixed-order scheduler under simulation.
78pub mod task {
79 #[cfg(all(feature = "simulation", madsim))]
80 pub use madsim::task::{JoinHandle, spawn, spawn_local, yield_now};
81 #[cfg(not(all(feature = "simulation", madsim)))]
82 pub use tokio::task::{JoinHandle, spawn, spawn_local, yield_now};
83}
84
85/// Deterministic runtime: single-threaded sim runtime under simulation.
86///
87/// Under simulation (`simulation` + `cfg(madsim)`),
88/// `Builder::new_multi_thread()` returns a single-threaded deterministic
89/// runtime. `worker_threads()` and `enable_all()` are no-ops.
90pub mod runtime {
91 #[cfg(not(all(feature = "simulation", madsim)))]
92 pub use tokio::runtime::{Builder, Handle, Runtime};
93
94 #[cfg(all(feature = "simulation", madsim))]
95 mod sim {
96 use std::io;
97
98 /// Tokio-compatible runtime builder that produces a madsim runtime.
99 #[derive(Debug)]
100 pub struct Builder {
101 _inner: (),
102 }
103
104 impl Builder {
105 #[must_use]
106 pub fn new_current_thread() -> Self {
107 Self { _inner: () }
108 }
109
110 #[must_use]
111 pub fn new_multi_thread() -> Self {
112 Self { _inner: () }
113 }
114
115 #[must_use]
116 pub fn worker_threads(&mut self, _val: usize) -> &mut Self {
117 self
118 }
119
120 #[must_use]
121 pub fn thread_name(&mut self, _val: impl Into<String>) -> &mut Self {
122 self
123 }
124
125 #[must_use]
126 pub fn enable_all(&mut self) -> &mut Self {
127 self
128 }
129
130 /// # Errors
131 ///
132 /// Returns an error if the runtime cannot be created.
133 pub fn build(&mut self) -> io::Result<madsim::runtime::Runtime> {
134 Ok(madsim::runtime::Runtime::new())
135 }
136 }
137
138 pub use madsim::runtime::Handle;
139 pub type Runtime = madsim::runtime::Runtime;
140 }
141
142 #[cfg(all(feature = "simulation", madsim))]
143 pub use sim::{Builder, Handle, Runtime};
144}
145
146/// Deterministic signal handling: injectable signals under simulation.
147///
148/// Under simulation (`simulation` + `cfg(madsim)`), `ctrl_c()` responds to
149/// `madsim::runtime::Handle::send_ctrl_c(node_id)` from test code.
150pub mod signal {
151 #[cfg(all(feature = "simulation", madsim))]
152 pub use madsim::signal::ctrl_c;
153 #[cfg(not(all(feature = "simulation", madsim)))]
154 pub use tokio::signal::ctrl_c;
155}
156
157/// Compile-time probe of the DST re-export surface.
158///
159/// Names every item listed in the module-level "Surface" section and pins the
160/// signatures of the non-generic free functions via function pointer coercion.
161/// Removing, renaming, cfg-gating, or changing the signature of any of them on
162/// either the tokio or madsim route causes this module to fail to compile, so
163/// both routes stay in sync. Compiled unconditionally so plain `cargo build`
164/// fires it, not only `cargo test`.
165///
166/// Generic free functions (`timeout`, `spawn`, `spawn_local`) and `async fn`s
167/// (`yield_now`, `ctrl_c`) cannot be written as concrete function pointers
168/// here, so their `use` binding only locks the name, not the signature.
169///
170/// Visibility narrowing (e.g. `pub` -> `pub(crate)`) cannot be detected from
171/// inside the defining crate; that gap is structurally outside this probe.
172///
173/// Shape check only, not a behavior test.
174#[allow(unused_imports)]
175mod surface {
176 use super::{
177 runtime::{Builder, Handle, Runtime},
178 signal::ctrl_c,
179 task::{JoinHandle, spawn, spawn_local, yield_now},
180 time::{
181 Duration, Instant, Interval, MissedTickBehavior, Sleep, error, interval, interval_at,
182 sleep, sleep_until, timeout,
183 },
184 };
185
186 const _: fn(Duration) -> Sleep = sleep;
187 const _: fn(Instant) -> Sleep = sleep_until;
188 const _: fn(Duration) -> Interval = interval;
189 const _: fn(Instant, Duration) -> Interval = interval_at;
190}
191
192#[cfg(test)]
193mod tests {
194 #[cfg(all(feature = "simulation", madsim))]
195 use nautilus_core::{datetime::NANOSECONDS_IN_SECOND, time::nanos_since_unix_epoch};
196 use rstest::rstest;
197
198 use super::*;
199
200 // -- Normal build tests (real tokio) --
201
202 #[cfg(not(all(feature = "simulation", madsim)))]
203 #[tokio::test]
204 async fn test_dst_sleep() {
205 let start = time::Instant::now();
206 time::sleep(time::Duration::from_millis(10)).await;
207 let elapsed = start.elapsed();
208 assert!(elapsed >= time::Duration::from_millis(5));
209 }
210
211 #[cfg(not(all(feature = "simulation", madsim)))]
212 #[tokio::test]
213 async fn test_dst_task_spawn() {
214 let handle = task::spawn(async { 42 });
215 let result = handle.await.unwrap();
216 assert_eq!(result, 42);
217 }
218
219 #[cfg(not(all(feature = "simulation", madsim)))]
220 #[tokio::test]
221 async fn test_real_tokio_sync_alongside_dst() {
222 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
223 tx.send(7).unwrap();
224 let result = time::timeout(time::Duration::from_secs(1), rx.recv()).await;
225 assert_eq!(result.unwrap(), Some(7));
226 }
227
228 #[cfg(not(all(feature = "simulation", madsim)))]
229 #[rstest]
230 fn test_dst_runtime_builder() {
231 let rt = runtime::Builder::new_multi_thread()
232 .worker_threads(2)
233 .enable_all()
234 .build()
235 .unwrap();
236 let result = rt.block_on(async { 99 });
237 assert_eq!(result, 99);
238 }
239
240 // -- Simulation build tests (madsim) --
241
242 #[cfg(all(feature = "simulation", madsim))]
243 #[madsim::test]
244 async fn test_dst_sleep() {
245 let start = time::Instant::now();
246 time::sleep(time::Duration::from_millis(100)).await;
247 let elapsed = start.elapsed();
248 // Virtual time: ~100ms with sub-ms scheduling epsilon.
249 // Real tokio would show 100-115ms from OS jitter.
250 assert!(elapsed >= time::Duration::from_millis(100));
251 assert!(elapsed < time::Duration::from_millis(101));
252 }
253
254 #[cfg(all(feature = "simulation", madsim))]
255 #[madsim::test]
256 async fn test_dst_task_spawn() {
257 let handle = task::spawn(async { 42 });
258 let result = handle.await.unwrap();
259 assert_eq!(result, 42);
260 }
261
262 #[cfg(all(feature = "simulation", madsim))]
263 #[madsim::test]
264 async fn test_real_tokio_sync_alongside_dst() {
265 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
266 tx.send(7).unwrap();
267 let result = time::timeout(time::Duration::from_secs(1), rx.recv()).await;
268 assert_eq!(result.unwrap(), Some(7));
269 }
270
271 #[cfg(all(feature = "simulation", madsim))]
272 #[rstest]
273 fn test_dst_runtime_builder() {
274 let rt = runtime::Builder::new_multi_thread()
275 .worker_threads(2)
276 .enable_all()
277 .build()
278 .unwrap();
279 let result = rt.block_on(async { 99 });
280 assert_eq!(result, 99);
281 }
282
283 // Pins the wall-clock seam end-to-end on the common leg: the `simulation`
284 // feature on `nautilus-common` propagates `nautilus-core/simulation`, so
285 // `nautilus_core::time::nanos_since_unix_epoch` routes through
286 // `madsim::time::TimeHandle::current().now_time()`. Sleeping for 60
287 // virtual seconds via the common DST `time::sleep` re-export must advance
288 // the wall-clock value by 60s. If either the cfg gate or the propagation
289 // regressed, the elapsed value would only reflect real wall-clock time
290 // (~0ms) and this assertion would fail.
291 #[cfg(all(feature = "simulation", madsim))]
292 #[madsim::test]
293 async fn test_dst_wall_clock_advances_with_virtual_time() {
294 let before = nanos_since_unix_epoch();
295 time::sleep(time::Duration::from_secs(60)).await;
296 let after = nanos_since_unix_epoch();
297
298 let elapsed_ns = after.saturating_sub(before);
299 let sixty_seconds_ns = 60 * NANOSECONDS_IN_SECOND;
300 assert!(
301 elapsed_ns >= sixty_seconds_ns,
302 "wall clock did not advance by full virtual sleep: elapsed={elapsed_ns}ns"
303 );
304 }
305}