nautilus_common/
testing.rs

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