mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-06 05:53:08 -05:00
Main: engine tests that share process-wide state run apart under cargo test
FreeBSD CI runs `cargo test --workspace`, which runs a crate's tests as threads of one process; Linux CI runs nextest, one process per test. Four vectorcraft-engine tests failed only in the shared process: - Events plug-ins are installed in the process-wide registry, so every installed one reacts to every session's changes and spends handlers of each session's frame budget (MAX_FRAME_HANDLERS, 4). With other tests' plug-ins installed at the same time, the plug-in a test counts on was left for a later frame and its handler never ran. a_plugin_installed_before_the_delivery_gets_no_earlier_events, a_save_reaches_the_plugin_when_its_document_closes_first and background_installs_during_a_drag_record_the_settled_baseline failed this way. The tests that install events plug-ins now hold a test lock (hold_event_plugins) while their session lives; sessions on one thread share it. - a_stale_placed_item_is_not_a_relative_object and a_stale_placed_item_is_refused_as_the_relative_object_of_move used the same fixture folder name, and each test empties its folder first, so one deleted the other's a.png. The second one now uses its own folder. `cargo test -p vectorcraft-engine --lib` on Linux reproduces all of them before this change.
This commit is contained in:
1 parent
c47b079671
commit
356e07b44c
3 files changed
+73
-4
No files matched your search
@@ -68,6 +68,53 @@ type Recipient = (String, String, u64);
|
||||
#[cfg(test)]
|
||||
std::thread_local! {
|
||||
pub(crate) static RECIPIENT_IDENTITIES: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
/// How many [`EventPluginsHeld`] this thread holds.
|
||||
static EVENT_PLUGINS_HELD: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// The thread whose test has events plug-ins installed in the process-wide registry, if any.
|
||||
#[cfg(test)]
|
||||
static EVENT_PLUGINS_OWNER: std::sync::Mutex<Option<std::thread::ThreadId>> = std::sync::Mutex::new(None);
|
||||
#[cfg(test)]
|
||||
static EVENT_PLUGINS_FREED: std::sync::Condvar = std::sync::Condvar::new();
|
||||
|
||||
/// Held by a test that installs events plug-ins in the process-wide registry. Every such plug-in
|
||||
/// reacts to every session's changes and spends handlers of every session's frame budget
|
||||
/// ([`MAX_FRAME_HANDLERS`]), so when tests run as threads of one process, another test's plug-ins
|
||||
/// would take the handlers a test counts on. A thread waits while another thread holds one; the
|
||||
/// guards one thread holds count as one, until the last of them is dropped.
|
||||
#[cfg(test)]
|
||||
pub(crate) struct EventPluginsHeld {
|
||||
/// Dropped on the thread that made it.
|
||||
_thread: std::marker::PhantomData<*const ()>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn hold_event_plugins() -> EventPluginsHeld {
|
||||
if EVENT_PLUGINS_HELD.with(std::cell::Cell::get) == 0 {
|
||||
let me = std::thread::current().id();
|
||||
let mut owner = EVENT_PLUGINS_OWNER.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
while owner.is_some_and(|t| t != me) {
|
||||
owner = EVENT_PLUGINS_FREED.wait(owner).unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
}
|
||||
*owner = Some(me);
|
||||
}
|
||||
EVENT_PLUGINS_HELD.with(|n| n.set(n.get() + 1));
|
||||
EventPluginsHeld { _thread: std::marker::PhantomData }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl Drop for EventPluginsHeld {
|
||||
fn drop(&mut self) {
|
||||
let last = EVENT_PLUGINS_HELD.with(|n| {
|
||||
n.set(n.get() - 1);
|
||||
n.get() == 0
|
||||
});
|
||||
if last {
|
||||
*EVENT_PLUGINS_OWNER.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = None;
|
||||
EVENT_PLUGINS_FREED.notify_all();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The recipient `p` is.
|
||||
@@ -836,6 +883,7 @@ mod tests {
|
||||
use serde_json::json;
|
||||
use vectorcraft_plugins::wat as tpl;
|
||||
let id = "test.events.nested";
|
||||
let _held = hold_event_plugins();
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
s.allow_plugin_grants_from_requests();
|
||||
|
||||
@@ -12,12 +12,33 @@ fn run(s: &mut Session, cmd: &str, p: Value) -> Value {
|
||||
s.execute(cmd, &p).unwrap_or_else(|e| panic!("{cmd}: {e}"))
|
||||
}
|
||||
|
||||
/// A session with a 400 × 200 document that takes `grant` from requests.
|
||||
fn session() -> Session {
|
||||
/// A session that holds [`crate::plugin_events::hold_event_plugins`] while it lives.
|
||||
struct Held {
|
||||
s: Session,
|
||||
_held: crate::plugin_events::EventPluginsHeld,
|
||||
}
|
||||
|
||||
impl std::ops::Deref for Held {
|
||||
type Target = Session;
|
||||
fn deref(&self) -> &Session {
|
||||
&self.s
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::DerefMut for Held {
|
||||
fn deref_mut(&mut self) -> &mut Session {
|
||||
&mut self.s
|
||||
}
|
||||
}
|
||||
|
||||
/// A session with a 400 × 200 document that takes `grant` from requests. The plug-ins its test
|
||||
/// installs are the only events plug-ins installed while it lives.
|
||||
fn session() -> Held {
|
||||
let held = crate::plugin_events::hold_event_plugins();
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 400, "height": 200})).unwrap();
|
||||
s.allow_plugin_grants_from_requests();
|
||||
s
|
||||
Held { s, _held: held }
|
||||
}
|
||||
|
||||
fn rect(s: &mut Session, x: f64) -> NodeId {
|
||||
|
||||
@@ -802,7 +802,7 @@ fn a_placed_items_file_binds_the_exact_id_of_the_image_placed() {
|
||||
/// it became is named by a fresh wrapper.
|
||||
#[test]
|
||||
fn a_stale_placed_item_is_refused_as_the_relative_object_of_move() {
|
||||
let base = folder("stale-relative");
|
||||
let base = folder("stale-relative-move");
|
||||
let a = base.join("main/a.png");
|
||||
std::fs::write(&a, gray_png(4, 4)).unwrap();
|
||||
let mut s = session();
|
||||
|
||||
Reference in new issue
Block a user