extern crate alloc;
use alloc::vec::Vec;
use core::num::NonZeroUsize;
use crate::metis::Dot;
use crate::metis::tests::support::dot as d;
use super::super::Note;
use super::super::replica::Replica;
#[test]
fn a_weave_racing_a_partial_condense_strands_its_anchor() {
const ROSTER: [u32; 3] = [1, 2, 3];
let depth = NonZeroUsize::new(2).expect("positive");
let mut one = Replica::new(1, &ROSTER, depth);
let mut two = Replica::new(2, &ROSTER, depth);
let mut three = Replica::new(3, &ROSTER, depth);
let mut round = Vec::new();
let _ = one.insert_visible(0, &mut round);
while !round.is_empty() {
let mut next = Vec::new();
for note in &round {
for replica in [&mut one, &mut two, &mut three] {
let mut out = Vec::new();
replica.handle(note, &mut out);
next.extend(out);
}
}
round = next;
}
let mut round = Vec::new();
let _ = three.insert_visible(0, &mut round);
while !round.is_empty() {
let mut next = Vec::new();
for note in &round {
for replica in [&mut one, &mut two, &mut three] {
let mut out = Vec::new();
replica.handle(note, &mut out);
next.extend(out);
}
}
round = next;
}
let mut round = Vec::new();
let deleted = one.delete_visible(0, &mut round);
assert_eq!(deleted, Some(d(3, 1)), "the front element is the tombstone");
while !round.is_empty() {
let mut next = Vec::new();
for note in &round {
for replica in [&mut one, &mut two, &mut three] {
let mut out = Vec::new();
replica.handle(note, &mut out);
next.extend(out);
}
}
round = next;
}
assert_eq!(
two.condense(),
1,
"the settled witness licenses the excision"
);
let mut round = Vec::new();
let minted = three.insert_visible(0, &mut round);
while !round.is_empty() {
let mut next = Vec::new();
for note in &round {
for replica in [&mut one, &mut two, &mut three] {
let mut out = Vec::new();
replica.handle(note, &mut out);
next.extend(out);
}
}
round = next;
}
assert!(
one.effective_order().contains(&minted),
"the uncondensed replica places the weave"
);
assert!(
three.effective_order().contains(&minted),
"the writer places its own weave"
);
assert!(
!two.effective_order().contains(&minted),
"the condensed replica strands the weave: the claim was not \
fleet-applied before writing resumed"
);
}
fn flood(mut round: Vec<Note>, replicas: &mut [&mut Replica]) {
while !round.is_empty() {
let mut next = Vec::new();
for note in &round {
for replica in replicas.iter_mut() {
let mut out = Vec::new();
replica.handle(note, &mut out);
next.extend(out);
}
}
round = next;
}
}
#[test]
fn the_writer_discipline_unstrands_the_racing_weave() {
const ROSTER: [u32; 3] = [1, 2, 3];
let depth = NonZeroUsize::new(2).expect("positive");
let mut one = Replica::new(1, &ROSTER, depth);
let mut two = Replica::new(2, &ROSTER, depth);
let mut three = Replica::new(3, &ROSTER, depth);
let mut round = Vec::new();
let _ = one.insert_visible(0, &mut round);
flood(round, &mut [&mut one, &mut two, &mut three]);
let mut round = Vec::new();
let _ = three.insert_visible(0, &mut round);
flood(round, &mut [&mut one, &mut two, &mut three]);
let mut round = Vec::new();
let deleted = one.delete_visible(0, &mut round);
assert_eq!(deleted, Some(d(3, 1)), "the front element is the tombstone");
flood(round, &mut [&mut one, &mut two, &mut three]);
assert_eq!(
two.condense(),
1,
"the settled witness licenses the excision"
);
let mut round = Vec::new();
let minted = three.insert_disciplined(0, &mut round);
flood(round, &mut [&mut one, &mut two, &mut three]);
for (replica, name) in [
(&one, "uncondensed"),
(&two, "condensed"),
(&three, "writer"),
] {
assert!(
replica.effective_order().contains(&minted),
"the {name} replica places the disciplined weave"
);
}
assert_eq!(one.effective_order(), two.effective_order());
assert_eq!(two.effective_order(), three.effective_order());
assert_eq!(one.condense(), 1, "the laggard's own meet licenses it late");
assert_eq!(three.condense(), 1);
assert_eq!(one.text().store().to_bytes(), two.text().store().to_bytes());
assert_eq!(
two.text().store().to_bytes(),
three.text().store().to_bytes()
);
}
#[test]
fn the_discipline_binds_on_own_acknowledgement_not_the_meet() {
const ROSTER: [u32; 3] = [1, 2, 3];
let depth = NonZeroUsize::new(2).expect("positive");
let mut one = Replica::new(1, &ROSTER, depth);
let mut two = Replica::new(2, &ROSTER, depth);
let mut three = Replica::new(3, &ROSTER, depth);
let mut round = Vec::new();
let _ = one.insert_visible(0, &mut round);
flood(round, &mut [&mut one, &mut two, &mut three]);
let mut round = Vec::new();
let _ = three.insert_visible(0, &mut round);
flood(round, &mut [&mut one, &mut two, &mut three]);
let mut round = Vec::new();
let deleted = two.delete_visible(0, &mut round);
assert_eq!(deleted, Some(d(3, 1)), "the front element is the tombstone");
let held_for_one = round.clone();
flood(round, &mut [&mut two, &mut three]);
let mut weave_notes = Vec::new();
let minted = three.insert_disciplined(0, &mut weave_notes);
let mut from_one = Vec::new();
for note in &held_for_one {
one.handle(note, &mut from_one);
}
flood(from_one, &mut [&mut two, &mut three]);
assert_eq!(two.condense(), 1, "the full meet licenses the excision");
flood(weave_notes, &mut [&mut one, &mut two, &mut three]);
for (replica, name) in [(&one, "late"), (&two, "condensed"), (&three, "writer")] {
assert!(
replica.effective_order().contains(&minted),
"the {name} replica places the oath-bound weave"
);
}
assert_eq!(one.effective_order(), two.effective_order());
assert_eq!(two.effective_order(), three.effective_order());
}
#[test]
fn the_discipline_prices_placement_not_convergence() {
fn race(disciplined: bool) -> (Dot, Dot, Vec<Dot>) {
const ROSTER: [u32; 3] = [1, 2, 3];
let depth = NonZeroUsize::new(2).expect("positive");
let mut one = Replica::new(1, &ROSTER, depth);
let mut two = Replica::new(2, &ROSTER, depth);
let mut three = Replica::new(3, &ROSTER, depth);
let mut round = Vec::new();
let _ = one.insert_visible(0, &mut round);
flood(round, &mut [&mut one, &mut two, &mut three]);
let mut round = Vec::new();
let _ = three.insert_visible(0, &mut round);
flood(round, &mut [&mut one, &mut two, &mut three]);
let mut round = Vec::new();
let _ = one.insert_visible(2, &mut round);
flood(round, &mut [&mut one, &mut two, &mut three]);
let mut round = Vec::new();
let deleted = two.delete_visible(0, &mut round);
assert_eq!(deleted, Some(d(3, 1)), "the front element is the tombstone");
let held_for_one = round.clone();
flood(round, &mut [&mut two, &mut three]);
let mut notes_one = Vec::new();
let mut notes_three = Vec::new();
let (n1, n3) = if disciplined {
(
one.insert_disciplined(0, &mut notes_one),
three.insert_disciplined(0, &mut notes_three),
)
} else {
(
one.insert_visible(0, &mut notes_one),
three.insert_visible(0, &mut notes_three),
)
};
let mut from_one = Vec::new();
for note in &held_for_one {
one.handle(note, &mut from_one);
}
flood(from_one, &mut [&mut one, &mut two, &mut three]);
flood(notes_one, &mut [&mut one, &mut two, &mut three]);
flood(notes_three, &mut [&mut one, &mut two, &mut three]);
assert_eq!(one.effective_order(), two.effective_order());
assert_eq!(two.effective_order(), three.effective_order());
(n1, n3, one.effective_order())
}
let (d1, d3, disciplined) = race(true);
let (p1, p3, plain) = race(false);
assert_eq!(d1, p1, "the same schedule mints the same dots");
assert_eq!(d3, p3, "the same schedule mints the same dots");
let position = |order: &[Dot], dot: Dot| {
order
.iter()
.position(|&placed| placed == dot)
.expect("placed")
};
assert!(
position(&plain, p3) < position(&plain, p1),
"plain rule: the shared-bucket rank contest reads station 3's \
mint first: {plain:?}"
);
assert!(
position(&disciplined, d1) < position(&disciplined, d3),
"discipline: the rebound weave forfeits the contest and reads \
after the Before(T) region: {disciplined:?}"
);
}