Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

test(engine): make the define-brush mask allocation fail on its own, fail-after-one (#212)

wwewtechc6e4d8a94528
diff --git a/crates/engine/src/allocation.rs b/crates/engine/src/allocation.rs
index 7c29ecc8e..fc59a1373 100644
--- a/crates/engine/src/allocation.rs
+++ b/crates/engine/src/allocation.rs
@@ -5,6 +5,7 @@ use crate::{EngineError, Result};
#[cfg(test)]
thread_local! {
static FAIL_NEXT: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
+ static FAIL_AFTER: std::cell::Cell<Option<usize>> = const { std::cell::Cell::new(None) };
}
/// Reserve and initialize a buffer without invoking the allocator's abort-on-OOM path.
@@ -18,7 +19,7 @@ pub(crate) fn filled<T: Clone>(len: usize, value: T, purpose: &str) -> Result<Ve
pub(crate) fn capacity<T>(len: usize, purpose: &str) -> Result<Vec<T>> {
let bytes = len.checked_mul(std::mem::size_of::<T>()).ok_or_else(|| error(purpose, "requested size overflowed"))?;
#[cfg(test)]
- if FAIL_NEXT.with(|flag| flag.replace(false)) {
+ if failpoint_hits() {
return Err(error(purpose, "not enough memory"));
}
let mut out = Vec::new();
@@ -30,7 +31,7 @@ pub(crate) fn capacity<T>(len: usize, purpose: &str) -> Result<Vec<T>> {
/// The test-only failpoint lets command tests prove their transaction boundary.
pub(crate) fn checkpoint(purpose: &str) -> Result<()> {
#[cfg(test)]
- if FAIL_NEXT.with(|flag| flag.replace(false)) {
+ if failpoint_hits() {
return Err(error(purpose, "not enough memory"));
}
let _ = purpose;
@@ -57,6 +58,32 @@ pub(crate) fn fail_next_for_test() {
FAIL_NEXT.with(|flag| flag.set(true));
}
+/// Fail the fallible allocation or checkpoint after `skip` clean attempts, then stop.
+/// `fail_after_for_test(0)` is equivalent to [`fail_next_for_test`].
+#[cfg(test)]
+pub(crate) fn fail_after_for_test(skip: usize) {
+ FAIL_AFTER.with(|flag| flag.set(Some(skip)));
+}
+
+/// Whether a test failpoint trips here: the single-shot flag, or the counted flag
+/// once its `skip` attempts have passed cleanly earlier.
+#[cfg(test)]
+fn failpoint_hits() -> bool {
+ let one_shot = FAIL_NEXT.with(|flag| flag.replace(false));
+ let after = FAIL_AFTER.with(|flag| match flag.get() {
+ Some(0) => {
+ flag.set(None);
+ true
+ }
+ Some(rest) => {
+ flag.set(Some(rest - 1));
+ false
+ }
+ None => false,
+ });
+ one_shot || after
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -67,4 +94,14 @@ mod tests {
let result = filled::<u8>(16, 0, "test image");
assert!(result.as_ref().is_err_and(|e| e.to_string().contains("not enough memory for test image")));
}
+
+ #[test]
+ fn fail_after_trips_only_on_the_counted_attempt() {
+ fail_after_for_test(2);
+ assert!(filled::<u8>(4, 0, "one").is_ok(), "the counted failpoint lets the first attempts pass cleanly");
+ assert!(filled::<u8>(4, 0, "two").is_ok());
+ assert!(filled::<u8>(4, 0, "three").is_err_and(|e| e.to_string().contains("not enough memory for three")));
+ // The flag is consumed by the trip: later attempts are clean again.
+ assert!(filled::<u8>(4, 0, "four").is_ok());
+ }
}
diff --git a/crates/engine/src/brush_cmds/tests.rs b/crates/engine/src/brush_cmds/tests.rs
index 5977603f9..90977f871 100644
--- a/crates/engine/src/brush_cmds/tests.rs
+++ b/crates/engine/src/brush_cmds/tests.rs
@@ -462,6 +462,29 @@ fn define_brush_from_selection_survives_allocation_failure() {
assert_eq!(s.active().unwrap().history.past_len(), history_before, "no history step was recorded");
}
+#[test]
+fn define_brush_from_selection_survives_failure_of_the_second_allocation() {
+ // Review of #5469: fail_next_for_test trips on the first buffer, so the second fallible
+ // call (the selection-mask read) did not have its own regression. Count the tip buffer as
+ // a clean attempt and fail the mask read instead; both report the same command purpose,
+ // and the command must still bow out before touching anything.
+ let mut s = session(40, 40);
+ s.execute("select.rect", json!({"x": 5, "y": 5, "width": 20, "height": 20})).unwrap();
+ let pixels_before = surface(&s);
+ let brush_before = s.tools.brush.clone();
+ let presets_before = s.tools.presets.clone();
+ let history_before = s.active().unwrap().history.past_len();
+ crate::allocation::fail_after_for_test(1);
+ let result = s.execute("brush.defineFromSelection", json!({"name": "Blob"}));
+ let _ = crate::allocation::capacity::<u8>(0, "reset failpoint");
+ assert!(result.is_err(), "an allocation failure must be reported, not panic");
+ assert!(result.as_ref().is_err_and(|e| e.to_string().contains("not enough memory for defining a brush from the selection")));
+ assert_eq!(s.tools.brush, brush_before, "the current brush is unchanged");
+ assert_eq!(s.tools.presets, presets_before, "the preset list is unchanged");
+ assert_eq!(s.active().unwrap().history.past_len(), history_before, "no history step was recorded");
+ assert!(same_pixels(&pixels_before, &surface(&s), s.active().unwrap().doc.bounds()), "the document pixels are unchanged");
+}
+
#[test]
fn works_on_cmyk_and_16_bit() {
for (mode, depth) in [("cmyk", 8), ("rgb", 16), ("grayscale", 32)] {