use pretty_assertions::assert_eq;
use super::*;
#[derive(Debug, Default)]
struct AtCheck {
follow: Follow,
replaced: bool,
idle: bool,
update_starts_while_probing: bool,
probes: u32,
claims: u32,
}
impl Watched for AtCheck {
fn follow(&self) -> Follow {
self.follow
}
async fn replaced(&mut self) -> bool {
self.probes += 1;
if self.update_starts_while_probing {
self.follow = Follow::Held;
}
self.replaced
}
fn commit(&mut self, follow: Follow, once_idle: bool) -> bool {
if follow != self.follow {
return false;
}
if !once_idle {
return true;
}
self.claims += 1;
self.idle
}
}
#[tokio::test]
async fn each_check_begins_the_move_the_update_left_it() {
let once_idle = |once_idle| Follow::Replace { once_idle };
let restart = Some(Begin::Restart);
let replace = Some(Begin::Replace);
let cases = [
((Follow::Held, true, true, false), (None, 0, 0)),
((Follow::Watch, false, true, false), (None, 1, 0)),
((Follow::Watch, true, false, false), (None, 1, 1)),
((Follow::Watch, true, true, false), (restart, 1, 1)),
((Follow::Watch, true, true, true), (None, 1, 0)),
((Follow::RestartNow, false, false, false), (None, 1, 0)),
((Follow::RestartNow, true, false, false), (restart, 1, 0)),
((Follow::RestartNow, true, true, true), (None, 1, 0)),
((once_idle(false), false, false, false), (replace, 0, 0)),
((once_idle(true), false, false, false), (None, 0, 1)),
((once_idle(true), false, true, false), (replace, 0, 1)),
];
for ((follow, replaced, idle, update_starts_while_probing), expected) in cases {
let case = format!("{follow:?}, replaced {replaced}, idle {idle}, update starts {update_starts_while_probing}");
let mut at = AtCheck {
follow,
replaced,
idle,
update_starts_while_probing,
..AtCheck::default()
};
let began = at_check(&mut at).await.map(|(begin, began_for)| {
assert_eq!(began_for, follow, "{case}");
begin
});
assert_eq!((began, at.probes, at.claims), expected, "{case}");
}
}