aboutsummaryrefslogtreecommitdiffstats
path: root/src/simtest/holding.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-16 23:00:34 +0000
committertv <tv@krebsco.de>2026-11-17 00:09:49 +0100
commit7469406a45ab625d9ba6ad430f01573c16f8f4de (patch)
tree222d51fc94faedd0761439b6eee0e6c360b13593 /src/simtest/holding.rs
parentce8ccdc68f5b0cc52b380177665ee07505c5bfb5 (diff)
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.
Diffstat (limited to 'src/simtest/holding.rs')
-rw-r--r--src/simtest/holding.rs94
1 files changed, 94 insertions, 0 deletions
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());
+}