Visual Studio Code, rebuilt in Rust
Rust
fix(gpui): free each test's app when its gpui::test iteration ends
rokartur836ad8f1b9ed
diff --git a/native/crates/app/src/workbench.rs b/native/crates/app/src/workbench.rsindex b2d88f562eb..665f1b4be05 100644--- a/native/crates/app/src/workbench.rs+++ b/native/crates/app/src/workbench.rs@@ -2291,6 +2291,21 @@ mod tests {assert_eq!(super::header_case("Explorer: my folder", true, true), "EXPLORER: MY FOLDER");}+ /// A test's app, its window and workbench are freed when the test ends. Kept, they added about 15 MB per test until+ /// the full run passed 24 GB and was killed.+ #[gpui::test(iterations = 2)]+ fn a_test_s_app_is_freed_when_the_test_ends(cx: &mut TestAppContext) {+ thread_local!(static PREVIOUS_APP: std::cell::RefCell<std::rc::Weak<gpui::AppCell>> = Default::default());+ assert_eq!(PREVIOUS_APP.with_borrow(|app| app.strong_count()), 0, "the previous iteration's app is still alive");+ PREVIOUS_APP.set(std::rc::Rc::downgrade(&cx.app));+ let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();+ let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();+ let theme = load_theme(&[themes], "Dark Modern").unwrap();+ let folder = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../../port/fixtures/workspace");+ let host = HostSync::new(std::sync::mpsc::channel().0);+ cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), Some(Workspace::folder(folder)), Languages::new(&[]), crate::default_settings(), host, cx));+ }+#[gpui::test]fn clicking_a_folder_row_toggles_it(cx: &mut TestAppContext) {let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();diff --git a/native/vendor/wu/crates/gpui/src/app/test_context.rs b/native/vendor/wu/crates/gpui/src/app/test_context.rsindex 5750b9a1beb..c74a78ef403 100644--- a/native/vendor/wu/crates/gpui/src/app/test_context.rs+++ b/native/vendor/wu/crates/gpui/src/app/test_context.rs@@ -12,9 +12,26 @@ 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());+ }+ }+}+/// A TestAppContext is provided to tests created with `#[gpui::test]`, it provides/// an implementation of `Context` with additional methods that are useful in tests.#[derive(Clone)]@@ -28,7 +45,7 @@ pub struct TestAppContext {test_platform: Rc<TestPlatform>,text_system: Arc<TextSystem>,fn_name: Option<&'static str>,- on_quit: Rc<RefCell<Vec<Box<dyn FnOnce() + 'static>>>>,+ on_quit: Rc<QuitCallbacks>,#[doc(hidden)]pub app: Rc<AppCell>,}@@ -135,6 +152,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,@@ -144,7 +163,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/wu/crates/gpui/src/test.rs b/native/vendor/wu/crates/gpui/src/test.rsindex a93424edeab..2a0c660f711 100644--- a/native/vendor/wu/crates/gpui/src/test.rs+++ b/native/vendor/wu/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,diff --git a/port/notes.md b/port/notes.mdindex 690151aba83..8adf9fbaa97 100644--- a/port/notes.md+++ b/port/notes.md@@ -971,6 +971,10 @@ What to do (milestone 21, in order):- Each new test binary waits about 2.8 s at its first launch for Gatekeeper's scan (syspolicyd `GK performScan`); a second launch takes 0.1 s.- Waits outside rustc: `~/.port-bin/cargo` on the Studio lets 3 cargo runs build at once; the rest wait for a slot, up to 80 s in the runs above.+## Test run memory (branch test-sigkill)++- Vendored GPUI: `gpui::run_test` runs the `on_quit` callbacks of the iteration's `TestAppContext`s after its teardown (`quit_unquit_contexts`). wu's `gpui::test` teardown calls `App::quit`, a no-op on `TestPlatform`, so the `VisualTestContext` that `add_window_view` boxes (`into_mut`) and its app were never dropped: about 15 MB per window test, a full `cargo test -p app` peaked at 28.5 GB of footprint and the Studio's memory watchdog (24 GB limit) killed it with SIGKILL. Now 0.9 GB (default threads) and 0.7 GB (4 threads); `a_test_s_app_is_freed_when_the_test_ends` checks it, and GPUI's entity leak detector now runs at each test's end.+## Rounded corners (branch port-corners)- Numbers per corner: `port/results/corners.md`. The `corner_lab` test (`port/tools/corner-lab` renders `port/fixtures/corners/lab.json` with the reference's Chromium into `chromium.png`; `cargo test -p app corner_lab` draws the same boxes with GPUI and fails past 4 levels on any pixel, 9 for the `backdrop blur` case and 1 inside its corners) measures the vendored GPUI (`native/vendor/wu`) against Chromium. The rules Chromium follows, now in GPUI: