use std::collections::BTreeMap;
use std::fmt;
use std::time::Duration;
use crate::authoring::{Congestion, Kind, Reliability};
use crate::contract::{Body, Rate, Resource};
pub const NO_CEILING: u64 = 4_294_967_295;
pub const STREAM_LIVENESS: Duration = Duration::from_secs(3600);
pub const DEFAULT_LEASE: Duration = Duration::from_secs(10);
pub const DEFAULT_KEEP_ALIVE: u32 = 4;
pub const DEFAULT_WAIT_BEFORE_CLOSE: Duration = Duration::from_secs(5);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Routers {
pub lease: Duration,
pub keep_alive: u32,
pub wait_before_close: Duration,
}
impl Default for Routers {
fn default() -> Self {
Routers {
lease: DEFAULT_LEASE,
keep_alive: DEFAULT_KEEP_ALIVE,
wait_before_close: DEFAULT_WAIT_BEFORE_CLOSE,
}
}
}
impl Routers {
#[must_use]
pub fn of(transport: &crate::link::Transport) -> Routers {
Routers {
lease: Duration::from_millis(transport.lease_ms),
keep_alive: u32::try_from(transport.keep_alive).unwrap_or(u32::MAX),
wait_before_close: Duration::from_millis(transport.wait_before_close_ms),
}
}
#[must_use]
pub fn stall_bound(&self) -> Duration {
stall_bound(self.wait_before_close, self.lease, self.keep_alive)
}
#[must_use]
pub fn presence_tail(&self, timeout: Duration) -> Duration {
presence_tail(self.lease, self.wait_before_close, timeout)
}
}
#[must_use]
pub fn stall_bound(wait_before_close: Duration, lease: Duration, keep_alive: u32) -> Duration {
let interval = lease.as_millis().div_ceil(u128::from(keep_alive.max(1)));
let interval = Duration::from_millis(u64::try_from(interval).unwrap_or(u64::MAX));
wait_before_close.min(lease.saturating_sub(interval))
}
#[must_use]
pub fn presence_tail(lease: Duration, wait_before_close: Duration, timeout: Duration) -> Duration {
lease.max(wait_before_close).saturating_add(timeout)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Delivery {
pub reliability: Reliability,
pub congestion: Congestion,
pub reader_lost: u64,
pub stall: ReaderStall,
pub miss_detection: MissDetection,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ReaderStall {
pub gap: Duration,
pub bound: Duration,
pub transport_closed: bool,
}
impl ReaderStall {
#[must_use]
pub fn stalled(&self) -> bool {
self.transport_closed || self.gap > self.bound
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct MissDetection {
pub declared: bool,
pub used: bool,
pub misses: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LossReason {
ReaderLost,
ReaderStalled,
MissesDetected,
BestEffort,
CongestionDrop,
ReliableBlock,
}
impl LossReason {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::ReaderLost => "reader_lost",
Self::ReaderStalled => "reader_stalled",
Self::MissesDetected => "misses_detected",
Self::BestEffort => "best_effort",
Self::CongestionDrop => "congestion_drop",
Self::ReliableBlock => "reliable_block",
}
}
#[must_use]
pub fn is_lossless(self) -> bool {
self == Self::ReliableBlock
}
}
impl fmt::Display for LossReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[must_use]
pub fn lossless(d: &Delivery) -> LossReason {
let m = d.miss_detection;
if d.reader_lost > 0 {
LossReason::ReaderLost
} else if d.stall.stalled() {
LossReason::ReaderStalled
} else if m.declared && m.used && m.misses > 0 {
LossReason::MissesDetected
} else if d.reliability == Reliability::BestEffort {
LossReason::BestEffort
} else if d.congestion == Congestion::Drop {
LossReason::CongestionDrop
} else {
LossReason::ReliableBlock
}
}
#[must_use]
pub fn delivery_of(
r: &Resource,
reader_lost: u64,
stall: ReaderStall,
misses: Option<u64>,
) -> Option<Delivery> {
match &r.body {
Body::Data(d) => Some(Delivery {
reliability: d.reliability,
congestion: d.congestion,
reader_lost,
stall,
miss_detection: MissDetection {
declared: d
.history
.as_ref()
.is_some_and(|h| h.miss_detection_ms.is_some()),
used: misses.is_some(),
misses: misses.unwrap_or(0),
},
}),
Body::Operation(_) => None,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Bound {
Untemplated,
NoCeiling,
Of(u64),
}
impl Bound {
#[must_use]
pub fn token(self) -> String {
match self {
Self::Untemplated => "untemplated".to_owned(),
Self::NoCeiling => "no_ceiling".to_owned(),
Self::Of(n) => n.to_string(),
}
}
#[must_use]
pub fn limit(self) -> Option<u64> {
match self {
Self::Of(n) => Some(n),
_ => None,
}
}
}
impl fmt::Display for Bound {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Untemplated => f.write_str("no bound (a template without parameters)"),
Self::NoCeiling => f.write_str("no ceiling (4294967295)"),
Self::Of(n) => write!(f, "{n}"),
}
}
}
#[must_use]
pub fn bound(contract: Option<u64>, stated: impl IntoIterator<Item = Option<u64>>) -> Bound {
let Some(c) = contract else {
return Bound::Untemplated;
};
let n = stated
.into_iter()
.map(|s| match s {
Some(n) if (1..=c).contains(&n) => n,
_ => c,
})
.max()
.unwrap_or(c);
if n == NO_CEILING {
Bound::NoCeiling
} else {
Bound::Of(n)
}
}
#[must_use]
pub fn bound_of(r: &Resource, stated: impl IntoIterator<Item = Option<u64>>) -> Bound {
bound(r.cardinality, stated)
}
#[must_use]
pub fn retention_of(r: &Resource) -> Option<Duration> {
match &r.body {
Body::Data(d) => d.retention_s.map(Duration::from_secs),
Body::Operation(_) => None,
}
}
#[must_use]
pub fn rate_of(r: &Resource) -> Option<Rate> {
match &r.body {
Body::Data(d) => d.rate,
Body::Operation(_) => None,
}
}
impl Rate {
#[must_use]
pub fn limit(self) -> (u32, Duration) {
const HOUR: Duration = Duration::from_secs(3600);
match self {
Self::Rare => (1, HOUR),
Self::Low => (1, Duration::from_secs(60)),
Self::Burst(n) => (n, HOUR),
}
}
}
#[must_use]
pub fn liveness(kind: Kind, retention: Option<Duration>) -> Option<Duration> {
match kind {
Kind::Stream => Some(STREAM_LIVENESS),
Kind::Event => retention,
Kind::State | Kind::Operation => None,
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Reading {
Get { members: u64, complete: bool },
Window(Window),
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Window {
pub heard: BTreeMap<String, Vec<Duration>>,
pub listened: Duration,
pub lossless: bool,
pub present: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Occurrences {
pub listened: Duration,
pub lossless: bool,
pub present: bool,
pub members: BTreeMap<String, Vec<Duration>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Verdict {
Exceeds,
Within,
Unobservable,
NotAsked,
}
impl Verdict {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::Exceeds => "exceeds",
Self::Within => "within",
Self::Unobservable => "unobservable",
Self::NotAsked => "not_asked",
}
}
}
impl fmt::Display for Verdict {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Reason {
OverBound,
RateExceeded,
Complete,
RateKept,
Empty,
Incomplete,
Lossy,
WindowTooShort,
OwnerAbsent,
ReadingKind,
NoCeiling,
Untemplated,
Kind,
NoRate,
}
impl Reason {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::OverBound => "over_bound",
Self::RateExceeded => "rate_exceeded",
Self::Complete => "complete",
Self::RateKept => "rate_kept",
Self::Empty => "empty",
Self::Incomplete => "incomplete",
Self::Lossy => "lossy",
Self::WindowTooShort => "window_too_short",
Self::OwnerAbsent => "owner_absent",
Self::ReadingKind => "reading_kind",
Self::NoCeiling => "no_ceiling",
Self::Untemplated => "untemplated",
Self::Kind => "kind",
Self::NoRate => "no_rate",
}
}
}
impl fmt::Display for Reason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Judged {
pub verdict: Verdict,
pub reason: Reason,
pub counted: u64,
}
impl Judged {
fn new(verdict: Verdict, reason: Reason, counted: u64) -> Self {
Self {
verdict,
reason,
counted,
}
}
}
#[must_use]
pub fn most_within(heard: &BTreeMap<String, Vec<Duration>>, period: Option<Duration>) -> u64 {
let Some(period) = period else {
return heard.values().filter(|v| !v.is_empty()).count() as u64;
};
let mut at: Vec<(Duration, &str)> = heard
.iter()
.flat_map(|(m, ts)| ts.iter().map(move |t| (*t, m.as_str())))
.collect();
at.sort();
let mut inside: BTreeMap<&str, usize> = BTreeMap::new();
let (mut best, mut end) = (0usize, 0usize);
for start in 0..at.len() {
while end < at.len() && at[end].0 < at[start].0 + period {
*inside.entry(at[end].1).or_default() += 1;
end += 1;
}
best = best.max(inside.len());
let m = at[start].1;
if let Some(n) = inside.get_mut(m) {
*n -= 1;
if *n == 0 {
inside.remove(m);
}
}
}
best as u64
}
#[must_use]
pub fn population(
kind: Kind,
bound: Bound,
retention: Option<Duration>,
reading: &Reading,
) -> Judged {
use Verdict::{Exceeds, NotAsked, Unobservable, Within};
if kind == Kind::Operation {
return Judged::new(NotAsked, Reason::Kind, 0);
}
let n = match bound {
Bound::Untemplated => return Judged::new(NotAsked, Reason::Untemplated, 0),
Bound::NoCeiling => return Judged::new(NotAsked, Reason::NoCeiling, 0),
Bound::Of(n) => n,
};
let span = liveness(kind, retention);
let (counted, short_of) = match (kind, reading) {
(Kind::State, Reading::Get { members, complete }) => {
(*members, (!complete).then_some(Reason::Incomplete))
}
(Kind::Stream | Kind::Event, Reading::Window(w)) => {
let short = span.is_none_or(|span| w.listened < span);
let why = if !w.lossless {
Some(Reason::Lossy)
} else if short {
Some(Reason::WindowTooShort)
} else if !w.present {
Some(Reason::OwnerAbsent)
} else {
None
};
(most_within(&w.heard, span), why)
}
_ => return Judged::new(Unobservable, Reason::ReadingKind, 0),
};
if counted > n {
return Judged::new(Exceeds, Reason::OverBound, counted);
}
if let Some(why) = short_of {
return Judged::new(Unobservable, why, counted);
}
if counted == 0 {
return Judged::new(Unobservable, Reason::Empty, 0);
}
Judged::new(Within, Reason::Complete, counted)
}
#[must_use]
pub fn rate(rate: Option<Rate>, o: &Occurrences) -> Judged {
use Verdict::{Exceeds, NotAsked, Unobservable, Within};
let Some(rate) = rate else {
return Judged::new(NotAsked, Reason::NoRate, 0);
};
let (n, period) = rate.limit();
let n = n as usize;
let mut most = 0usize;
for ts in o.members.values() {
let mut ts = ts.clone();
ts.sort();
let mut end = 0usize;
for start in 0..ts.len() {
while end < ts.len() && ts[end] < ts[start] + period {
end += 1;
}
most = most.max(end - start);
}
}
if most > n {
return Judged::new(Exceeds, Reason::RateExceeded, most as u64);
}
if !o.lossless {
return Judged::new(Unobservable, Reason::Lossy, most as u64);
}
if o.listened < period {
return Judged::new(Unobservable, Reason::WindowTooShort, most as u64);
}
if !o.present {
return Judged::new(Unobservable, Reason::OwnerAbsent, most as u64);
}
if most == 0 {
return Judged::new(Unobservable, Reason::Empty, 0);
}
Judged::new(Within, Reason::RateKept, most as u64)
}
#[cfg(test)]
mod tests {
use super::*;
fn s(secs: f64) -> Duration {
Duration::from_secs_f64(secs)
}
fn heard(of: &[(&str, &[f64])]) -> BTreeMap<String, Vec<Duration>> {
of.iter()
.map(|(m, ts)| ((*m).to_owned(), ts.iter().map(|t| s(*t)).collect()))
.collect()
}
#[test]
fn the_bound_is_the_greatest_valid_one_stated() {
assert_eq!(bound(None, []), Bound::Untemplated);
assert_eq!(bound(Some(64), []), Bound::Of(64));
assert_eq!(bound(Some(64), [Some(32)]), Bound::Of(32));
assert_eq!(bound(Some(64), [Some(16), Some(32)]), Bound::Of(32));
assert_eq!(bound(Some(64), [Some(32), None]), Bound::Of(64));
assert_eq!(bound(Some(64), [Some(128)]), Bound::Of(64), "D007's");
assert_eq!(bound(Some(64), [Some(0)]), Bound::Of(64), "D007's");
assert_eq!(bound(Some(NO_CEILING), []), Bound::NoCeiling);
assert_eq!(bound(Some(NO_CEILING), [Some(500)]), Bound::Of(500));
assert_eq!(
bound(Some(NO_CEILING), [Some(NO_CEILING)]),
Bound::NoCeiling
);
}
#[test]
fn a_window_counts_the_members_live_at_once() {
let h = heard(&[("a", &[0.0]), ("b", &[10.0]), ("c", &[4000.0])]);
assert_eq!(most_within(&h, Some(STREAM_LIVENESS)), 2);
assert_eq!(most_within(&h, None), 3);
let h = heard(&[("a", &[0.0]), ("b", &[3600.0])]);
assert_eq!(most_within(&h, Some(STREAM_LIVENESS)), 1);
let h = heard(&[("a", &[0.0, 1.0, 2.0]), ("b", &[1.5])]);
assert_eq!(most_within(&h, Some(STREAM_LIVENESS)), 2);
assert_eq!(most_within(&BTreeMap::new(), Some(STREAM_LIVENESS)), 0);
}
#[test]
fn population_poles() {
let get = |members, complete| Reading::Get { members, complete };
let j = |kind, bound, r: &Reading| {
let j = population(kind, bound, Some(s(60.0)), r);
(j.verdict, j.reason)
};
use Reason as R;
use Verdict as V;
assert_eq!(
j(Kind::State, Bound::Of(2), &get(3, false)),
(V::Exceeds, R::OverBound)
);
assert_eq!(
j(Kind::State, Bound::Of(2), &get(2, true)),
(V::Within, R::Complete)
);
assert_eq!(
j(Kind::State, Bound::Of(2), &get(2, false)),
(V::Unobservable, R::Incomplete)
);
assert_eq!(
j(Kind::State, Bound::Of(2), &get(0, true)),
(V::Unobservable, R::Empty)
);
assert_eq!(
j(Kind::State, Bound::NoCeiling, &get(9, true)),
(V::NotAsked, R::NoCeiling)
);
assert_eq!(
j(Kind::State, Bound::Of(2), &get(0, false)),
(V::Unobservable, R::Incomplete)
);
let w = |listened: f64, lossless, present, of: &[(&str, &[f64])]| {
Reading::Window(Window {
heard: heard(of),
listened: s(listened),
lossless,
present,
})
};
let two: &[(&str, &[f64])] = &[("a", &[0.0]), ("b", &[1.0])];
assert_eq!(
j(Kind::Stream, Bound::Of(1), &w(5.0, false, false, two)),
(V::Exceeds, R::OverBound)
);
assert_eq!(
j(Kind::Stream, Bound::Of(2), &w(3600.0, true, true, two)),
(V::Within, R::Complete)
);
assert_eq!(
j(Kind::Stream, Bound::Of(2), &w(3599.0, true, true, two)),
(V::Unobservable, R::WindowTooShort)
);
assert_eq!(
j(Kind::Stream, Bound::Of(2), &w(3600.0, false, true, two)),
(V::Unobservable, R::Lossy)
);
assert_eq!(
j(Kind::Stream, Bound::Of(2), &w(3600.0, true, false, two)),
(V::Unobservable, R::OwnerAbsent)
);
assert_eq!(
j(Kind::Stream, Bound::Of(2), &w(3600.0, true, true, &[])),
(V::Unobservable, R::Empty)
);
assert_eq!(
j(Kind::State, Bound::Of(2), &w(3600.0, true, true, two)),
(V::Unobservable, R::ReadingKind)
);
assert_eq!(
j(Kind::Operation, Bound::Of(2), &w(3600.0, true, true, two)),
(V::NotAsked, R::Kind)
);
let apart: &[(&str, &[f64])] = &[("a", &[0.0]), ("b", &[61.0])];
assert_eq!(
j(Kind::Event, Bound::Of(1), &w(120.0, true, true, apart)),
(V::Within, R::Complete)
);
assert_eq!(
j(Kind::Event, Bound::Of(1), &w(59.0, true, true, apart)),
(V::Unobservable, R::WindowTooShort)
);
}
#[test]
fn a_face_policys_transport_is_what_its_routers_run() {
let s = Duration::from_secs;
let t = crate::link::Transport {
lease_ms: 240_000,
keep_alive: 4,
wait_before_close_ms: 240_000,
sequence: crate::link::Sequence::Bit16,
batch_bytes: 222,
open_timeout_ms: 10_000,
accept_timeout_ms: 10_000,
interests_timeout_ms: 10_000,
};
let rf = Routers::of(&t);
assert_eq!(rf.wait_before_close, s(240));
assert_eq!(
rf.presence_tail(s(15)),
s(255),
"RF: 240 s and the reply timeout"
);
assert_eq!(
rf.stall_bound(),
s(180),
"the lease less one 60-s keep-alive"
);
let sbd = Routers::of(&crate::link::Transport {
lease_ms: 3_600_000,
keep_alive: 2,
wait_before_close_ms: 3_600_000,
..t
});
assert_eq!(sbd.stall_bound(), s(1800), "SBD's two keep-alives");
assert_eq!(
Routers::default().presence_tail(s(1)),
presence_tail(DEFAULT_LEASE, DEFAULT_WAIT_BEFORE_CLOSE, s(1)),
"given no policy, zenoh 1.10.1's"
);
}
#[test]
fn the_presence_tail_is_the_longer_of_the_lease_and_wait_before_close() {
let s = Duration::from_secs;
assert_eq!(
presence_tail(DEFAULT_LEASE, DEFAULT_WAIT_BEFORE_CLOSE, s(1)),
s(11),
"zenoh 1.10.1's defaults: the 10 s lease, then the timeout"
);
assert_eq!(
presence_tail(s(1), DEFAULT_WAIT_BEFORE_CLOSE, s(1)),
s(6),
"wait_before_close above the lease is the longer one: budget.md ยง1's 1 s lease"
);
assert_eq!(
presence_tail(s(1), s(10), s(1)),
s(11),
"zm-1.10.2-r1's 10 s wait_before_close beside a 1 s lease (#1011)"
);
assert_eq!(presence_tail(s(5), s(5), s(2)), s(7), "equal: either");
assert_eq!(
presence_tail(s(3), Duration::ZERO, Duration::ZERO),
s(3),
"no wait and no timeout: the lease alone"
);
}
#[test]
fn the_stall_bound_is_the_lesser_of_the_queue_and_the_lease() {
let s = Duration::from_secs;
assert_eq!(
stall_bound(DEFAULT_WAIT_BEFORE_CLOSE, DEFAULT_LEASE, DEFAULT_KEEP_ALIVE),
s(5),
"zenoh 1.10.1's defaults: 5 s, against 7.5 s for the lease"
);
assert_eq!(
stall_bound(s(10), DEFAULT_LEASE, DEFAULT_KEEP_ALIVE),
Duration::from_millis(7500),
"zm-1.10.2-r1's defaults: the lease's 7.5 s, against 10 s for the queue (#1011)"
);
assert_eq!(
stall_bound(DEFAULT_WAIT_BEFORE_CLOSE, s(1), DEFAULT_KEEP_ALIVE),
Duration::from_millis(750),
"a 1 s lease: its last keep-alive may be a quarter old"
);
assert_eq!(stall_bound(s(20), s(30), 3), s(20));
assert_eq!(
stall_bound(s(20), s(10), 0),
Duration::ZERO,
"keep_alive 0 as 1"
);
}
#[test]
fn lossless_rests_on_the_reader_and_the_qos() {
use LossReason as L;
let d = |reliability, congestion, reader_lost, declared, used, misses| Delivery {
reliability,
congestion,
reader_lost,
stall: ReaderStall {
gap: Duration::from_millis(50),
bound: DEFAULT_WAIT_BEFORE_CLOSE,
transport_closed: false,
},
miss_detection: MissDetection {
declared,
used,
misses,
},
};
let (re, be) = (Reliability::Reliable, Reliability::BestEffort);
let (block, drop) = (Congestion::Block, Congestion::Drop);
assert_eq!(
lossless(&d(re, block, 0, false, false, 0)),
L::ReliableBlock
);
assert!(L::ReliableBlock.is_lossless());
assert_eq!(lossless(&d(re, block, 1, false, false, 0)), L::ReaderLost);
assert_eq!(lossless(&d(be, drop, 0, false, false, 0)), L::BestEffort);
assert_eq!(lossless(&d(be, block, 0, false, false, 0)), L::BestEffort);
assert_eq!(
lossless(&d(re, drop, 0, false, false, 0)),
L::CongestionDrop
);
assert_eq!(lossless(&d(re, block, 0, true, true, 1)), L::MissesDetected);
assert_eq!(lossless(&d(be, drop, 0, true, true, 0)), L::BestEffort);
assert_eq!(lossless(&d(re, block, 0, false, true, 3)), L::ReliableBlock);
assert_eq!(lossless(&d(re, block, 0, true, false, 3)), L::ReliableBlock);
assert_eq!(lossless(&d(re, block, 2, true, true, 5)), L::ReaderLost);
let stalled = |gap_ms, closed, base: Delivery| Delivery {
stall: ReaderStall {
gap: Duration::from_millis(gap_ms),
bound: DEFAULT_WAIT_BEFORE_CLOSE,
transport_closed: closed,
},
..base
};
let clean = d(re, block, 0, false, false, 0);
assert_eq!(lossless(&stalled(50, true, clean)), L::ReaderStalled);
assert_eq!(lossless(&stalled(7000, false, clean)), L::ReaderStalled);
assert_eq!(lossless(&stalled(5000, false, clean)), L::ReliableBlock);
assert_eq!(
lossless(&stalled(7000, false, d(re, block, 1, false, false, 0))),
L::ReaderLost
);
assert_eq!(
lossless(&stalled(50, true, d(re, block, 0, true, true, 2))),
L::ReaderStalled
);
assert_eq!(
lossless(&stalled(50, true, d(be, drop, 0, false, false, 0))),
L::ReaderStalled
);
for r in [
L::ReaderLost,
L::ReaderStalled,
L::MissesDetected,
L::BestEffort,
L::CongestionDrop,
] {
assert!(!r.is_lossless(), "{r}");
}
}
#[test]
fn rate_poles() {
let o = |listened, lossless, of: &[(&str, &[f64])]| Occurrences {
listened: s(listened),
lossless,
present: true,
members: heard(of),
};
let j = |r, o: &Occurrences| {
let j = rate(r, o);
(j.verdict, j.reason)
};
use Reason as R;
use Verdict as V;
let low = Some(Rate::Low);
assert_eq!(
j(low, &o(5.0, true, &[("a", &[0.0, 59.9])])),
(V::Exceeds, R::RateExceeded)
);
assert_eq!(
j(low, &o(120.0, true, &[("a", &[0.0, 60.0])])),
(V::Within, R::RateKept)
);
assert_eq!(
j(low, &o(30.0, true, &[("a", &[0.0])])),
(V::Unobservable, R::WindowTooShort)
);
assert_eq!(
j(low, &o(120.0, false, &[("a", &[0.0])])),
(V::Unobservable, R::Lossy)
);
assert_eq!(j(low, &o(120.0, true, &[])), (V::Unobservable, R::Empty));
assert_eq!(
j(low, &o(120.0, true, &[("a", &[0.0]), ("b", &[1.0])])),
(V::Within, R::RateKept)
);
let burst = Some(Rate::Burst(3));
assert_eq!(
j(
burst,
&o(3600.0, true, &[("a", &[0.0, 1200.0, 2400.0, 3599.0])])
),
(V::Exceeds, R::RateExceeded)
);
assert_eq!(
j(
burst,
&o(3600.0, true, &[("a", &[0.0, 1200.0, 2400.0, 3600.0])])
),
(V::Within, R::RateKept)
);
assert_eq!(j(None, &o(120.0, true, &[])), (V::NotAsked, R::NoRate));
let absent = |listened, of: &[(&str, &[f64])]| Occurrences {
present: false,
..o(listened, true, of)
};
assert_eq!(
j(low, &absent(120.0, &[("a", &[0.0])])),
(V::Unobservable, R::OwnerAbsent)
);
assert_eq!(
j(low, &absent(120.0, &[])),
(V::Unobservable, R::OwnerAbsent)
);
assert_eq!(
j(low, &absent(30.0, &[("a", &[0.0])])),
(V::Unobservable, R::WindowTooShort)
);
assert_eq!(
j(low, &absent(120.0, &[("a", &[0.0, 1.0])])),
(V::Exceeds, R::RateExceeded)
);
}
}