use core::time::Duration;
use std::{thread, time::Instant};
use super::{
super::{Cadence, ItemId, Policies, Rings, Sweeper, Taps, Ticks, Wake, channel},
Clock, PollUpstream, Recurrence, Schedule, Seats, Swept, Unpolled,
};
fn shipped() -> Ticks {
Policies::new().ticks("clock", Taps::new())
}
fn due(schedule: &Schedule, start: Instant, at: Instant) -> Vec<PollUpstream> {
let mut clock = Clock::start(schedule, start);
let mut ticked = Vec::new();
clock.take_due(at, &mut ticked, &shipped());
ticked
}
fn ticks<K, T>(inbox: &super::super::Inbox<K, T>) -> Vec<Option<PollUpstream>> {
inbox
.drain()
.map(|wake| match wake {
Wake::Tick(tick) => Some(tick.upstream()),
Wake::Sweep | Wake::Delivered(_) => None,
})
.collect()
}
#[test]
fn a_poll_source_is_its_index_and_orders_by_it() {
assert_eq!(PollUpstream::new(2).index(), 2);
assert_eq!(PollUpstream::new(2).to_string(), "poll#2");
assert!(PollUpstream::new(0) < PollUpstream::new(1));
}
#[test]
fn a_schedule_is_its_sources_in_declaration_order() {
let schedule = Schedule::new()
.every(Duration::from_secs(600))
.every(Duration::from_secs(3_600));
assert_eq!(schedule.len(), 2);
assert!(!schedule.is_empty());
assert_eq!(
schedule.interval(PollUpstream::new(0)),
Some(Duration::from_secs(600))
);
assert_eq!(
schedule.interval(PollUpstream::new(1)),
Some(Duration::from_secs(3_600))
);
assert_eq!(schedule.interval(PollUpstream::new(2)), None);
assert!(Schedule::new().is_empty());
}
#[test]
fn a_clock_parks_until_the_nearest_source_or_the_sweep_whichever_is_sooner() {
let start = Instant::now();
let schedule = Schedule::new()
.every(Duration::from_millis(10))
.every(Duration::from_millis(100));
let clock = Clock::start(&schedule, start);
assert_eq!(
clock.park_for(Duration::from_secs(1), start),
Duration::from_millis(10),
"the nearest upstream is sooner than the sweep"
);
assert_eq!(
clock.park_for(Duration::from_millis(5), start),
Duration::from_millis(5),
"the sweep is sooner than any upstream"
);
assert_eq!(
Clock::start(&Schedule::new(), start).park_for(Duration::from_secs(1), start),
Duration::from_secs(1)
);
}
#[test]
fn each_source_comes_due_on_its_own_interval_and_never_on_anothers() {
let start = Instant::now();
let schedule = Schedule::new()
.every(Duration::from_millis(10))
.every(Duration::from_millis(100));
assert_eq!(
due(&schedule, start, start + Duration::from_millis(15)),
[PollUpstream::new(0)],
"only the fast upstream is due"
);
assert_eq!(
due(&schedule, start, start + Duration::from_millis(105)),
[PollUpstream::new(0), PollUpstream::new(1)],
"both are due once the slow one's interval has passed"
);
assert!(
due(&schedule, start, start + Duration::from_millis(5)).is_empty(),
"the first tick falls one interval after launch, as a sweeper's does"
);
}
#[test]
fn a_source_that_was_looked_at_late_is_one_look_and_not_a_burst_of_catch_up_ticks() {
let start = Instant::now();
let schedule = Schedule::new().every(Duration::from_millis(10));
let mut clock = Clock::start(&schedule, start);
let mut ticked = Vec::new();
clock.take_due(start + Duration::from_millis(35), &mut ticked, &shipped());
assert_eq!(ticked, [PollUpstream::new(0)]);
ticked.clear();
clock.take_due(start + Duration::from_millis(40), &mut ticked, &shipped());
assert!(ticked.is_empty());
clock.take_due(start + Duration::from_millis(46), &mut ticked, &shipped());
assert_eq!(ticked, [PollUpstream::new(0)]);
}
#[test]
fn a_bursting_source_is_one_tick_and_never_swallows_another_sources() {
let (out, inbox) = channel::<ItemId, u64>(8);
let fast = out.ticker(PollUpstream::new(0));
let slow = out.ticker(PollUpstream::new(1));
for _ in 0..1_000 {
assert!(fast.ring());
}
assert!(slow.ring());
assert_eq!(
ticks(&inbox),
[Some(PollUpstream::new(0)), Some(PollUpstream::new(1))],
"one look per upstream, and the slow upstream's is still there"
);
}
#[test]
fn taking_a_tick_frees_its_source_to_ring_again() {
let (out, inbox) = channel::<ItemId, u64>(8);
let ticker = out.ticker(PollUpstream::new(0));
assert!(ticker.ring());
assert!(ticker.ring(), "coalesced into the one already queued");
assert_eq!(inbox.len(), 1);
assert_eq!(ticks(&inbox), [Some(PollUpstream::new(0))]);
assert!(ticker.ring());
assert_eq!(ticks(&inbox), [Some(PollUpstream::new(0))], "and again");
}
#[test]
fn a_tick_with_nowhere_to_go_leaves_its_cell_as_it_found_it() {
let (out, inbox) = channel::<ItemId, u64>(1);
let ticker = out.ticker(PollUpstream::new(0));
assert!(out.nudge().ring(), "the one slot is taken by a sweep");
assert!(ticker.ring(), "no room, and the ringer is not blocked");
assert_eq!(inbox.len(), 1);
assert_eq!(ticks(&inbox), [None], "the sweep, and no tick");
assert!(ticker.ring());
assert_eq!(ticks(&inbox), [Some(PollUpstream::new(0))]);
}
#[test]
fn a_ticker_clone_is_the_same_source_sharing_the_same_cell() {
let (out, inbox) = channel::<ItemId, u64>(8);
let ticker = out.ticker(PollUpstream::new(3));
let twin = ticker.clone();
assert_eq!(twin.upstream(), PollUpstream::new(3));
assert!(ticker.ring());
assert!(twin.ring());
assert_eq!(ticks(&inbox), [Some(PollUpstream::new(3))]);
}
#[test]
fn a_ticker_at_a_gone_consumer_reports_that_it_is_gone() {
let (out, inbox) = channel::<ItemId, u64>(8);
let ticker = out.ticker(PollUpstream::new(0));
drop(inbox);
assert!(!ticker.ring());
assert!(!ticker.ring());
}
#[test]
fn a_schedules_cadences_pair_each_source_with_its_own_interval() {
let (out, inbox) = channel::<ItemId, u64>(8);
let schedule = Schedule::new()
.every(Duration::from_millis(10))
.every(Duration::from_millis(100));
let cadences = schedule.cadences(&out);
assert_eq!(cadences.len(), 2);
assert_eq!(cadences[0].until_next(), Some(Duration::from_millis(10)));
assert_eq!(cadences[1].until_next(), Some(Duration::from_millis(100)));
assert!(cadences[1].ring());
assert_eq!(ticks(&inbox), [Some(PollUpstream::new(1))]);
}
#[test]
fn a_sweeper_rings_each_cadence_at_its_own_rate_from_one_thread() {
let (fast_out, fast_in) = channel::<ItemId, u64>(256);
let (slow_out, slow_in) = channel::<ItemId, u64>(256);
let sweeper = Sweeper::ticking(vec![
Cadence::new(Duration::from_millis(5), Box::new(fast_out.nudge())),
Cadence::new(Duration::from_millis(80), Box::new(slow_out.nudge())),
]);
thread::sleep(Duration::from_millis(250));
drop(sweeper);
let fast = fast_in.len();
let slow = slow_in.len();
assert!(fast >= 5, "the fast cadence rang {fast} times");
assert!(slow >= 1, "the slow cadence rang at all: {slow}");
assert!(
fast > slow,
"each cadence keeps its own rate: fast {fast}, slow {slow}"
);
drop((fast_out, slow_out));
}
#[test]
fn a_sweeper_with_no_cadence_stops_rather_than_waiting_for_one() {
drop(Sweeper::ticking(Vec::new()));
}
#[tokio::test]
async fn a_unit_that_declared_no_poll_source_sweeps_nothing() {
struct Titling;
let mut node = Titling;
let mut absent = Unpolled;
assert_eq!(
absent.seat(&mut node, PollUpstream::new(0)).await,
Ok(Swept::Ran(()))
);
}
#[test]
fn a_schedule_mixes_intervals_and_calendars() {
let schedule = Schedule::new()
.every(Duration::from_secs(600))
.cron("0 3 * * *")
.expect("a nightly expression")
.cron_in("*/15 * * * *", "Etc/UTC")
.expect("a quarter-hourly expression in a named zone");
assert_eq!(schedule.len(), 3);
assert_eq!(
schedule.interval(PollUpstream::new(0)),
Some(Duration::from_secs(600))
);
assert_eq!(schedule.interval(PollUpstream::new(1)), None);
assert!(matches!(
schedule.recurrence(PollUpstream::new(1)),
Some(Recurrence::Calendar(_))
));
let Some(Recurrence::Calendar(calendar)) = schedule.recurrence(PollUpstream::new(2)) else {
panic!("the third entry keeps a calendar");
};
assert_eq!(calendar.expression(), "*/15 * * * *");
assert!(schedule.recurrence(PollUpstream::new(3)).is_none());
}
#[test]
fn a_schedule_refuses_an_expression_that_is_not_one() {
assert!(Schedule::new().cron("99 3 * * *").is_err());
assert!(Schedule::new().cron_in("0 3 * * *", "Not/AZone").is_err());
}
#[test]
fn one_next_due_core_answers_for_both_arms() {
assert_eq!(
Recurrence::Every(Duration::from_secs(600)).until_next(),
Some(Duration::from_secs(600))
);
let quarterly = Recurrence::Calendar(
super::Calendar::parse_in("*/15 * * * *", "Etc/UTC").expect("a quarter-hourly expression"),
);
let wait = quarterly.until_next().expect("always due again");
assert!(wait <= Duration::from_secs(15 * 60), "{wait:?}");
}
#[test]
fn a_clock_parks_on_whichever_cadence_is_nearest_across_both_arms() {
let start = Instant::now();
let schedule = Schedule::new()
.cron_in("*/15 * * * *", "Etc/UTC")
.expect("a quarter-hourly expression")
.every(Duration::from_millis(10));
let clock = Clock::start(&schedule, start);
assert_eq!(
clock.park_for(Duration::from_secs(60), start),
Duration::from_millis(10)
);
assert_eq!(
due(&schedule, start, start + Duration::from_millis(20)),
[PollUpstream::new(1)]
);
}
#[test]
fn a_schedules_cadences_carry_both_arms() {
let (out, inbox) = channel::<ItemId, u64>(8);
let schedule = Schedule::new()
.every(Duration::from_millis(10))
.cron_in("0 3 * * *", "Etc/UTC")
.expect("a nightly expression");
let cadences = schedule.cadences(&out);
assert_eq!(cadences.len(), 2);
assert!(matches!(cadences[0].recurrence(), Recurrence::Every(_)));
assert!(matches!(cadences[1].recurrence(), Recurrence::Calendar(_)));
assert!(cadences[1].ring());
assert_eq!(ticks(&inbox), [Some(PollUpstream::new(1))]);
}