nautilus_common/
testing.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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;
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 mut config = LoggerConfig::default();
39    config.stdout_level = stdout_level.unwrap_or(log::LevelFilter::Trace);
40    init_logging(
41        TraderId::default(),
42        UUID4::new(),
43        config,
44        FileWriterConfig::default(),
45    )
46}
47
48/// Repeatedly evaluates a condition with a delay until it becomes true or a timeout occurs.
49///
50/// # Panics
51///
52/// This function panics if the timeout duration is exceeded without the condition being met.
53///
54/// # Examples
55///
56/// ```
57/// use std::time::Duration;
58/// use std::thread;
59/// use nautilus_common::testing::wait_until;
60///
61/// let start_time = std::time::Instant::now();
62/// let timeout = Duration::from_secs(5);
63///
64/// wait_until(|| {
65///     if start_time.elapsed().as_secs() > 2 {
66///         true
67///     } else {
68///         false
69///     }
70/// }, timeout);
71/// ```
72///
73/// In the above example, the `wait_until` function will block for at least 2 seconds, as that's how long
74/// it takes for the condition to be met. If the condition was not met within 5 seconds, it would panic.
75pub fn wait_until<F>(mut condition: F, timeout: Duration)
76where
77    F: FnMut() -> bool,
78{
79    let start_time = Instant::now();
80
81    loop {
82        if condition() {
83            break;
84        }
85
86        assert!(
87            start_time.elapsed() <= timeout,
88            "Timeout waiting for condition"
89        );
90
91        thread::sleep(Duration::from_millis(100));
92    }
93}
94
95/// # Panics
96///
97/// Panics if the timeout duration is exceeded without the condition being met.
98#[cfg(feature = "live")]
99pub async fn wait_until_async<F, Fut>(mut condition: F, timeout: Duration)
100where
101    F: FnMut() -> Fut,
102    Fut: Future<Output = bool>,
103{
104    let start_time = Instant::now();
105
106    loop {
107        if condition().await {
108            break;
109        }
110
111        assert!(
112            start_time.elapsed() <= timeout,
113            "Timeout waiting for condition"
114        );
115
116        tokio::time::sleep(Duration::from_millis(100)).await;
117    }
118}