use super::{ContractChecker, ContractViolation, Terminator};
use crate::directory::VolumeIdentity;
use crate::notify::{Change, ChangeKind, DesyncCause, RelativeName};
use crate::queue::{Notification, Outcome, WatchId};
use crate::retry::WatchMode;
fn watch() -> WatchId {
WatchId::from_raw(1)
}
fn batch(watch: WatchId) -> Notification {
Notification::Batch {
watch,
changes: vec![Change {
kind: ChangeKind::Added,
name: RelativeName::for_test("a.txt"),
}],
}
}
fn desync(watch: WatchId, cause: DesyncCause) -> Notification {
Notification::Desync { watch, cause }
}
fn established(watch: WatchId, mode: WatchMode) -> Notification {
Notification::Established { watch, mode }
}
fn volume(serial: u32) -> VolumeIdentity {
VolumeIdentity::for_test(serial, "NTFS", "Label")
}
#[test]
fn nothing_may_follow_a_cancelled_completion() {
let mut checker = ContractChecker::new();
let w = watch();
checker
.observe(&Notification::Completion {
watch: w,
outcome: Outcome::Cancelled,
})
.expect("cancellation is legal");
assert_eq!(
checker.observe(&batch(w)),
Err(ContractViolation::AfterTerminal {
watch: w,
terminator: Terminator::Cancelled,
})
);
}
#[test]
fn nothing_may_follow_a_stopped_desync() {
let mut checker = ContractChecker::new();
let w = watch();
checker
.observe(&desync(w, DesyncCause::Stopped))
.expect("a terminal desync is legal");
assert_eq!(
checker.observe(&desync(w, DesyncCause::Overflow)),
Err(ContractViolation::AfterTerminal {
watch: w,
terminator: Terminator::Stopped,
})
);
}
#[test]
fn one_watchs_terminator_does_not_end_another() {
let mut checker = ContractChecker::new();
let (a, b) = (WatchId::from_raw(1), WatchId::from_raw(2));
checker
.observe(&desync(a, DesyncCause::Stopped))
.expect("a ends");
checker.observe(&batch(b)).expect("b is untouched");
}
#[test]
fn a_coarse_watch_may_not_emit_a_batch_or_a_kernel_overflow() {
let mut checker = ContractChecker::new();
let w = watch();
checker
.observe(&established(w, WatchMode::Coarse))
.expect("establishing coarse is legal");
assert_eq!(
checker.observe(&batch(w)),
Err(ContractViolation::BatchInCoarseTier { watch: w })
);
assert_eq!(
checker.observe(&desync(w, DesyncCause::Overflow)),
Err(ContractViolation::CauseUnreachableInTier {
watch: w,
cause: DesyncCause::Overflow,
tier: WatchMode::Coarse,
})
);
}
#[test]
fn a_coarse_watch_may_emit_queue_full() {
let mut checker = ContractChecker::new();
let w = watch();
checker
.observe(&established(w, WatchMode::Coarse))
.expect("coarse");
checker
.observe(&desync(w, DesyncCause::QueueFull))
.expect("QueueFull is legal under either tier");
}
#[test]
fn a_detailed_watch_may_not_emit_the_coarse_cause() {
let mut checker = ContractChecker::new();
let w = watch();
checker
.observe(&established(w, WatchMode::Detailed))
.expect("detailed");
assert_eq!(
checker.observe(&desync(w, DesyncCause::Coarse)),
Err(ContractViolation::CauseUnreachableInTier {
watch: w,
cause: DesyncCause::Coarse,
tier: WatchMode::Detailed,
})
);
}
#[test]
fn tier_rules_are_unchecked_until_a_tier_is_actually_reported() {
let mut checker = ContractChecker::new();
let w = watch();
checker
.observe(&desync(w, DesyncCause::Coarse))
.expect("no tier known, so nothing to contradict");
checker
.observe(&desync(w, DesyncCause::Overflow))
.expect("likewise");
}
#[test]
fn a_resumed_without_a_preceding_suspended_is_legal() {
let mut checker = ContractChecker::new();
let w = watch();
checker
.observe(&Notification::Resumed { watch: w })
.expect("Resumed may open a bracket the watch never saw start");
}
#[test]
fn a_suspended_closed_by_stopped_rather_than_resumed_is_legal() {
let mut checker = ContractChecker::new();
let w = watch();
checker
.observe(&Notification::Suspended { watch: w })
.expect("suspend");
checker
.observe(&desync(w, DesyncCause::Stopped))
.expect("a terminator may close an open bracket");
}
#[test]
fn established_need_not_be_a_watchs_first_notification() {
let mut checker = ContractChecker::new();
let w = watch();
checker
.observe(&Notification::Completion {
watch: w,
outcome: Outcome::Subscribed,
})
.expect("completion first");
checker
.observe(&desync(w, DesyncCause::Reestablished))
.expect("recovery");
checker
.observe(&Notification::Resumed { watch: w })
.expect("resumed");
checker
.observe(&established(w, WatchMode::Detailed))
.expect("and only now a tier");
}
#[test]
fn a_watchs_tier_may_change_between_establishments() {
let mut checker = ContractChecker::new();
let w = watch();
checker
.observe(&established(w, WatchMode::Detailed))
.expect("detailed");
checker
.observe(&established(w, WatchMode::Coarse))
.expect("downgraded");
checker
.observe(&established(w, WatchMode::Detailed))
.expect("and back again");
}
#[test]
fn establishing_then_subscribed_is_legal() {
let mut checker = ContractChecker::new();
let w = watch();
checker
.observe(&Notification::Completion {
watch: w,
outcome: Outcome::Establishing,
})
.expect("not a failure");
checker
.observe(&Notification::Completion {
watch: w,
outcome: Outcome::Subscribed,
})
.expect("and later establishes");
}
#[test]
fn a_volume_change_to_the_same_serial_is_rejected() {
let mut checker = ContractChecker::new();
let w = watch();
assert_eq!(
checker.observe(&Notification::VolumeChanged {
watch: w,
previous: volume(0x1111),
current: volume(0x1111),
}),
Err(ContractViolation::VolumeUnchanged { watch: w })
);
}
#[test]
fn a_second_volume_change_must_continue_from_the_first() {
let mut checker = ContractChecker::new();
let w = watch();
checker
.observe(&Notification::VolumeChanged {
watch: w,
previous: volume(0x1111),
current: volume(0x2222),
})
.expect("first change");
let mut ok = ContractChecker::new();
ok.observe(&Notification::VolumeChanged {
watch: w,
previous: volume(0x1111),
current: volume(0x2222),
})
.expect("first");
ok.observe(&Notification::VolumeChanged {
watch: w,
previous: volume(0x2222),
current: volume(0x3333),
})
.expect("continues from the prior current");
assert_eq!(
checker.observe(&Notification::VolumeChanged {
watch: w,
previous: volume(0x9999),
current: volume(0x3333),
}),
Err(ContractViolation::VolumeDiscontinuity { watch: w })
);
}
#[test]
fn observe_all_reports_the_first_violation() {
let mut checker = ContractChecker::new();
let w = watch();
let stream = vec![
Notification::Completion {
watch: w,
outcome: Outcome::Subscribed,
},
desync(w, DesyncCause::Stopped),
batch(w),
];
assert_eq!(
checker.observe_all(&stream),
Err(ContractViolation::AfterTerminal {
watch: w,
terminator: Terminator::Stopped,
})
);
}
#[test]
fn a_batch_inside_a_fault_bracket_is_legal() {
let mut checker = ContractChecker::new();
let w = watch();
checker
.observe(&Notification::Suspended { watch: w })
.expect("the fault opens");
checker
.observe(&batch(w))
.expect("the completion that triggered it still delivers");
checker
.observe(&desync(w, DesyncCause::Reestablished))
.expect("and the bracket resolves");
}
#[test]
fn a_volume_violation_does_not_cascade_into_the_next_change() {
let mut checker = ContractChecker::new();
let w = watch();
checker
.observe(&Notification::VolumeChanged {
watch: w,
previous: volume(0x1111),
current: volume(0x2222),
})
.expect("first change");
assert_eq!(
checker.observe(&Notification::VolumeChanged {
watch: w,
previous: volume(0x9999),
current: volume(0x3333),
}),
Err(ContractViolation::VolumeDiscontinuity { watch: w })
);
checker
.observe(&Notification::VolumeChanged {
watch: w,
previous: volume(0x3333),
current: volume(0x4444),
})
.expect("the violation must not echo into the next change");
}
#[test]
fn an_unchanged_volume_violation_also_advances_state() {
let mut checker = ContractChecker::new();
let w = watch();
assert_eq!(
checker.observe(&Notification::VolumeChanged {
watch: w,
previous: volume(0x1111),
current: volume(0x1111),
}),
Err(ContractViolation::VolumeUnchanged { watch: w })
);
assert_eq!(
checker.observe(&Notification::VolumeChanged {
watch: w,
previous: volume(0x9999),
current: volume(0x2222),
}),
Err(ContractViolation::VolumeDiscontinuity { watch: w }),
"the violated change still recorded its current, so this discontinuity is visible"
);
}
#[test]
fn a_saturated_cancellation_delivers_its_terminator_before_the_owed_loss() {
let (sender, receiver) =
crate::queue::channel_with_bound(std::num::NonZeroUsize::new(2).expect("a positive bound"));
let w = watch();
let cancellation = sender.reserve().expect("a slot for the cancellation");
assert_eq!(sender.send(batch(w)), crate::queue::Delivery::Queued);
assert_eq!(sender.send(batch(w)), crate::queue::Delivery::Latched);
cancellation.send(Notification::Completion {
watch: w,
outcome: Outcome::Cancelled,
});
let mut drained = Vec::new();
while let Some(item) = receiver.try_recv() {
drained.push(item);
}
assert!(
matches!(
drained.as_slice(),
[
Notification::Batch { .. },
Notification::Completion {
outcome: Outcome::Cancelled,
..
},
Notification::Desync {
cause: DesyncCause::QueueFull,
..
},
]
),
"expected Batch, Cancelled, then the owed loss; got {drained:?}"
);
let mut checker = ContractChecker::new();
checker
.observe_all(&drained)
.expect("the checker must accept what the real queue produced");
}
#[test]
fn nothing_other_than_an_owed_loss_may_follow_a_terminator() {
let w = watch();
for follower in [
batch(w),
desync(w, DesyncCause::Overflow),
desync(w, DesyncCause::Coarse),
desync(w, DesyncCause::Reestablished),
Notification::Resumed { watch: w },
established(w, WatchMode::Detailed),
] {
let mut checker = ContractChecker::new();
checker
.observe(&Notification::Completion {
watch: w,
outcome: Outcome::Cancelled,
})
.expect("cancellation");
assert_eq!(
checker.observe(&follower),
Err(ContractViolation::AfterTerminal {
watch: w,
terminator: Terminator::Cancelled,
}),
"{follower:?} must not be accepted after a terminator"
);
}
}
#[test]
fn only_one_owed_loss_may_follow_a_terminator() {
let mut checker = ContractChecker::new();
let w = watch();
checker
.observe(&Notification::Completion {
watch: w,
outcome: Outcome::Cancelled,
})
.expect("cancellation");
checker
.observe(&desync(w, DesyncCause::QueueFull))
.expect("the one owed report");
assert_eq!(
checker.observe(&desync(w, DesyncCause::QueueFull)),
Err(ContractViolation::AfterTerminal {
watch: w,
terminator: Terminator::Cancelled,
}),
"a second post-terminal loss report is not reachable"
);
}