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