use core::time::Duration;
use std::time::Instant;
use super::{Entry, Flip, RetireOrigin, Retirement, RootAction, RootAdvance, RootState, Stamp};
use crate::RunId;
const RUN: RunId = RunId::new(7);
fn stamp() -> Stamp {
Stamp::new(Instant::now(), crate::Timestamp::UNIX_EPOCH, RUN)
}
fn later(from: Stamp) -> Stamp {
Stamp::new(
from.monotonic() + Duration::from_secs(60),
crate::Timestamp::from(jiff::Timestamp::from_second(1_800_000_000).expect("in range")),
RunId::new(from.run().get() + 1),
)
}
fn spent(
entry: &mut Entry,
action: RootAction,
at: Stamp,
) -> (RootAdvance, Vec<Retirement>, Vec<Flip>) {
let mut owed = Vec::new();
let mut turned = Vec::new();
let advance = entry.advance(action, at, &mut |act| owed.push(act), &mut |act| {
turned.push(act)
});
(advance, owed, turned)
}
fn step(entry: &mut Entry, action: RootAction, at: Stamp) -> (RootAdvance, Vec<Retirement>) {
let named = action.clone();
let (advance, owed, turned) = spent(entry, action, at);
assert_eq!(
turned,
Vec::new(),
"{named:?} was not supposed to close a run"
);
(advance, owed)
}
fn quiet(entry: &mut Entry, action: RootAction, at: Stamp) -> RootAdvance {
let named = format!("{action:?}");
let (advance, owed) = step(entry, action, at);
assert_eq!(
owed,
Vec::new(),
"{named} was not supposed to owe the ledger anything"
);
advance
}
#[test]
fn a_fresh_entry_is_the_absence_of_news() {
let entry = Entry::idle();
assert_eq!(entry.state(), RootState::Idle);
assert_eq!(entry.queued(), &[]);
assert_eq!(entry.retiring_as(), None);
assert!(entry.is_vacant());
}
#[test]
fn intake_opens_an_analysis_with_nothing_in_flight() {
let at = stamp();
let mut entry = Entry::idle();
assert_eq!(
quiet(&mut entry, RootAction::Intake, at),
RootAdvance::Moved
);
assert_eq!(
entry.state(),
RootState::Analyzing {
inflight: 0,
since: at.monotonic(),
}
);
assert!(!entry.is_vacant());
}
#[test]
fn a_dispatch_on_an_idle_key_opens_the_analysis_itself() {
let at = stamp();
let mut entry = Entry::idle();
assert_eq!(
quiet(&mut entry, RootAction::Dispatched, at),
RootAdvance::Moved
);
assert_eq!(
entry.state(),
RootState::Analyzing {
inflight: 1,
since: at.monotonic(),
}
);
}
#[test]
fn retiring_an_untouched_key_lands_at_once() {
let at = stamp();
let mut entry = Entry::idle();
let (advance, owed) = step(&mut entry, RootAction::Retire(RetireOrigin::User), at);
assert_eq!(advance, RootAdvance::Moved);
assert_eq!(
entry.state(),
RootState::Retired {
at: at.wall(),
origin: RetireOrigin::User,
late_tails: false,
}
);
assert_eq!(
owed,
vec![Retirement::Recorded {
at: at.wall(),
origin: RetireOrigin::User,
late_tails: false,
}]
);
}
#[test]
fn an_idle_key_declines_what_it_has_no_work_for() {
let at = stamp();
for action in [
RootAction::Observed,
RootAction::Restore,
RootAction::Restored,
] {
let mut entry = Entry::idle();
assert_eq!(quiet(&mut entry, action, at), RootAdvance::Declined);
assert_eq!(entry.state(), RootState::Idle);
}
}
#[test]
fn a_reappearance_of_a_live_key_is_unchanged() {
let at = stamp();
let mut entry = Entry::idle();
assert_eq!(
quiet(&mut entry, RootAction::Reappear, at),
RootAdvance::Unchanged
);
assert!(entry.is_vacant());
}
#[test]
fn a_repeat_intake_while_analyzing_changes_nothing() {
let at = stamp();
let mut entry = Entry::idle();
assert_eq!(
quiet(&mut entry, RootAction::Intake, at),
RootAdvance::Moved
);
assert_eq!(
quiet(&mut entry, RootAction::Intake, later(at)),
RootAdvance::Unchanged
);
assert_eq!(
entry.state(),
RootState::Analyzing {
inflight: 0,
since: at.monotonic(),
}
);
}
#[test]
fn the_count_rises_and_falls_and_zero_is_idle() {
let at = stamp();
let mut entry = Entry::idle();
for expected in 1..=3 {
assert_eq!(
quiet(&mut entry, RootAction::Dispatched, at),
RootAdvance::Moved
);
assert_eq!(
entry.state(),
RootState::Analyzing {
inflight: expected,
since: at.monotonic(),
}
);
}
for expected in (1..=2).rev() {
assert_eq!(
quiet(&mut entry, RootAction::Observed, at),
RootAdvance::Moved
);
assert_eq!(
entry.state(),
RootState::Analyzing {
inflight: expected,
since: at.monotonic(),
}
);
}
let (advance, owed, turned) = spent(&mut entry, RootAction::Observed, at);
assert_eq!(advance, RootAdvance::Moved);
assert_eq!(owed, Vec::new(), "reaching zero is not a retirement");
assert_eq!(
turned,
vec![Flip::new(RUN, at.wall())],
"the last answer closes the run the first dispatch opened"
);
assert_eq!(entry.state(), RootState::Idle);
assert!(entry.is_vacant());
}
#[test]
fn an_answer_with_nothing_in_flight_is_declined() {
let at = stamp();
let mut entry = Entry::idle();
assert_eq!(
quiet(&mut entry, RootAction::Intake, at),
RootAdvance::Moved
);
assert_eq!(
quiet(&mut entry, RootAction::Observed, at),
RootAdvance::Declined
);
assert_eq!(
entry.state(),
RootState::Analyzing {
inflight: 0,
since: at.monotonic(),
}
);
}
#[test]
fn retiring_with_work_out_waits_in_retiring() {
let at = stamp();
let mut entry = Entry::idle();
assert_eq!(
quiet(&mut entry, RootAction::Dispatched, at),
RootAdvance::Moved
);
assert_eq!(
quiet(&mut entry, RootAction::Dispatched, at),
RootAdvance::Moved
);
assert_eq!(
quiet(&mut entry, RootAction::Retire(RetireOrigin::Vanished), at),
RootAdvance::Moved
);
assert_eq!(
entry.state(),
RootState::Retiring {
inflight: 2,
since: at.monotonic(),
}
);
assert_eq!(entry.retiring_as(), Some(RetireOrigin::Vanished));
}
#[test]
fn a_retiring_key_refuses_new_dispatch() {
let at = stamp();
let mut entry = retiring(2, RetireOrigin::Vanished, at);
assert_eq!(
quiet(&mut entry, RootAction::Dispatched, at),
RootAdvance::Declined
);
assert_eq!(
entry.state(),
RootState::Retiring {
inflight: 2,
since: at.monotonic(),
}
);
}
#[test]
fn the_last_answer_lands_the_retirement() {
let opened = stamp();
let drained = later(opened);
let mut entry = retiring(2, RetireOrigin::Vanished, opened);
assert_eq!(
quiet(&mut entry, RootAction::Observed, drained),
RootAdvance::Moved
);
assert_eq!(
entry.state(),
RootState::Retiring {
inflight: 1,
since: opened.monotonic(),
},
"the drain window is measured from when the retirement OPENED"
);
let (advance, owed) = step(&mut entry, RootAction::Observed, drained);
assert_eq!(advance, RootAdvance::Moved);
assert_eq!(
entry.state(),
RootState::Retired {
at: drained.wall(),
origin: RetireOrigin::Vanished,
late_tails: false,
}
);
assert_eq!(
owed,
vec![Retirement::Recorded {
at: drained.wall(),
origin: RetireOrigin::Vanished,
late_tails: false,
}]
);
assert_eq!(entry.retiring_as(), None);
}
#[test]
fn a_user_retire_outranks_a_draining_vanish() {
let at = stamp();
let mut entry = retiring(1, RetireOrigin::Vanished, at);
assert_eq!(
quiet(&mut entry, RootAction::Retire(RetireOrigin::User), at),
RootAdvance::Moved
);
assert_eq!(entry.retiring_as(), Some(RetireOrigin::User));
let (_, owed) = step(&mut entry, RootAction::Observed, at);
assert_eq!(
owed,
vec![Retirement::Recorded {
at: at.wall(),
origin: RetireOrigin::User,
late_tails: false,
}]
);
}
#[test]
fn a_vanish_does_not_outrank_a_draining_user_retire() {
let at = stamp();
let mut entry = retiring(1, RetireOrigin::User, at);
assert_eq!(
quiet(&mut entry, RootAction::Retire(RetireOrigin::Vanished), at),
RootAdvance::Unchanged
);
assert_eq!(entry.retiring_as(), Some(RetireOrigin::User));
}
#[test]
fn a_lapse_retires_a_drain_that_never_reached_zero() {
let opened = stamp();
let spent = later(opened);
let mut entry = retiring(3, RetireOrigin::Vanished, opened);
let (advance, owed) = step(&mut entry, RootAction::Lapsed, spent);
assert_eq!(advance, RootAdvance::Moved);
assert_eq!(
entry.state(),
RootState::Retired {
at: spent.wall(),
origin: RetireOrigin::Vanished,
late_tails: true,
},
"stamped with the reading of the call that spent the window, not the one that opened it"
);
assert_eq!(
owed,
vec![Retirement::Recorded {
at: spent.wall(),
origin: RetireOrigin::Vanished,
late_tails: true,
}],
"the durable act carries the flag too — the row is the truth a restart reads"
);
assert_eq!(entry.retiring_as(), None);
assert!(entry.state().has_late_tails());
}
#[test]
fn a_lapse_lands_as_the_origin_that_was_draining() {
let at = stamp();
let mut entry = retiring(1, RetireOrigin::Vanished, at);
assert_eq!(
quiet(&mut entry, RootAction::Retire(RetireOrigin::User), at),
RootAdvance::Moved
);
let (_, owed) = step(&mut entry, RootAction::Lapsed, at);
assert_eq!(
owed,
vec![Retirement::Recorded {
at: at.wall(),
origin: RetireOrigin::User,
late_tails: true,
}]
);
}
#[test]
fn the_tails_a_lapse_abandoned_still_arrive_and_are_declined() {
let at = stamp();
let mut entry = retiring(2, RetireOrigin::Vanished, at);
let (advance, _row) = step(&mut entry, RootAction::Lapsed, at);
assert_eq!(advance, RootAdvance::Moved);
assert_eq!(
quiet(&mut entry, RootAction::Observed, at),
RootAdvance::Declined
);
assert_eq!(
quiet(&mut entry, RootAction::Observed, at),
RootAdvance::Declined
);
assert!(
entry.state().has_late_tails(),
"and the flag still says they were coming"
);
}
#[test]
fn a_dispatch_refused_mid_drain_lands_the_retirement_with_late_tails() {
let at = stamp();
let mut entry = retiring(1, RetireOrigin::Vanished, at);
assert_eq!(
quiet(&mut entry, RootAction::Dispatched, at),
RootAdvance::Declined,
"the refusal stands"
);
let (advance, owed) = step(&mut entry, RootAction::Observed, at);
assert_eq!(advance, RootAdvance::Moved);
assert_eq!(
owed,
vec![Retirement::Recorded {
at: at.wall(),
origin: RetireOrigin::Vanished,
late_tails: true,
}],
"the drain ended early, and the row says a tail may still land"
);
assert_eq!(
quiet(&mut entry, RootAction::Observed, at),
RootAdvance::Declined,
"and here it is"
);
}
#[test]
fn a_remembered_refusal_is_spent_by_the_retirement_that_landed() {
let at = stamp();
let mut entry = retiring(1, RetireOrigin::Vanished, at);
assert_eq!(
quiet(&mut entry, RootAction::Dispatched, at),
RootAdvance::Declined
);
let (_, _spent) = step(&mut entry, RootAction::Observed, at);
assert!(entry.state().has_late_tails());
let (_, _erased) = step(&mut entry, RootAction::Restore, at);
let (_, _restored) = step(&mut entry, RootAction::Restored, at);
assert_eq!(entry.state(), RootState::Idle);
assert!(entry.is_vacant(), "and the memory went with it");
let (_, owed) = step(&mut entry, RootAction::Retire(RetireOrigin::User), at);
assert_eq!(
owed,
vec![Retirement::Recorded {
at: at.wall(),
origin: RetireOrigin::User,
late_tails: false,
}],
"a fresh retirement carries no memory of the last one's tail"
);
}
#[test]
fn a_lapse_declines_everywhere_but_a_draining_key() {
let at = stamp();
for mut entry in [
Entry::idle(),
analyzing(2, at),
retired(RetireOrigin::User, at),
restoring(at),
] {
let before = entry.state();
assert_eq!(
quiet(&mut entry, RootAction::Lapsed, at),
RootAdvance::Declined,
"{before:?} has no drain to bound"
);
assert_eq!(entry.state(), before);
}
}
#[test]
fn a_vanished_retiree_restores_itself_on_reappearance() {
let at = stamp();
let mut entry = retired(RetireOrigin::Vanished, at);
let (advance, owed) = step(&mut entry, RootAction::Reappear, at);
assert_eq!(advance, RootAdvance::Moved);
assert_eq!(entry.state(), RootState::Restoring);
assert_eq!(owed, vec![Retirement::Erased]);
}
#[test]
fn a_user_retiree_never_restores_itself() {
let at = stamp();
let mut entry = retired(RetireOrigin::User, at);
assert_eq!(
quiet(&mut entry, RootAction::Reappear, at),
RootAdvance::Declined
);
assert_eq!(
entry.state(),
RootState::Retired {
at: at.wall(),
origin: RetireOrigin::User,
late_tails: false,
}
);
let (advance, owed) = step(&mut entry, RootAction::Restore, at);
assert_eq!(advance, RootAdvance::Moved);
assert_eq!(entry.state(), RootState::Restoring);
assert_eq!(owed, vec![Retirement::Erased]);
}
#[test]
fn a_retired_key_takes_no_work_and_no_tail() {
let at = stamp();
for action in [
RootAction::Intake,
RootAction::Dispatched,
RootAction::Observed,
RootAction::Restored,
] {
let mut entry = retired(RetireOrigin::Vanished, at);
assert_eq!(quiet(&mut entry, action, at), RootAdvance::Declined);
assert_eq!(
entry.state(),
RootState::Retired {
at: at.wall(),
origin: RetireOrigin::Vanished,
late_tails: false,
}
);
}
}
#[test]
fn a_stronger_origin_rewrites_a_standing_retirement() {
let at = stamp();
let mut entry = retired(RetireOrigin::Vanished, at);
let (advance, owed) = step(
&mut entry,
RootAction::Retire(RetireOrigin::User),
later(at),
);
assert_eq!(advance, RootAdvance::Moved);
assert_eq!(
entry.state(),
RootState::Retired {
at: at.wall(),
origin: RetireOrigin::User,
late_tails: false,
},
"the instant it retired is the FIRST retirement's — only who owns it changed"
);
assert_eq!(
owed,
vec![Retirement::Recorded {
at: at.wall(),
origin: RetireOrigin::User,
late_tails: false,
}]
);
assert_eq!(
quiet(&mut entry, RootAction::Retire(RetireOrigin::User), at),
RootAdvance::Unchanged
);
}
#[test]
fn a_restore_completes_when_the_erase_lands() {
let at = stamp();
let mut entry = retired(RetireOrigin::User, at);
let (advance, owed) = step(&mut entry, RootAction::Restore, at);
assert_eq!(advance, RootAdvance::Moved);
assert_eq!(
owed,
vec![Retirement::Erased],
"the row is owed an erase the moment the restore starts"
);
assert_eq!(entry.state(), RootState::Restoring);
assert_eq!(
quiet(&mut entry, RootAction::Restored, at),
RootAdvance::Moved
);
assert_eq!(entry.state(), RootState::Idle);
assert!(entry.is_vacant());
}
#[test]
fn a_restoring_key_is_unchanged_by_another_restore() {
let at = stamp();
let mut entry = restoring(at);
for action in [RootAction::Restore, RootAction::Reappear] {
assert_eq!(quiet(&mut entry, action, at), RootAdvance::Unchanged);
assert_eq!(entry.state(), RootState::Restoring);
}
}
#[test]
fn the_queue_keeps_order_through_a_completion() {
let at = stamp();
let mut entry = restoring(at);
assert_eq!(
quiet(&mut entry, RootAction::Retire(RetireOrigin::User), at),
RootAdvance::Queued
);
assert_eq!(
quiet(&mut entry, RootAction::Restore, at),
RootAdvance::Queued
);
assert_eq!(
quiet(&mut entry, RootAction::Retire(RetireOrigin::User), at),
RootAdvance::Queued
);
assert_eq!(
entry.queued(),
&[
RootAction::Retire(RetireOrigin::User),
RootAction::Restore,
RootAction::Retire(RetireOrigin::User),
]
);
let (advance, owed) = step(&mut entry, RootAction::Restored, at);
assert_eq!(advance, RootAdvance::Moved);
assert_eq!(entry.state(), RootState::Restoring);
assert_eq!(
owed,
vec![
Retirement::Recorded {
at: at.wall(),
origin: RetireOrigin::User,
late_tails: false,
},
Retirement::Erased,
]
);
assert_eq!(entry.queued(), &[RootAction::Retire(RetireOrigin::User)]);
let (_, owed) = step(&mut entry, RootAction::Restored, at);
assert_eq!(
entry.state(),
RootState::Retired {
at: at.wall(),
origin: RetireOrigin::User,
late_tails: false,
}
);
assert_eq!(
owed,
vec![Retirement::Recorded {
at: at.wall(),
origin: RetireOrigin::User,
late_tails: false,
}]
);
}
#[test]
fn a_repeat_at_the_tail_coalesces() {
let at = stamp();
let mut entry = restoring(at);
assert_eq!(
quiet(&mut entry, RootAction::Intake, at),
RootAdvance::Queued
);
assert_eq!(
quiet(&mut entry, RootAction::Intake, at),
RootAdvance::Coalesced
);
assert_eq!(
quiet(&mut entry, RootAction::Intake, at),
RootAdvance::Coalesced
);
assert_eq!(entry.queued(), &[RootAction::Intake]);
}
#[test]
fn a_repeat_that_is_not_at_the_tail_still_queues() {
let at = stamp();
let mut entry = restoring(at);
assert_eq!(
quiet(&mut entry, RootAction::Intake, at),
RootAdvance::Queued
);
assert_eq!(
quiet(&mut entry, RootAction::Retire(RetireOrigin::User), at),
RootAdvance::Queued
);
assert_eq!(
quiet(&mut entry, RootAction::Intake, at),
RootAdvance::Queued
);
assert_eq!(
entry.queued(),
&[
RootAction::Intake,
RootAction::Retire(RetireOrigin::User),
RootAction::Intake,
]
);
}
#[test]
fn a_queued_intake_reopens_the_analysis() {
let at = stamp();
let mut entry = restoring(at);
assert_eq!(
quiet(&mut entry, RootAction::Intake, at),
RootAdvance::Queued
);
assert_eq!(
quiet(&mut entry, RootAction::Restored, later(at)),
RootAdvance::Moved
);
assert_eq!(
entry.state(),
RootState::Analyzing {
inflight: 0,
since: later(at).monotonic(),
}
);
assert_eq!(entry.queued(), &[]);
}
#[test]
fn the_counting_calls_never_queue() {
let at = stamp();
let mut entry = restoring(at);
for action in [RootAction::Dispatched, RootAction::Observed] {
assert_eq!(quiet(&mut entry, action, at), RootAdvance::Declined);
assert_eq!(entry.queued(), &[]);
}
}
#[test]
fn only_a_retirement_that_stays_down_outranks() {
assert!(RetireOrigin::User.outranks(&RetireOrigin::Vanished));
assert!(!RetireOrigin::Vanished.outranks(&RetireOrigin::User));
assert!(!RetireOrigin::User.outranks(&RetireOrigin::User));
assert!(!RetireOrigin::Vanished.outranks(&RetireOrigin::Vanished));
let other = RetireOrigin::Other("policy".into());
assert!(other.outranks(&RetireOrigin::Vanished));
assert!(!RetireOrigin::Vanished.outranks(&other));
assert!(!other.outranks(&RetireOrigin::User));
}
#[test]
fn only_the_two_mid_act_states_are_busy() {
let at = stamp();
assert!(!RootState::Idle.is_busy());
assert!(
!RootState::Analyzing {
inflight: 3,
since: at.monotonic(),
}
.is_busy()
);
assert!(
RootState::Retiring {
inflight: 1,
since: Instant::now(),
}
.is_busy()
);
assert!(
!RootState::Retired {
at: at.wall(),
origin: RetireOrigin::User,
late_tails: false,
}
.is_busy()
);
assert!(RootState::Restoring.is_busy());
}
fn analyzing(inflight: u32, at: Stamp) -> Entry {
let mut entry = Entry::idle();
for _ in 0..inflight {
assert_eq!(
quiet(&mut entry, RootAction::Dispatched, at),
RootAdvance::Moved
);
}
entry
}
fn retiring(inflight: u32, origin: RetireOrigin, at: Stamp) -> Entry {
let mut entry = Entry::idle();
for _ in 0..inflight {
assert_eq!(
quiet(&mut entry, RootAction::Dispatched, at),
RootAdvance::Moved
);
}
assert_eq!(
quiet(&mut entry, RootAction::Retire(origin), at),
RootAdvance::Moved
);
assert_eq!(
entry.state(),
RootState::Retiring {
inflight,
since: at.monotonic(),
}
);
entry
}
fn retired(origin: RetireOrigin, at: Stamp) -> Entry {
let mut entry = Entry::idle();
let (advance, _) = step(&mut entry, RootAction::Retire(origin), at);
assert_eq!(advance, RootAdvance::Moved);
entry
}
fn restoring(at: Stamp) -> Entry {
let mut entry = retired(RetireOrigin::User, at);
let (advance, _) = step(&mut entry, RootAction::Restore, at);
assert_eq!(advance, RootAdvance::Moved);
assert_eq!(entry.state(), RootState::Restoring);
entry
}
#[test]
fn an_agreeing_ask_still_queues_behind_a_waiting_one() {
let at = stamp();
let mut entry = restoring(at);
assert_eq!(
quiet(&mut entry, RootAction::Restore, at),
RootAdvance::Unchanged,
"nothing waits, so the restore under way IS the answer"
);
assert_eq!(
quiet(&mut entry, RootAction::Retire(RetireOrigin::User), at),
RootAdvance::Queued
);
assert_eq!(
quiet(&mut entry, RootAction::Restore, at),
RootAdvance::Queued,
"a retirement now waits, so where this key is headed is retired — the restore is a real ask"
);
}
#[test]
fn the_rank_reaches_the_running_act_past_a_queue() {
let at = stamp();
let mut entry = retiring(1, RetireOrigin::Vanished, at);
assert_eq!(
quiet(&mut entry, RootAction::Intake, at),
RootAdvance::Queued
);
assert_eq!(
quiet(&mut entry, RootAction::Retire(RetireOrigin::User), at),
RootAdvance::Moved
);
assert_eq!(entry.retiring_as(), Some(RetireOrigin::User));
assert_eq!(entry.queued(), &[RootAction::Intake]);
}
#[test]
fn either_road_out_of_idle_opens_a_run() {
for road in [RootAction::Intake, RootAction::Dispatched] {
let at = stamp();
let mut entry = Entry::idle();
assert_eq!(quiet(&mut entry, road.clone(), at), RootAdvance::Moved);
assert_eq!(
entry.running_as(),
Some(RUN),
"{road:?} left the key analyzing with no run on it"
);
}
}
#[test]
fn a_run_closes_exactly_once() {
let at = stamp();
let mut entry = Entry::idle();
let _opened = quiet(&mut entry, RootAction::Intake, at);
let _out = quiet(&mut entry, RootAction::Dispatched, at);
let (advance, owed, turned) = spent(&mut entry, RootAction::Observed, at);
assert_eq!(advance, RootAdvance::Moved);
assert_eq!(owed, Vec::new());
assert_eq!(turned, vec![Flip::new(RUN, at.wall())]);
assert_eq!(entry.running_as(), None, "the number was not taken");
let (advance, _owed, turned) = spent(&mut entry, RootAction::Observed, at);
assert_eq!(advance, RootAdvance::Declined);
assert_eq!(turned, Vec::new(), "a declined answer closed a run");
}
#[test]
fn a_second_analysis_is_a_second_run() {
let at = stamp();
let mut entry = Entry::idle();
let _opened = quiet(&mut entry, RootAction::Dispatched, at);
let (_advance, _owed, first) = spent(&mut entry, RootAction::Observed, at);
assert_eq!(first, vec![Flip::new(RUN, at.wall())]);
let again = later(at);
let _reopened = quiet(&mut entry, RootAction::Dispatched, again);
let (_advance, _owed, second) = spent(&mut entry, RootAction::Observed, again);
assert_eq!(second, vec![Flip::new(again.run(), again.wall())]);
assert_ne!(again.run(), RUN, "the second stamp carried the same run");
}
#[test]
fn a_retirement_masks_the_run_it_interrupted() {
let at = stamp();
let mut entry = Entry::idle();
let _opened = quiet(&mut entry, RootAction::Intake, at);
let _out = quiet(&mut entry, RootAction::Dispatched, at);
assert_eq!(entry.running_as(), Some(RUN));
let (advance, owed, turned) = spent(&mut entry, RootAction::Retire(RetireOrigin::User), at);
assert_eq!(advance, RootAdvance::Moved);
assert_eq!(owed, Vec::new(), "the drain has not ended yet");
assert_eq!(turned, Vec::new(), "a retirement is not a flip");
assert_eq!(entry.running_as(), None, "the run outlived the retirement");
let (advance, owed, turned) = spent(&mut entry, RootAction::Observed, at);
assert_eq!(advance, RootAdvance::Moved);
assert_eq!(owed.len(), 1, "the drain's last answer owes the ledger");
assert_eq!(turned, Vec::new(), "a retiring key closed a run");
}
#[test]
fn a_restored_key_runs_again_under_a_new_number() {
let at = stamp();
let mut entry = Entry::idle();
let _opened = quiet(&mut entry, RootAction::Dispatched, at);
let _out = quiet(&mut entry, RootAction::Retire(RetireOrigin::User), at);
let (_advance, _owed, turned) = spent(&mut entry, RootAction::Observed, at);
assert_eq!(turned, Vec::new());
let _restoring = step(&mut entry, RootAction::Restore, at);
let _restored = step(&mut entry, RootAction::Restored, at);
assert_eq!(entry.state(), RootState::Idle);
assert_eq!(entry.running_as(), None);
let again = later(at);
let _reopened = quiet(&mut entry, RootAction::Dispatched, again);
let (_advance, _owed, turned) = spent(&mut entry, RootAction::Observed, again);
assert_eq!(
turned,
vec![Flip::new(again.run(), again.wall())],
"the restored key flipped under the run the retirement masked"
);
}