From 7469406a45ab625d9ba6ad430f01573c16f8f4de Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 16 Nov 2026 23:00:34 +0000 Subject: simtest: a swarm holding a store, its members and a node its party doesn't list tv lists a and b, which start from the store's root; lass lists c, which joins by the store's ID; d is of lass too, which doesn't list it. Events put on a and on c reach a, b and c, which read them; d, which no member syncs with, gets nothing. A simulated node started on a document expects its swarm, as `run` has it do. --- src/simtest/holding.rs | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/simtest/mod.rs | 15 ++++++++ 2 files changed, 109 insertions(+) create mode 100644 src/simtest/holding.rs (limited to 'src/simtest') diff --git a/src/simtest/holding.rs b/src/simtest/holding.rs new file mode 100644 index 0000000..cbf1942 --- /dev/null +++ b/src/simtest/holding.rs @@ -0,0 +1,94 @@ +//! A swarm holding a store (`docs/store.md`, Members): nodes started from its root, or +//! joining by its ID, sync with the members its store names; the events put on any of +//! them reach all of them, and none reaches a node its party doesn't list. + +use std::sync::Arc; +use std::time::Duration; + +use super::{SOON, Sim, sim_key}; +use crate::document::events; +use crate::federation::fold::Federation; +use crate::federation::harness::{self, Party, Spec}; +use crate::federation::held::{self, Holding}; +use crate::federation::store::{Fold, Id}; +use crate::files::Files; +use crate::node::NodeConfig; +use crate::state::AppState; +use crate::swarm::bootstrap::{Origin, Start}; + +/// `config`, of a node of the swarm holding the store `id`, of `party`: started from the +/// store's `root` where given (`--store-root`), else joining by its ID (`--store-id`). +fn of_the_store(config: &mut NodeConfig, id: Id, party: &str, root: Option<&[u8]>) { + config.store = Some(id); + config.holding = Holding::open( + None, + Arc::new(Files::default()), + None, + Some(party.to_owned()), + ) + .expect("no file to read"); + if let Some(root) = root { + config.initial_doc = events::started(Federation::NAME, root).expect("a root"); + } +} + +/// Whether `node` holds every event of `ids`. +fn holds(node: &AppState, ids: &[Id]) -> bool { + node.holding + .with(|store| ids.iter().all(|id| store.holds(id))) + == Some(true) +} + +/// tv lists a and b, which start from the root; lass lists c, which joins by the store's +/// ID; d is of lass too, which doesn't list it. Events put on a and on c reach a, b and +/// c; d, which no member syncs with, gets nothing. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn the_events_put_on_a_member_reach_every_member_alone() { + let sim = Sim::with_identity(81); + let (tv, lass) = (Party::new("tv", 1), Party::new("lass", 2)); + let mut first = Spec::first(&[&tv, &lass], &[]); + first.parties[0].nodes = vec![sim_key("a").id(), sim_key("b").id()]; + first.parties[1].nodes = vec![sim_key("c").id()]; + let root = first.root(); + let id = Id::of(&root); + let a = sim.start_with("a", &[], Start::FromDocument(Origin::Root), |config| { + of_the_store(config, id, "tv", Some(&root)) + }); + let b = sim.start_with("b", &["a"], Start::FromDocument(Origin::Root), |config| { + of_the_store(config, id, "tv", Some(&root)) + }); + let c = sim.start_with("c", &["a"], Start::Join, |config| { + of_the_store(config, id, "lass", None) + }); + let members = [&a, &b, &c]; + sim.wait_ready(&members, SOON).await; + sim.wait_until("a, b and c hold the store", SOON, move || async move { + members.iter().all(|node| node.holding.tells_members()) + }) + .await; + + let d = sim.start_with("d", &["a"], Start::Join, |config| { + of_the_store(config, id, "lass", None) + }); + let on_a = tv.write(&[id], "home/tv/x", harness::str("1")); + let on_c = lass.write(&[id], "home/lass/y", harness::str("2")); + let ids = [Id::of(&on_a), Id::of(&on_c)]; + held::write(&a, None, on_a).await.expect("put on a"); + held::write(&c, None, on_c).await.expect("put on c"); + sim.wait_until("a, b and c hold both events", SOON, move || async move { + members.iter().all(|node| holds(node, &ids)) + }) + .await; + sim.wait_converged(&members, SOON).await; + for node in members { + assert_eq!( + node.holding.with(|store| store.state().values().len()), + Some(2) + ); + } + // Long enough for d to have joined, were it let in. + tokio::time::sleep(Duration::from_secs(3)).await; + assert!(!d.is_ready().await); + assert!(!d.holding.tells_members()); + assert!(events::ids(&d.doc.read().await.doc).is_empty()); +} diff --git a/src/simtest/mod.rs b/src/simtest/mod.rs index 4da8821..b886721 100644 --- a/src/simtest/mod.rs +++ b/src/simtest/mod.rs @@ -11,6 +11,8 @@ mod bench; mod deltas; +#[cfg(feature = "federation")] +mod holding; mod hyparview; mod identity; mod policy; @@ -248,6 +250,19 @@ impl Sim { let bootstrapping = state.clone(); let id = id.to_owned(); tokio::spawn(async move { + // As `run` does: a document to start on names its swarm, the one it joins. + if let Start::FromDocument(_) = start { + let swarm = store::read_path( + &bootstrapping.doc.read().await.doc, + &crate::document::meta::path(&[crate::document::meta::SWARM]), + ); + if let Ok(Some(JsonValue::String(swarm_id))) = swarm { + bootstrapping + .set_expected_swarm_id(swarm_id) + .await + .expect("the swarm of the document to start on"); + } + } if let Err(error) = bootstrap::bootstrap(&bootstrapping, &seeds, start).await { panic!("bootstrapping {id} failed: {error:#}"); } -- cgit v1.3.1