Skip to content

Visual Studio Code, rebuilt in Rust

Rust

fix(gpui): free each test's app when its gpui::test iteration ends

rokarturb394fcafc1d8
diff --git a/native/vendor/gpui-fast/crates/gpui/src/app/test_context.rs b/native/vendor/gpui-fast/crates/gpui/src/app/test_context.rs
index 83a96f8405c..a1f6ff82c14 100644
--- a/native/vendor/gpui-fast/crates/gpui/src/app/test_context.rs
+++ b/native/vendor/gpui-fast/crates/gpui/src/app/test_context.rs
@@ -13,9 +13,32 @@ use anyhow::{anyhow, bail};
use futures::{Stream, StreamExt, channel::oneshot};
use std::{
- cell::RefCell, future::Future, ops::Deref, path::PathBuf, rc::Rc, sync::Arc, time::Duration,
+ cell::RefCell,
+ future::Future,
+ ops::Deref,
+ path::PathBuf,
+ rc::{Rc, Weak},
+ sync::Arc,
+ time::Duration,
};
+type QuitCallbacks = RefCell<Vec<Box<dyn FnOnce() + 'static>>>;
+
+thread_local! {
+ static UNQUIT_CONTEXTS: RefCell<Vec<Weak<QuitCallbacks>>> = RefCell::default();
+}
+
+/// Runs the `on_quit` callbacks of every context built on this thread since the last call. The `gpui::test` teardown
+/// calls `App::quit`, not [`TestAppContext::quit`], so without this each `into_mut` context, and its app, outlives its test.
+pub(crate) fn quit_unquit_contexts() {
+ for on_quit in UNQUIT_CONTEXTS.take() {
+ if let Some(on_quit) = on_quit.upgrade() {
+ let callbacks = on_quit.take();
+ callbacks.into_iter().for_each(|f| f());
+ }
+ }
+}
+
pub(crate) fn test_window(app: &AppCell, window: AnyWindowHandle) -> TestWindow {
app.borrow_mut()
.windows
@@ -42,7 +65,7 @@ pub struct TestAppContext {
pub(crate) test_platform: Rc<TestPlatform>,
pub(crate) text_system: Arc<TextSystem>,
pub(crate) fn_name: Option<&'static str>,
- pub(crate) on_quit: Rc<RefCell<Vec<Box<dyn FnOnce() + 'static>>>>,
+ pub(crate) on_quit: Rc<QuitCallbacks>,
#[doc(hidden)]
pub app: Rc<AppCell>,
}
@@ -193,6 +216,8 @@ impl TestAppContext {
let app = App::new_app(platform.clone(), asset_source, http_client);
app.borrow_mut().mode = GpuiMode::test();
+ let on_quit = Rc::new(QuitCallbacks::default());
+ UNQUIT_CONTEXTS.with_borrow_mut(|contexts| contexts.push(Rc::downgrade(&on_quit)));
Self {
app,
@@ -202,7 +227,7 @@ impl TestAppContext {
test_platform: platform,
text_system,
fn_name,
- on_quit: Rc::new(RefCell::new(Vec::default())),
+ on_quit,
}
}
diff --git a/native/vendor/gpui-fast/crates/gpui/src/test.rs b/native/vendor/gpui-fast/crates/gpui/src/test.rs
index c1c8e666aec..58ef1731c86 100644
--- a/native/vendor/gpui-fast/crates/gpui/src/test.rs
+++ b/native/vendor/gpui-fast/crates/gpui/src/test.rs
@@ -113,6 +113,7 @@ pub fn run_test(
test_fn(dispatcher, seed);
scheduler.end_test();
});
+ crate::app::quit_unquit_contexts();
match result {
Ok(_) => break,