From cb2eb9269d829c16666c277813a00a2634d86a10 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 27 Nov 2026 20:59:50 +0100 Subject: [PATCH] cultcache-rs: write-conflict accessor reload_failed, Display without a space run, mover-at-same-key and gate-race tests Co-Authored-By: Claude Sonnet 5.5 --- packages/cultcache-rs/src/lib.rs | 11 +++- packages/cultcache-rs/tests/unique_index.rs | 61 ++++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/packages/cultcache-rs/src/lib.rs b/packages/cultcache-rs/src/lib.rs index c5d84a86..e9360b64 100644 --- a/packages/cultcache-rs/src/lib.rs +++ b/packages/cultcache-rs/src/lib.rs @@ -3359,12 +3359,21 @@ pub struct CultCacheWriteConflict { reload_failed: bool, } +impl CultCacheWriteConflict { + /// Whether reloading the changed records failed. When it did, the cache is refused (every write returns + /// `CultCacheLoadRefused` until a pull succeeds) and the reload error is this error's source; otherwise the cache already + /// holds the changed records and the write can be retried. + pub fn reload_failed(&self) -> bool { + self.reload_failed + } +} + impl std::fmt::Display for CultCacheWriteConflict { fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let quoted = |keys: &[String]| keys.iter().map(|key| format!("'{key}'")).collect::>().join(", "); write!( formatter, - "The backing store changed since this cache last read it, and it holds a record with a unique index, which resolve against other records; nothing was written. Records this write would have written: {}; records that changed: {}. {}", + "The backing store changed since this cache last read it, and it holds a record with a unique index, which resolve against other records; nothing was written. Records this write would have written: {}; records that changed: {}. {}", quoted(&self.record_keys), quoted(&self.changed_keys), if self.reload_failed { diff --git a/packages/cultcache-rs/tests/unique_index.rs b/packages/cultcache-rs/tests/unique_index.rs index 18d91f2f..f6c67404 100644 --- a/packages/cultcache-rs/tests/unique_index.rs +++ b/packages/cultcache-rs/tests/unique_index.rs @@ -406,7 +406,12 @@ fn a_write_onto_a_moved_store_is_a_conflict_and_the_retry_is_refused() { let conflict = conflict_of(&error); assert_eq!(conflict.record_keys, ["c"]); assert_eq!(conflict.changed_keys, ["b"]); + assert!(!conflict.reload_failed()); let text = error.to_string(); + assert_eq!( + text, + "The backing store changed since this cache last read it, and it holds a record with a unique index, which resolve against other records; nothing was written. Records this write would have written: 'c'; records that changed: 'b'. The cache reloaded them; retry the write against what the store holds now." + ); assert!(text.contains("unique index") && text.contains("'b'") && text.contains("'c'"), "{text}"); assert!(!text.contains(CANARY), "the conflict names keys, never values"); assert!(second.get::("b").unwrap().is_some(), "the moved record is loaded"); @@ -524,7 +529,8 @@ fn a_failed_reload_leaves_the_cache_refused() { twin.put("item:a", &Twin { sku: "S".into(), category: "c".into() }).unwrap(); let error = second.put("c", &unique("fresh")).unwrap_err(); - conflict_of(&error); + assert!(conflict_of(&error).reload_failed()); + assert!(error.to_string().contains("Reloading them failed")); assert!(format!("{error:#}").contains("both hold unique index"), "the reload error is the source: {error:#}"); let next = second.put("d", &unique("other")).unwrap_err(); assert!(next.downcast_ref::().is_some(), "{next:#}"); @@ -1050,3 +1056,56 @@ fn a_record_another_writer_removed_or_added_moves_the_store_and_the_keys_come_ba assert_eq!(conflict_of(&second.put("c", &unique("fresh")).unwrap_err()).changed_keys, ["b", "\u{10400}", "\u{ff21}"]); } } + +#[test] +fn a_mover_that_took_the_same_key_with_another_type_is_a_conflict_not_a_retype_refusal() { + for kind in ["single", "redb", "owned"] { + let dir = tempfile::tempdir().unwrap(); + let kind = kinds(dir.path()).into_iter().find(|(name, _)| *name == kind).unwrap().1; + let mut second = reader(&kind, dir.path()); + reader(&kind, dir.path()).put("k", &Plain { value: "first".into() }).unwrap(); + + let error = second.put("k", &unique("S")).unwrap_err(); + assert_eq!(conflict_of(&error).changed_keys, ["k"]); + assert!(second.get::("k").unwrap().is_some(), "the cache reloaded the mover's record"); + let retry = second.put("k", &unique("S")).unwrap_err(); + assert!(retry.downcast_ref::().is_none(), "the retry meets the retype rule: {retry:#}"); + } +} + +/// Two caches that both hold the current image race one value onto two keys through the gate: the store's exclusion lets +/// exactly one in, and the other is a conflict that reloaded. +#[test] +fn two_caches_racing_one_value_through_the_gate_leave_one_holder() { + for kind in ["single", "redb", "owned"] { + let dir = tempfile::tempdir().unwrap(); + let kind = kinds(dir.path()).into_iter().find(|(name, _)| *name == kind).unwrap().1; + let mut left = reader(&kind, dir.path()); + let mut right = reader(&kind, dir.path()); + for round in 0..25 { + let value = format!("V{round}"); + let gate = std::sync::Barrier::new(2); + let (won_left, won_right) = std::thread::scope(|scope| { + let run = |cache: &mut CultCache, key: String| { + let (gate, value) = (&gate, &value); + scope.spawn(move || { + gate.wait(); + match cache.put(&key, &unique(value)) { + Ok(_) => true, + Err(error) => { + conflict_of(&error); + false + } + } + }) + }; + let l = run(&mut left, format!("l{round}")); + let r = run(&mut right, format!("r{round}")); + (l.join().unwrap(), r.join().unwrap()) + }); + assert!(won_left ^ won_right, "{round}: exactly one writer lands (left {won_left}, right {won_right})"); + let held = reader(&kind, dir.path()).get_all_with_keys::().unwrap(); + assert_eq!(held.iter().filter(|(_, row)| row.sku == value).count(), 1, "round {round}"); + } + } +}