use raft_io::{Action, Event, RaftConfig, RaftNode};
fn main() {
let mut node = RaftNode::new(RaftConfig::single(1));
let mut ticks = 0u32;
while !node.is_leader() {
let _ = node
.step(Event::Tick)
.expect("an in-memory tick never fails");
ticks += 1;
}
println!(
"node {} became leader in term {} after {ticks} ticks",
node.id(),
node.term()
);
for command in [&b"set x = 1"[..], b"set y = 2", b"delete x"] {
let actions = node
.step(Event::Propose(command.to_vec()))
.expect("the leader accepts proposals");
for action in actions {
if let Action::Apply { index, command, .. } = action {
println!(" applied #{index}: {}", String::from_utf8_lossy(&command));
}
}
}
println!("commit index is now {}", node.commit_index());
assert_eq!(node.commit_index(), 3);
}