diff options
| author | tv <tv@krebsco.de> | 2026-11-30 14:55:03 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-30 14:55:03 +0000 |
| commit | 8017b0e69f0350c2a35629ae71d511ddf7a3aa9c (patch) | |
| tree | 494d282a79985a19d2194717fa7a7e7a4bba52e1 /src/simtest | |
| parent | 8e97bcfa65e92da21df912cc5e19cf18027b82b8 (diff) | |
A node of a swarm holding a store, on a set of events (--store-document)
With store-root or store-id, store-document names the file of the
store's own document, a set of events: the node holds its store there
and nothing in the Automerge document, syncs with nodes holding a set
and no others, and goes on from the file when started again. Without a
file yet it begins from the root (StoreStart::set); joining by the ID
alone needs a file. What a checkpoint forgot is kept for
store-keeps-forgotten (30 days). The file stands for store-file, and no
checkpoint is pinned.
The set is written to its file where it changed, every five seconds
with the store's worker and as the node stops (Holding::save), what a
checkpoint forgot longer ago let go of first.
Simulations: a node behind catches up; a node goes on from its file.
Diffstat (limited to 'src/simtest')
| -rw-r--r-- | src/simtest/holding.rs | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/src/simtest/holding.rs b/src/simtest/holding.rs index 4a8098b..6da7a90 100644 --- a/src/simtest/holding.rs +++ b/src/simtest/holding.rs @@ -1806,3 +1806,125 @@ async fn a_swarm_of_sets_passes_entries_and_events_on() { assert!(node.doc.read().await.heads().is_empty()); } } + +/// Starts `name`, of `party`, on a set of events `set`, kept in the file at `path` where +/// given: the first of them with no seed, the others seeded by `a`. +fn start_on_a_set( + sim: &Sim, + name: &str, + party: &str, + id: Id, + set: crate::federation::store::set::Set<Federation>, + path: Option<std::path::PathBuf>, + restart: bool, +) -> AppState { + let seeds: Vec<&str> = if name == "a" { Vec::new() } else { vec!["a"] }; + let party = party.to_owned(); + let adjust = move |config: &mut NodeConfig| { + config.store = Some(id); + config.holding = holding(&party).carrying_if(Some(set), path, Arc::new(Files::default())); + }; + if restart { + sim.restart_with(name, &seeds, Start::FromDocument(Origin::Root), adjust) + } else { + sim.start_with(name, &seeds, Start::FromDocument(Origin::Root), adjust) + } +} + +/// A set of events begun from `root`. +fn a_set(root: &[u8]) -> crate::federation::store::set::Set<Federation> { + crate::federation::store::set::Set::new( + Store::<Federation>::new(root.to_vec(), Vec::new()).expect("a root"), + Duration::from_secs(600), + ) +} + +/// a and b hold events; c, of another party, starts later from the root alone, and +/// catches up: it comes to hold what they hold, and to stand as they do. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn a_node_of_a_set_that_is_behind_catches_up() { + let sim = Sim::with_identity(92); + let (tv, lass, _) = three(); + let (_, root, id) = federation(&[(&tv, &["a", "b"]), (&lass, &["c"])]); + let a = start_on_a_set(&sim, "a", "tv", id, a_set(&root), None, false); + let b = start_on_a_set(&sim, "b", "tv", id, a_set(&root), None, false); + sim.wait_ready(&[&a, &b], SOON).await; + // A chain of writes, each on the one before. + let mut on = id; + let mut ids = Vec::new(); + for n in 0..30 { + let write = tv.write(&[on], "home/tv/x", s(&n.to_string())); + on = Id::of(&write); + ids.push(on); + put(&a, &[&write]).await; + } + let (first, ids) = (&[&a, &b], &ids); + sim.wait_until("a and b hold the writes", SOON, move || async move { + first.iter().all(|node| holds(node, ids)) + }) + .await; + + let c = start_on_a_set(&sim, "c", "lass", id, a_set(&root), None, false); + let all = &[&a, &b, &c]; + sim.wait_until("c holds them too", SOON, move || async move { + holds(all[2], ids) + }) + .await; + sim.wait_until("all stand alike", SOON, move || async move { + let first = stands(all[0]); + first.is_some() && all.iter().all(|node| stands(node) == first) && entries(all[2]) == 3 + }) + .await; +} + +/// A node keeps its set in a file, written as the set changes; started again on what +/// the file holds, it holds what it held, the others' entries among it, before it hears +/// of anyone. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn a_node_of_a_set_goes_on_from_its_file() { + let sim = Sim::with_identity(93); + let (tv, _, _) = three(); + let (_, root, id) = federation(&[(&tv, &["a", "b"])]); + let dir = crate::files::scratch_dir("set-file"); + let path = dir.join("b.events"); + let a = start_on_a_set(&sim, "a", "tv", id, a_set(&root), None, false); + let b = start_on_a_set(&sim, "b", "tv", id, a_set(&root), Some(path.clone()), false); + sim.wait_ready(&[&a, &b], SOON).await; + let write = tv.write(&[id], "home/tv/x", s("1")); + let ids = [Id::of(&write)]; + put(&a, &[&write]).await; + let (nodes, ids) = (&[&a, &b], &ids); + sim.wait_until( + "both hold the write and both entries", + SOON, + move || async move { + nodes + .iter() + .all(|node| holds(node, ids) && entries(node) == 2) + }, + ) + .await; + assert!(b.holding.save().expect("written") || path.exists()); + + let bytes = std::fs::read(&path).expect("the set's file"); + let set = crate::federation::store::set::Set::<Federation>::load( + &bytes, + Duration::from_secs(600), + std::time::Instant::now(), + ) + .expect("a set"); + let b = start_on_a_set(&sim, "b", "tv", id, set, Some(path), true); + // What the file held, at once. + assert!(holds(&b, ids)); + assert_eq!(entries(&b), 2); + // And it goes on: a write put on it reaches a. + sim.wait_ready(&[&b], SOON).await; + let next = tv.write(&[ids[0]], "home/tv/x", s("2")); + let next_id = [Id::of(&next)]; + put(&b, &[&next]).await; + let (a, next_id) = (&a, &next_id); + sim.wait_until("a holds what b wrote after", SOON, move || async move { + holds(a, next_id) + }) + .await; +} |
