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