mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 01:34:29 +01:00
Merge pull request #9829 from dotCooCoo/fix/previewbounds-memo-flake
Tests: hover memo counts hold plug-ins and hyphenation steady
This commit is contained in:
11 files changed
+240
-9
No files matched your search
Generated
+1
@@ -8962,6 +8962,7 @@ dependencies = [
|
||||
"vectorcraft-doc",
|
||||
"vectorcraft-geom 0.24.1",
|
||||
"vectorcraft-js",
|
||||
"vectorcraft-plugins",
|
||||
"wasmi",
|
||||
"wasmparser 0.228.0",
|
||||
"wat",
|
||||
|
||||
@@ -122,6 +122,8 @@ vectorcraft-testkit = { workspace = true }
|
||||
proptest = { workspace = true }
|
||||
# A variable font built in code (no font file is committed).
|
||||
vectorcraft-text = { workspace = true, features = ["test-fonts"] }
|
||||
# Holds the installed plug-ins steady while a test counts what a cache keeps.
|
||||
vectorcraft-plugins = { workspace = true, features = ["test-steady"] }
|
||||
# How many times an open reads a PDF, and the work its import takes.
|
||||
vectorcraft-pdf = { workspace = true, features = ["read-count"] }
|
||||
# The work `.ai` blends take to be checked and made live, and how many included EPS open checks
|
||||
|
||||
@@ -1311,6 +1311,13 @@ fn a_handle_drag_keeps_the_opposite_edge_of_the_offset_shape() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Holds the fonts, the hyphenation exceptions and the plug-ins steady on other threads: the memo
|
||||
/// also answers for them ([`vectorcraft_doc::hit::live_inputs`]), and a test that counts what it
|
||||
/// measures or checks must not count a refresh that another test's change caused.
|
||||
fn steady_live_inputs() -> (vectorcraft_text::test_fonts::SteadyFonts, vectorcraft_plugins::steady::SteadyPlugins) {
|
||||
(vectorcraft_text::test_fonts::steady_fonts(vectorcraft_text::FontDb::global()), vectorcraft_plugins::steady::steady_plugins())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_hover_checks_the_memo_as_often_however_many_objects_are_selected() {
|
||||
let checks = |objects: u32| {
|
||||
@@ -1326,6 +1333,7 @@ fn a_hover_checks_the_memo_as_often_however_many_objects_are_selected() {
|
||||
s.pointer(&PointerEvent::new(PointerKind::Move, 350.0, 350.0), v).unwrap();
|
||||
s.cursor(Point::new(350.0, 350.0), Mods::default(), v)
|
||||
};
|
||||
let _steady = steady_live_inputs();
|
||||
hover(&mut s);
|
||||
let before = s.preview_memo.checks();
|
||||
hover(&mut s);
|
||||
@@ -1350,6 +1358,7 @@ fn hovering_over_an_unchanged_document_measures_nothing_again() {
|
||||
s.cursor(Point::new(x, 150.0), Mods::default(), v)
|
||||
};
|
||||
let xs = [210.0, 150.0, 151.0, 180.0, 250.0];
|
||||
let _steady = steady_live_inputs();
|
||||
for x in xs {
|
||||
hover(&mut s, x);
|
||||
}
|
||||
|
||||
@@ -37,3 +37,10 @@ rustix = { workspace = true, features = ["std", "fs"] }
|
||||
|
||||
[dev-dependencies]
|
||||
wat = { workspace = true }
|
||||
# The `steady` gate vectorcraft-engine's tests turn on, also when this crate is tested without
|
||||
# vectorcraft-engine (`cargo xtask ci shard`).
|
||||
vectorcraft-plugins = { path = ".", features = ["test-steady"] }
|
||||
|
||||
[features]
|
||||
# `vectorcraft_plugins::steady` for other crates' tests (dev-dependencies only).
|
||||
test-steady = []
|
||||
@@ -414,6 +414,8 @@ impl Deferrals {
|
||||
}
|
||||
drop(s);
|
||||
if out.refreshed > 0 {
|
||||
#[cfg(any(test, feature = "test-steady"))]
|
||||
let _change = crate::steady::plugin_change();
|
||||
REVISION.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
out
|
||||
@@ -613,6 +615,8 @@ fn unmark(key: u64) {
|
||||
pub(crate) fn resolved(key: u64) {
|
||||
let marked = MARKED.lock().unwrap_or_else(|e| e.into_inner()).as_ref().is_some_and(|m| m.contains_key(&key));
|
||||
if marked {
|
||||
#[cfg(any(test, feature = "test-steady"))]
|
||||
let _change = crate::steady::plugin_change();
|
||||
REVISION.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
@@ -875,6 +879,25 @@ mod tests {
|
||||
assert!(!marked(), "the document closed");
|
||||
}
|
||||
|
||||
/// While a thread holds [`crate::steady::steady_plugins`], a result for a deferred key given on
|
||||
/// another thread changes the revision only once the guard is dropped.
|
||||
#[test]
|
||||
fn a_result_on_another_thread_waits_for_steady_plugins() {
|
||||
const KEY: u64 = 0xdef0_0002;
|
||||
let d = Deferrals::with_clock(stepping(1));
|
||||
d.defer(Stage::default(), "a", Reason::Budget, KEY);
|
||||
let steady = crate::steady::steady_plugins();
|
||||
let before = revision();
|
||||
std::thread::scope(|scope| {
|
||||
let resolving = scope.spawn(|| resolved(KEY));
|
||||
crate::steady::asked(resolving.thread().id(), || resolving.is_finished());
|
||||
assert_eq!(revision(), before, "the other thread's result waits for the guard");
|
||||
drop(steady);
|
||||
resolving.join().unwrap();
|
||||
});
|
||||
assert!(revision() > before, "the revision changes once the guard is dropped");
|
||||
}
|
||||
|
||||
/// Deferrals after the first view with `n` stages, one input each, keyed from `base`.
|
||||
fn many(n: u64, base: u64) -> Deferrals {
|
||||
let d = Deferrals::with_clock(stepping(1));
|
||||
|
||||
@@ -54,6 +54,8 @@ pub mod permissions;
|
||||
pub mod registry;
|
||||
mod runtime;
|
||||
pub mod safe_mode;
|
||||
#[cfg(any(test, feature = "test-steady"))]
|
||||
pub mod steady;
|
||||
pub mod storage;
|
||||
pub mod wat;
|
||||
pub mod worker;
|
||||
|
||||
@@ -79,7 +79,8 @@ static BEFORE_INSTALL_LOCK: Mutex<Option<InstallHook>> = Mutex::new(None);
|
||||
pub fn install(plugin: Plugin) -> Result<Arc<Plugin>> {
|
||||
// The revision moves while the write lock is held, so a cache that reads the new revision and
|
||||
// then the plug-ins finds the new plug-in among them. The approval is checked while it is held
|
||||
// too.
|
||||
// too. The change waits for other threads' steady guards ([`crate::steady`]) before it takes
|
||||
// the lock.
|
||||
#[cfg(test)]
|
||||
{
|
||||
let hook = BEFORE_INSTALL_LOCK.lock().unwrap_or_else(|e| e.into_inner()).clone();
|
||||
@@ -87,6 +88,8 @@ pub fn install(plugin: Plugin) -> Result<Arc<Plugin>> {
|
||||
hook(plugin.id());
|
||||
}
|
||||
}
|
||||
#[cfg(any(test, feature = "test-steady"))]
|
||||
let _change = crate::steady::plugin_change();
|
||||
let mut plugins = PLUGINS.write().unwrap_or_else(|e| e.into_inner());
|
||||
if !approved(&plugin) {
|
||||
return Err(Error::NeedsApproval(crate::permissions::names(&plugin.manifest().permissions()).join(", ")));
|
||||
@@ -111,6 +114,8 @@ fn insert(plugins: &mut BTreeMap<String, Arc<Plugin>>, mut plugin: Plugin) -> Ar
|
||||
/// runs, then the grant is recorded and the plug-in installed. `attach` runs while the plug-ins are
|
||||
/// held, so it may call [`set_storage`] but no function that reads or changes the plug-ins or grants.
|
||||
pub fn install_for_load(plugin: Plugin, changes: &LoadChanges, grant: Option<GrantToken>, attach: impl FnOnce()) -> Result<Option<Arc<Plugin>>> {
|
||||
#[cfg(any(test, feature = "test-steady"))]
|
||||
let _change = crate::steady::plugin_change();
|
||||
let mut plugins = PLUGINS.write().unwrap_or_else(|e| e.into_inner());
|
||||
if changes.superseded(plugin.id()) {
|
||||
return Ok(None);
|
||||
@@ -220,6 +225,8 @@ pub fn install_checked<E>(
|
||||
refused: impl FnOnce(Error) -> E,
|
||||
) -> std::result::Result<Arc<Plugin>, E> {
|
||||
// The same order as `install`, which reads the grants while it holds the plug-ins.
|
||||
#[cfg(any(test, feature = "test-steady"))]
|
||||
let _change = crate::steady::plugin_change();
|
||||
let mut plugins = PLUGINS.write().unwrap_or_else(|e| e.into_inner());
|
||||
let mut grants = GRANTS.write().unwrap_or_else(|e| e.into_inner());
|
||||
let id = plugin.id().to_string();
|
||||
@@ -267,6 +274,8 @@ pub fn list() -> Vec<Arc<Plugin>> {
|
||||
/// Uninstalls `id`; false if it wasn't installed.
|
||||
pub fn remove(id: &str) -> bool {
|
||||
// The revision moves while the write lock is held, as in `install`.
|
||||
#[cfg(any(test, feature = "test-steady"))]
|
||||
let _change = crate::steady::plugin_change();
|
||||
let mut plugins = PLUGINS.write().unwrap_or_else(|e| e.into_inner());
|
||||
let gone = plugins.remove(id).is_some();
|
||||
if gone {
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
//! Holding the installed plug-ins and the deferred stages' results steady while a test counts what
|
||||
//! a cache keyed on [`crate::registry::revision`] and [`crate::deferred::revision`] keeps: tests run
|
||||
//! in parallel, and a count must not include a refresh that another test's install caused.
|
||||
|
||||
/// Who holds [`SteadyPlugins`] and who changes the revisions ([`plugin_change`]), guarded by
|
||||
/// [`steadiness`] and signaled through [`STEADINESS_CHANGED`].
|
||||
struct Steadiness {
|
||||
/// Threads that hold a [`SteadyPlugins`].
|
||||
holders: usize,
|
||||
/// Changes waiting to start.
|
||||
waiting: usize,
|
||||
/// Of `waiting`, those made on a thread that holds a [`SteadyPlugins`].
|
||||
holders_waiting: usize,
|
||||
/// Whether a change is under way.
|
||||
changing: bool,
|
||||
}
|
||||
|
||||
static STEADINESS: std::sync::Mutex<Steadiness> = std::sync::Mutex::new(Steadiness { holders: 0, waiting: 0, holders_waiting: 0, changing: false });
|
||||
static STEADINESS_CHANGED: std::sync::Condvar = std::sync::Condvar::new();
|
||||
|
||||
fn steadiness() -> std::sync::MutexGuard<'static, Steadiness> {
|
||||
STEADINESS.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
}
|
||||
|
||||
/// Wait for [`STEADINESS_CHANGED`].
|
||||
fn await_steadiness(s: std::sync::MutexGuard<'static, Steadiness>) -> std::sync::MutexGuard<'static, Steadiness> {
|
||||
STEADINESS_CHANGED.wait(s).unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
}
|
||||
|
||||
std::thread_local! {
|
||||
/// How many [`SteadyPlugins`] this thread holds.
|
||||
static STEADY: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
/// Whether this thread holds the [`PluginChange`] under way.
|
||||
static CHANGING: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
|
||||
}
|
||||
|
||||
/// While held, [`crate::registry::revision`] and [`crate::deferred::revision`] change only on this
|
||||
/// thread: an install or a removal on another thread waits before it takes the plug-ins' lock, and
|
||||
/// a deferred stage that gives its result on another thread waits before the revision moves. A
|
||||
/// change on this thread waits until no other thread holds one, unless every other thread that
|
||||
/// holds one is itself waiting to make a change. The guards one thread holds count as one, until
|
||||
/// the last of them is dropped, in whatever order they are.
|
||||
pub struct SteadyPlugins {
|
||||
/// Dropped on the thread that made it.
|
||||
_thread: std::marker::PhantomData<*const ()>,
|
||||
}
|
||||
|
||||
impl Drop for SteadyPlugins {
|
||||
fn drop(&mut self) {
|
||||
let last = STEADY.with(|n| {
|
||||
n.set(n.get() - 1);
|
||||
n.get() == 0
|
||||
});
|
||||
if last {
|
||||
steadiness().holders -= 1;
|
||||
STEADINESS_CHANGED.notify_all();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// [`SteadyPlugins`]. A thread's first guard waits for a change under way, and for a waiting change
|
||||
/// that can start now. It does not wait for a change that is waiting for guards other threads hold:
|
||||
/// the thread waiting for this guard may hold one of them.
|
||||
pub fn steady_plugins() -> SteadyPlugins {
|
||||
if STEADY.with(std::cell::Cell::get) == 0 {
|
||||
let mut s = steadiness();
|
||||
let waits = |s: &Steadiness| !CHANGING.with(std::cell::Cell::get) && (s.changing || (s.waiting > 0 && s.holders == s.holders_waiting));
|
||||
while waits(&s) {
|
||||
s = await_steadiness(s);
|
||||
}
|
||||
s.holders += 1;
|
||||
}
|
||||
STEADY.with(|n| n.set(n.get() + 1));
|
||||
SteadyPlugins { _thread: std::marker::PhantomData }
|
||||
}
|
||||
|
||||
/// Held while the plug-ins or the deferred stages' results change, from before the change is made
|
||||
/// until its revision has moved ([`plugin_change`]).
|
||||
pub(crate) struct PluginChange {
|
||||
/// Made while this thread already held one.
|
||||
nested: bool,
|
||||
}
|
||||
|
||||
impl Drop for PluginChange {
|
||||
fn drop(&mut self) {
|
||||
if !self.nested {
|
||||
CHANGING.with(|c| c.set(false));
|
||||
steadiness().changing = false;
|
||||
STEADINESS_CHANGED.notify_all();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// [`PluginChange`]: waits until no other change is under way and no other thread holds a
|
||||
/// [`SteadyPlugins`] (on a thread that holds one: none that is not itself waiting to make a change).
|
||||
/// Inside a change this thread is making, it does not wait.
|
||||
pub(crate) fn plugin_change() -> PluginChange {
|
||||
if CHANGING.with(std::cell::Cell::get) {
|
||||
return PluginChange { nested: true };
|
||||
}
|
||||
let holder = STEADY.with(std::cell::Cell::get) > 0;
|
||||
let mut s = steadiness();
|
||||
#[cfg(test)]
|
||||
changes_asked().push(std::thread::current().id());
|
||||
s.waiting += 1;
|
||||
s.holders_waiting += usize::from(holder);
|
||||
let waits = |s: &Steadiness| s.changing || if holder { s.holders > s.holders_waiting } else { s.holders > 0 };
|
||||
while waits(&s) {
|
||||
s = await_steadiness(s);
|
||||
}
|
||||
s.waiting -= 1;
|
||||
s.holders_waiting -= usize::from(holder);
|
||||
s.changing = true;
|
||||
CHANGING.with(|c| c.set(true));
|
||||
PluginChange { nested: false }
|
||||
}
|
||||
|
||||
/// The threads that have asked to make a change ([`plugin_change`]).
|
||||
#[cfg(test)]
|
||||
fn changes_asked() -> std::sync::MutexGuard<'static, Vec<std::thread::ThreadId>> {
|
||||
static ASKED: std::sync::Mutex<Vec<std::thread::ThreadId>> = std::sync::Mutex::new(Vec::new());
|
||||
ASKED.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
}
|
||||
|
||||
/// Waits until the thread `id` has asked to make a change, or `finished` says it ended.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn asked(id: std::thread::ThreadId, finished: impl Fn() -> bool) {
|
||||
while !finished() && !changes_asked().contains(&id) {
|
||||
std::thread::yield_now();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// While a thread holds [`steady_plugins`], a removal on another thread waits until the last of
|
||||
/// the thread's guards is dropped, whichever was made first.
|
||||
#[test]
|
||||
fn steady_plugins_hold_back_other_threads_changes() {
|
||||
let outer = steady_plugins();
|
||||
let inner = steady_plugins();
|
||||
std::thread::scope(|scope| {
|
||||
let removing = scope.spawn(|| crate::registry::remove("steady.test.none"));
|
||||
asked(removing.thread().id(), || removing.is_finished());
|
||||
assert!(!removing.is_finished(), "the other thread's change waits for the guard");
|
||||
drop(outer);
|
||||
assert!(!removing.is_finished(), "the inner guard is still held");
|
||||
drop(inner);
|
||||
assert!(!removing.join().unwrap_or(true), "nothing was installed under the id");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -96,9 +96,13 @@ fn parse_list(source: &str) -> Exceptions {
|
||||
|
||||
/// Preferences › Hyphenation › Exceptions: comma-separated words. A word with no hyphens is never
|
||||
/// hyphenated; hyphens mark the only allowed break points (`ty-pog-ra-phy`). Matching is
|
||||
/// case-insensitive. Empty clears the list.
|
||||
/// case-insensitive. Empty clears the list. With the `test-fonts` feature, the change waits while
|
||||
/// another thread holds `test_fonts::steady_fonts`.
|
||||
pub fn set_hyphenation_exceptions(source: &str) {
|
||||
*store() = parse_list(source);
|
||||
let list = parse_list(source);
|
||||
#[cfg(any(test, feature = "test-fonts"))]
|
||||
let _change = crate::test_fonts::font_change();
|
||||
*store() = list;
|
||||
EXCEPTIONS_SET.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
|
||||
@@ -631,9 +635,11 @@ mod tests {
|
||||
}
|
||||
|
||||
/// Process-wide store used by `prefs.set` / restore. Each change makes envelopes measure type
|
||||
/// again, and caches of layouts lay type out again.
|
||||
/// again, and caches of layouts lay type out again. The guard holds back other tests' changes
|
||||
/// until the last assertion.
|
||||
#[test]
|
||||
fn set_hyphenation_exceptions_round_trips_the_source_string() {
|
||||
let _steady = crate::test_fonts::steady_fonts(&crate::FontDb::with_font_dirs(vec![]));
|
||||
let inputs = crate::layout_inputs();
|
||||
set_hyphenation_exceptions("typography, hap-pen");
|
||||
assert_eq!(hyphenation_exceptions(), "typography, hap-pen");
|
||||
|
||||
@@ -80,11 +80,12 @@ std::thread_local! {
|
||||
/// While held, a cataloged font database's fonts and its
|
||||
/// [`FontDb::generation`](crate::FontDb::generation) change only on this thread: a change on another
|
||||
/// thread (a system font loaded for a character no loaded font covers, fonts added, a rescan, a
|
||||
/// composite font set or removed) waits until it is dropped, before it publishes anything. A change
|
||||
/// on this thread waits until no other thread holds one, unless every other thread that holds one is
|
||||
/// itself waiting to change fonts. Tests hold it to count the layouts that a cache keyed on the
|
||||
/// generation keeps. The guards one thread holds count as one, until the last of them is dropped, in
|
||||
/// whatever order they are.
|
||||
/// composite font set or removed) waits until it is dropped, before it publishes anything. So does a
|
||||
/// change of the hyphenation exceptions ([`crate::set_hyphenation_exceptions`]), the other input of
|
||||
/// [`crate::layout_inputs`]. A change on this thread waits until no other thread holds one, unless
|
||||
/// every other thread that holds one is itself waiting to change fonts. Tests hold it to count the
|
||||
/// layouts that a cache keyed on the generation keeps. The guards one thread holds count as one,
|
||||
/// until the last of them is dropped, in whatever order they are.
|
||||
pub struct SteadyFonts {
|
||||
/// Dropped on the thread that made it.
|
||||
_thread: std::marker::PhantomData<*const ()>,
|
||||
|
||||
@@ -110,6 +110,24 @@ fn steady_fonts_hold_back_other_threads_font_changes() {
|
||||
});
|
||||
}
|
||||
|
||||
/// While a thread holds [`test_fonts::steady_fonts`], hyphenation exceptions set on another thread
|
||||
/// change [`crate::layout_inputs`] only once the guard is dropped.
|
||||
#[test]
|
||||
fn steady_fonts_hold_back_other_threads_hyphenation_changes() {
|
||||
let db = FontDb::with_font_dirs(vec![]);
|
||||
let steady = crate::test_fonts::steady_fonts(&db);
|
||||
let before = crate::layout_inputs();
|
||||
std::thread::scope(|scope| {
|
||||
let setting = scope.spawn(|| crate::set_hyphenation_exceptions(&crate::hyphenation_exceptions()));
|
||||
let waits = first_decision(setting.thread().id(), GateAsk::Change, || setting.is_finished());
|
||||
assert_eq!(waits, Some(true), "the other thread's change waits for the guard");
|
||||
assert_eq!(crate::layout_inputs(), before, "the other thread's change waits for the guard");
|
||||
drop(steady);
|
||||
setting.join().unwrap();
|
||||
});
|
||||
assert_ne!(crate::layout_inputs(), before, "the change is made once the guard is dropped");
|
||||
}
|
||||
|
||||
/// Whether the first `ask` that the thread `id` made of the font gate waited, once the thread has
|
||||
/// made it; `None` when `finished` says the thread ended without making it.
|
||||
fn first_decision(id: std::thread::ThreadId, ask: GateAsk, finished: impl Fn() -> bool) -> Option<bool> {
|
||||
|
||||
Reference in new issue
Block a user