From 867a6788e467a5b3fd5d09f7a5431d8c91ec05e4 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 29 Nov 2026 14:03:38 -0800 Subject: [PATCH] Tests: a held stored load waits for the test instead of a yield count HeldLoad in the plug-in tests held a stored module's load for at most 1,000,000 yield_now calls and then let it go on its own. On a busy test host that limit passed before the test released it, so the held file loaded early: a_grant_given_elsewhere_since_the_load_removed_one_is_kept and a_stored_module_without_permissions_ends_its_grant_when_it_installs failed in a full engine run and passed alone. - The hook now waits on a condition variable that release() and the drop set. A load that runs on the test's own thread is not held and sets gave_up, so a load that is no longer in the background still fails the test instead of hanging it. - stored_plugins_load_without_holding_the_plugin_changes_lock waited at most 100,000 yields for the plug-in changes lock to be free; it now waits until it is free. - canceled_loads_forget_the_grants_of_what_they_installed releases the held load before canceling, since cancel waits for a running load up to its 10 second deadline. - a_query_runs_while_the_plugins_load checks which commands wait for the loads before it runs document.info. --- crates/engine/src/tests/plugins.rs | 48 +++++++++++++++++------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/crates/engine/src/tests/plugins.rs b/crates/engine/src/tests/plugins.rs index 6cd51a5a1d..4ecff31b3b 100644 --- a/crates/engine/src/tests/plugins.rs +++ b/crates/engine/src/tests/plugins.rs @@ -2347,29 +2347,35 @@ static STORED_HOOK: std::sync::Mutex<()> = std::sync::Mutex::new(()); struct HeldLoad { _lock: std::sync::MutexGuard<'static, ()>, entered: std::sync::Arc, - go: std::sync::Arc, + go: std::sync::Arc<(std::sync::Mutex, std::sync::Condvar)>, gave_up: std::sync::Arc, } impl HeldLoad { - /// Holds the load of the file at `path`. A load the test never lets go gives up after a - /// million yields (so a test of code that waits for it ends) and says so in `gave_up`. + /// Holds the load of the file at `path` until [`HeldLoad::release`] or the drop. A load that + /// runs on the thread that made this one is not held, since nothing could let it go: it goes + /// ahead at once and says so in `gave_up`. fn new(path: std::path::PathBuf) -> HeldLoad { - use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; + use std::sync::{Arc, Condvar, Mutex}; let lock = STORED_HOOK.lock().unwrap_or_else(|e| e.into_inner()); - let (entered, go, gave_up) = (Arc::new(AtomicBool::new(false)), Arc::new(AtomicBool::new(false)), Arc::new(AtomicBool::new(false))); + let (entered, gave_up) = (Arc::new(AtomicBool::new(false)), Arc::new(AtomicBool::new(false))); + let go = Arc::new((Mutex::new(false), Condvar::new())); let (e, g, q) = (entered.clone(), go.clone(), gave_up.clone()); + let test_thread = std::thread::current().id(); *crate::store::BEFORE_STORED_LOAD.lock().unwrap_or_else(|e| e.into_inner()) = Some(Arc::new(move |p: &std::path::Path| { if p != path { return; } e.store(true, Ordering::SeqCst); - if !(0..1_000_000).any(|_| { - std::thread::yield_now(); - g.load(Ordering::SeqCst) - }) { + if std::thread::current().id() == test_thread { q.store(true, Ordering::SeqCst); + return; + } + let (open, opened) = &*g; + let mut open = open.lock().unwrap_or_else(|e| e.into_inner()); + while !*open { + open = opened.wait(open).unwrap_or_else(|e| e.into_inner()); } })); HeldLoad { _lock: lock, entered, go, gave_up } @@ -2380,7 +2386,9 @@ impl HeldLoad { } fn release(&self) { - self.go.store(true, std::sync::atomic::Ordering::SeqCst); + let (open, opened) = &*self.go; + *open.lock().unwrap_or_else(|e| e.into_inner()) = true; + opened.notify_all(); } fn gave_up(&self) -> bool { @@ -2904,6 +2912,7 @@ fn a_folder_queued_behind_the_stored_plugins_replaces_one_with_its_id() { #[test] fn a_query_runs_while_the_plugins_load() { let id = "test.engine.background.query"; + assert!(crate::waits_for_plugin_loads("plugin.list") && !crate::waits_for_plugin_loads("document.info")); let (folder, path) = store_with("plugin-background-query", id); let held = HeldLoad::new(path); let mut s = session(); @@ -2917,7 +2926,6 @@ fn a_query_runs_while_the_plugins_load() { let waited = held.gave_up() || !s.plugins_loading(); held.release(); assert!(!waited, "document.info waited for the plug-ins to load"); - assert!(crate::waits_for_plugin_loads("plugin.list") && !crate::waits_for_plugin_loads("document.info")); s.finish_plugin_loads(); assert!(vectorcraft_plugins::registry::remove(id)); } @@ -2935,18 +2943,14 @@ fn stored_plugins_load_without_holding_the_plugin_changes_lock() { while !held.entered() && !held.gave_up() { std::thread::yield_now(); } - // Other tests take the lock for a moment; the load holding it would hold it throughout (a - // tenth of the yields the held load waits before it gives up). - let free = (0..100_000).any(|_| { + // A load run on this thread isn't held, so a free lock then shows nothing. + assert!(!held.gave_up(), "the module loaded on the test's thread"); + // Other tests take the lock for a moment; the held load holding it would hold it until it is let go. + while !crate::cmd::plugin::plugin_changes_free() { std::thread::yield_now(); - crate::cmd::plugin::plugin_changes_free() - }); - // A load that gave up waiting no longer holds anything, so a free lock then shows nothing. - let gave_up = held.gave_up(); + } held.release(); s.finish_plugin_loads(); - assert!(!gave_up, "the held load gave up before the lock was looked at"); - assert!(free, "the lock was held while the module loaded"); assert!(vectorcraft_plugins::registry::remove(id)); } @@ -2974,6 +2978,10 @@ fn canceled_loads_forget_the_grants_of_what_they_installed() { std::thread::yield_now(); } assert!(vectorcraft_plugins::registry::get(first).is_some(), "the first module installed while the second loads"); + assert!(s.plugins_loading(), "the second module is still loading"); + // Canceling waits for a running load until its deadline; the second module, let go, loads + // while the cancel runs or before it, and doesn't install either way. + held.release(); s.cancel_plugin_loads(); // The file goes once it holds no grant. let listed: Vec = std::fs::read(&grants).map(|b| serde_json::from_slice(&b).unwrap()).unwrap_or_default();