//! 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()); }