From 95190a2034d92596ae40b3619f7615ce246e2906 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Tue, 30 Sep 2025 13:07:23 +0300 Subject: [PATCH] Add a test on a `with_timeout` util function (#39187) Release Notes: - N/A Co-authored-by: Lukas Wirth --- crates/gpui/src/gpui.rs | 2 ++ crates/gpui/src/util.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/crates/gpui/src/gpui.rs b/crates/gpui/src/gpui.rs index eda10a41ab..5b277a0d43 100644 --- a/crates/gpui/src/gpui.rs +++ b/crates/gpui/src/gpui.rs @@ -65,6 +65,8 @@ #![allow(clippy::collapsible_else_if)] // False positives in platform specific code #![allow(unused_mut)] // False positives in platform specific code +extern crate self as gpui; + #[macro_use] mod action; mod app; diff --git a/crates/gpui/src/util.rs b/crates/gpui/src/util.rs index 3704784a95..bf8f3fee0e 100644 --- a/crates/gpui/src/util.rs +++ b/crates/gpui/src/util.rs @@ -140,3 +140,35 @@ pub(crate) fn atomic_incr_if_not_zero(counter: &AtomicUsize) -> usize { } } } + +#[cfg(test)] +mod tests { + use crate::TestAppContext; + + use super::*; + + #[gpui::test] + async fn test_with_timeout(cx: &mut TestAppContext) { + Task::ready(()) + .with_timeout(Duration::from_secs(1), &cx.executor()) + .await + .expect("Timeout should be noop"); + + let long_duration = Duration::from_secs(6000); + let short_duration = Duration::from_secs(1); + cx.executor() + .timer(long_duration) + .with_timeout(short_duration, &cx.executor()) + .await + .expect_err("timeout should have triggered"); + + let fut = cx + .executor() + .timer(long_duration) + .with_timeout(short_duration, &cx.executor()); + cx.executor().advance_clock(short_duration * 2); + futures::FutureExt::now_or_never(fut) + .unwrap_or_else(|| panic!("timeout should have triggered")) + .expect_err("timeout"); + } +}