aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/simtest/holding.rs135
1 files changed, 135 insertions, 0 deletions
diff --git a/src/simtest/holding.rs b/src/simtest/holding.rs
index 120c6da..8091cfc 100644
--- a/src/simtest/holding.rs
+++ b/src/simtest/holding.rs
@@ -2197,3 +2197,138 @@ async fn a_node_of_both_kinds_carries_events_between_them() {
assert!(events::ids(&a.doc.read().await.doc).contains(&ids[1]));
assert!(e.doc.read().await.heads().is_empty());
}
+
+/// A swarm moves from Automerge documents to sets of events, node by node, as its
+/// operators are told to: a and b on the document; then each of both kinds, started
+/// again on its document with a file for its set; then each on its set alone, started
+/// again from that file. What was put before is held throughout, and what is put at
+/// each step reaches the other node.
+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
+async fn a_swarm_moves_to_sets_node_by_node() {
+ use crate::federation::store::set::Set;
+
+ let sim = Sim::with_identity(98);
+ let (tv, lass, _) = three();
+ let (_, root, id) = federation(&[(&tv, &["a"]), (&lass, &["b"])]);
+ let dir = crate::files::scratch_dir("move-to-sets");
+ let keeps = Duration::from_secs(600);
+ let nodes = started(
+ &sim,
+ &root,
+ vec![("a", holding("tv")), ("b", holding("lass"))],
+ )
+ .await;
+ let (mut a, mut b) = (nodes[0].clone(), nodes[1].clone());
+ let x = tv.write(&[id], "home/tv/x", s("1"));
+ put(&a, &[&x]).await;
+ let mut ids = vec![Id::of(&x)];
+ {
+ let (pair, ids) = (&[&a, &b], &ids);
+ sim.wait_until("both hold x", LONG, move || async move {
+ pair.iter().all(|node| holds(node, ids))
+ })
+ .await;
+ }
+
+ // Each of both kinds, on the document it has.
+ for (name, party, seeds) in [("a", "tv", [""; 0].as_slice()), ("b", "lass", &["a"])] {
+ let node = if name == "a" { &a } else { &b };
+ let doc = node.doc.read().await.doc.clone();
+ let path = dir.join(format!("{name}.events"));
+ let party = party.to_owned();
+ let again = sim.restart_with(
+ name,
+ seeds,
+ Start::FromDocument(Origin::Root),
+ move |config| {
+ config.store = Some(id);
+ config.initial_doc = doc;
+ config.holding = holding(&party).of_both(keeps).carrying_if(
+ None,
+ Some(path),
+ Arc::new(Files::default()),
+ );
+ },
+ );
+ sim.wait_ready(&[&again], SOON).await;
+ {
+ let (again, ids) = (&again, &ids);
+ sim.wait_until("it made its set, holding what it held", LONG, move || async move {
+ again.holding.with_set(|_| ()).is_some() && holds(again, ids)
+ })
+ .await;
+ }
+ if name == "a" {
+ a = again;
+ } else {
+ b = again;
+ }
+ }
+ let y = lass.write(&[ids[0]], "home/lass/y", s("2"));
+ put(&b, &[&y]).await;
+ ids.push(Id::of(&y));
+ {
+ let (pair, ids) = (&[&a, &b], &ids);
+ sim.wait_until(
+ "both hold y, and each other's entry in their sets",
+ LONG,
+ move || async move {
+ pair.iter()
+ .all(|node| holds(node, ids) && entries(node) == 2)
+ },
+ )
+ .await;
+ }
+
+ // Each on its set alone, from the set's file.
+ for (name, party, seeds) in [("a", "tv", [""; 0].as_slice()), ("b", "lass", &["a"])] {
+ let node = if name == "a" { &a } else { &b };
+ node.holding.save().expect("the set written");
+ let path = dir.join(format!("{name}.events"));
+ let bytes = std::fs::read(&path).expect("the set's file");
+ let set = Set::<Federation>::load(&bytes, keeps, std::time::Instant::now()).expect("a set");
+ let party = party.to_owned();
+ let again = sim.restart_with(
+ name,
+ seeds,
+ Start::FromDocument(Origin::Root),
+ move |config| {
+ config.store = Some(id);
+ config.holding = holding(&party).carrying_if(
+ Some(held::Begun::Set(Box::new(set))),
+ Some(path),
+ Arc::new(Files::default()),
+ );
+ },
+ );
+ assert_eq!(again.kinds(), [crate::federation::store::set::KIND]);
+ assert!(holds(&again, &ids), "what the file held");
+ sim.wait_ready(&[&again], SOON).await;
+ let z = tv.write(
+ &[*ids.last().expect("some")],
+ &format!("home/tv/{name}"),
+ s("3"),
+ );
+ put(&again, &[&z]).await;
+ ids.push(Id::of(&z));
+ if name == "a" {
+ a = again;
+ } else {
+ b = again;
+ }
+ let (pair, ids) = (&[&a, &b], &ids);
+ sim.wait_until(
+ "both hold what was put on the one started again",
+ LONG,
+ move || async move { pair.iter().all(|node| holds(node, ids)) },
+ )
+ .await;
+ }
+ assert!(a.doc.read().await.heads().is_empty());
+ assert_eq!(read(&a, |store| store.state().values().len()), Some(4));
+ let pair = &[&a, &b];
+ sim.wait_until("they stand alike", LONG, move || async move {
+ stands(pair[0]).is_some() && stands(pair[0]) == stands(pair[1])
+ })
+ .await;
+}