Merge pull request #10136 from dotCooCoo/fix/plugin-test-held-load-handshake

Tests: a held stored load waits for the test instead of a yield count
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-29 18:21:32 -08:00
commit c767960328
1 file changed
+28 -20
+28 -20
View File
@@ -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<std::sync::atomic::AtomicBool>,
go: std::sync::Arc<std::sync::atomic::AtomicBool>,
go: std::sync::Arc<(std::sync::Mutex<bool>, std::sync::Condvar)>,
gave_up: std::sync::Arc<std::sync::atomic::AtomicBool>,
}
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<Value> = std::fs::read(&grants).map(|b| serde_json::from_slice(&b).unwrap()).unwrap_or_default();