Skip to main content

nautilus_common/
testing.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//! Common test related helper functions.
17
18#[cfg(feature = "live")]
19use std::future::Future;
20use std::{
21    thread,
22    time::{Duration, Instant},
23};
24
25use nautilus_core::UUID4;
26use nautilus_model::{identifiers::TraderId, stubs::TestDefault};
27
28use crate::logging::{
29    init_logging,
30    logger::{LogGuard, LoggerConfig},
31    writer::FileWriterConfig,
32};
33
34/// # Errors
35///
36/// Returns an error if initializing the logger fails.
37pub fn init_logger_for_testing(stdout_level: Option<log::LevelFilter>) -> anyhow::Result<LogGuard> {
38    let config = LoggerConfig {
39        stdout_level: stdout_level.unwrap_or(log::LevelFilter::Trace),
40        ..Default::default()
41    };
42    init_logging(
43        TraderId::test_default(),
44        UUID4::new(),
45        config,
46        FileWriterConfig::default(),
47    )
48}
49
50/// Repeatedly evaluates a condition with a delay until it becomes true or a timeout occurs.
51///
52/// # Panics
53///
54/// This function panics if the timeout duration is exceeded without the condition being met.
55///
56/// # Examples
57///
58/// ```
59/// use std::time::Duration;
60/// use std::thread;
61/// use nautilus_common::testing::wait_until;
62///
63/// let start_time = std::time::Instant::now();
64/// let timeout = Duration::from_secs(5);
65///
66/// wait_until(|| {
67///     if start_time.elapsed().as_secs() > 2 {
68///         true
69///     } else {
70///         false
71///     }
72/// }, timeout);
73/// ```
74///
75/// In the above example, the `wait_until` function will block for at least 2 seconds, as that's how long
76/// it takes for the condition to be met. If the condition was not met within 5 seconds, it would panic.
77pub fn wait_until<F>(mut condition: F, timeout: Duration)
78where
79    F: FnMut() -> bool,
80{
81    let start_time = Instant::now(); // dst-ok: test helper timer; uses real time by design
82
83    loop {
84        if condition() {
85            break;
86        }
87
88        assert!(
89            start_time.elapsed() <= timeout,
90            "Timeout waiting for condition after {:.1}s (limit {:.1}s)",
91            start_time.elapsed().as_secs_f64(),
92            timeout.as_secs_f64(),
93        );
94
95        thread::sleep(Duration::from_millis(100));
96    }
97}
98
99/// # Panics
100///
101/// Panics if the timeout duration is exceeded without the condition being met.
102#[cfg(feature = "live")]
103pub async fn wait_until_async<F, Fut>(mut condition: F, timeout: Duration)
104where
105    F: FnMut() -> Fut,
106    Fut: Future<Output = bool>,
107{
108    let start_time = Instant::now(); // dst-ok: test helper timer; uses real time by design
109
110    loop {
111        if condition().await {
112            break;
113        }
114
115        assert!(
116            start_time.elapsed() <= timeout,
117            "Timeout waiting for condition after {:.1}s (limit {:.1}s)",
118            start_time.elapsed().as_secs_f64(),
119            timeout.as_secs_f64(),
120        );
121
122        tokio::time::sleep(Duration::from_millis(100)).await;
123    }
124}