Skip to content

Commit 59b8a38

Browse files
authored
Add wait_until_async in Postgres cache Rust tests (#1710)
1 parent 75be86d commit 59b8a38

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

nautilus_core/common/src/testing.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
//! Common test related helper functions.
1717
1818
use std::{
19+
future::Future,
1920
thread,
2021
time::{Duration, Instant},
2122
};
@@ -71,3 +72,24 @@ where
7172
thread::sleep(Duration::from_millis(100));
7273
}
7374
}
75+
76+
pub async fn wait_until_async<F, Fut>(mut condition: F, timeout: Duration)
77+
where
78+
F: FnMut() -> Fut,
79+
Fut: Future<Output = bool>,
80+
{
81+
let start_time = Instant::now();
82+
83+
loop {
84+
if condition().await {
85+
break;
86+
}
87+
88+
assert!(
89+
start_time.elapsed() <= timeout,
90+
"Timeout waiting for condition"
91+
);
92+
93+
tokio::time::sleep(Duration::from_millis(100)).await;
94+
}
95+
}

nautilus_core/infrastructure/tests/test_cache_database_postgres.rs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ pub async fn get_pg_cache_database() -> anyhow::Result<PostgresCacheDatabase> {
6161
mod tests {
6262
use std::time::Duration;
6363

64+
use nautilus_common::testing::wait_until_async;
6465
use nautilus_core::equality::entirely_equal;
6566
use nautilus_model::{
66-
enums::{CurrencyType, OrderSide},
67+
enums::{CurrencyType, OrderSide, OrderStatus},
6768
identifiers::{
6869
stubs::account_id, AccountId, ClientOrderId, InstrumentId, TradeId, VenueOrderId,
6970
},
@@ -91,7 +92,14 @@ mod tests {
9192
.add(String::from("test_id"), test_id_value.clone())
9293
.await
9394
.unwrap();
94-
tokio::time::sleep(Duration::from_secs(2)).await;
95+
wait_until_async(
96+
|| async {
97+
let result = pg_cache.load().await.unwrap();
98+
result.keys().len() > 0
99+
},
100+
Duration::from_secs(2),
101+
)
102+
.await;
95103
let result = pg_cache.load().await.unwrap();
96104
assert_eq!(result.keys().len(), 1);
97105
assert_eq!(
@@ -149,7 +157,15 @@ mod tests {
149157
.add_instrument(InstrumentAny::OptionsContract(options_contract))
150158
.await
151159
.unwrap();
152-
tokio::time::sleep(Duration::from_secs(2)).await;
160+
// Wait for the cache to update
161+
wait_until_async(
162+
|| async {
163+
let currencies = pg_cache.load_currencies().await.unwrap();
164+
currencies.len() >= 4
165+
},
166+
Duration::from_secs(2),
167+
)
168+
.await;
153169
// Check that currency list is correct
154170
let currencies = pg_cache.load_currencies().await.unwrap();
155171
assert_eq!(currencies.len(), 4);
@@ -261,7 +277,22 @@ mod tests {
261277
);
262278
pg_cache.add_order(market_order.clone()).await.unwrap();
263279
pg_cache.add_order(limit_order.clone()).await.unwrap();
264-
tokio::time::sleep(Duration::from_secs(2)).await;
280+
wait_until_async(
281+
|| async {
282+
pg_cache
283+
.load_order(&market_order.client_order_id())
284+
.await
285+
.unwrap()
286+
.is_some()
287+
&& pg_cache
288+
.load_order(&limit_order.client_order_id())
289+
.await
290+
.unwrap()
291+
.is_some()
292+
},
293+
Duration::from_secs(2),
294+
)
295+
.await;
265296
let market_order_result = pg_cache
266297
.load_order(&market_order.client_order_id())
267298
.await
@@ -320,7 +351,17 @@ mod tests {
320351
);
321352
market_order.apply(filled).unwrap();
322353
pg_cache.update_order(market_order.clone()).await.unwrap();
323-
tokio::time::sleep(Duration::from_secs(2)).await;
354+
wait_until_async(
355+
|| async {
356+
let result = pg_cache
357+
.load_order(&market_order.client_order_id())
358+
.await
359+
.unwrap();
360+
result.is_some() && result.unwrap().status() == OrderStatus::Filled
361+
},
362+
Duration::from_secs(2),
363+
)
364+
.await;
324365
// Assert
325366
let market_order_result = pg_cache
326367
.load_order(&market_order.client_order_id())

0 commit comments

Comments
 (0)