Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Add a test on a `with_timeout` util function (#39187)

Kirill Bulatov95190a2034d9
diff --git a/crates/gpui/src/gpui.rs b/crates/gpui/src/gpui.rs
index eda10a41abc..5b277a0d43f 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 3704784a954..bf8f3fee0e8 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");
+ }
+}