use core::time::Duration;
use std::sync::{
Arc,
atomic::{AtomicUsize, Ordering},
};
use agnostic_lite::{RuntimeLite, tokio::TokioRuntime};
use super::{
super::{Cadence, ItemId, Rings, channel, launch::Ignition},
AfterClock, Unruntimed, engage,
};
type After = <TokioRuntime as RuntimeLite>::AfterSpawner;
struct Counting(Arc<AtomicUsize>);
impl Rings for Counting {
fn ring(&self) -> bool {
self.0.fetch_add(1, Ordering::SeqCst);
true
}
}
struct Departed;
impl Rings for Departed {
fn ring(&self) -> bool {
false
}
}
fn cadence(every: Duration, ringer: impl Rings + 'static) -> Cadence {
Cadence::new(every, Box::new(ringer))
}
#[tokio::test]
async fn the_after_clock_rings_each_cadence_on_its_own_interval() {
let fast = Arc::new(AtomicUsize::new(0));
let slow = Arc::new(AtomicUsize::new(0));
let clock = AfterClock::start::<After>(vec![
cadence(Duration::from_millis(10), Counting(Arc::clone(&fast))),
cadence(Duration::from_millis(100), Counting(Arc::clone(&slow))),
]);
assert_eq!(clock.hands(), 2);
tokio::time::sleep(Duration::from_millis(120)).await;
let (fast, slow) = (fast.load(Ordering::SeqCst), slow.load(Ordering::SeqCst));
assert!(fast >= 5, "the fast hand re-armed itself: {fast}");
assert!(fast > slow, "and it outran the slow one: {fast} vs {slow}");
}
#[tokio::test]
async fn dropping_the_after_clock_releases_the_ringers_at_once() {
let (out, inbox) = channel::<ItemId, ()>(4);
let clock = AfterClock::start::<After>(vec![cadence(Duration::from_secs(3600), out.nudge())]);
drop(out);
assert!(
!inbox.is_disconnected(),
"the clock's own hand is holding it open"
);
drop(clock);
assert!(
inbox.is_disconnected(),
"the drop released the ringer, rather than the fire an hour from now"
);
}
#[tokio::test]
async fn a_cadence_whose_far_end_is_gone_stops_re_arming() {
let live = Arc::new(AtomicUsize::new(0));
let clock = AfterClock::start::<After>(vec![
cadence(Duration::from_millis(10), Departed),
cadence(Duration::from_millis(10), Counting(Arc::clone(&live))),
]);
tokio::time::sleep(Duration::from_millis(60)).await;
assert!(
live.load(Ordering::SeqCst) >= 3,
"the survivor is still rung: cadences registered together do not die together"
);
drop(clock);
}
#[tokio::test]
async fn an_engaged_body_is_spawned_and_its_join_awaits_it() {
let ran = Arc::new(AtomicUsize::new(0));
let counted = Arc::clone(&ran);
let (started, running) = flume::bounded::<()>(1);
let join = engage::<<TokioRuntime as RuntimeLite>::Spawner, _>(async move {
running.recv_async().await.expect("the go-ahead");
counted.fetch_add(1, Ordering::SeqCst);
});
assert_eq!(
ran.load(Ordering::SeqCst),
0,
"it is spawned and waiting, which is what makes the join worth holding"
);
started.send(()).expect("the body is listening");
join.await;
assert_eq!(
ran.load(Ordering::SeqCst),
1,
"awaiting the join means the body is finished, not merely started"
);
}
const _: fn(Ignition<Unruntimed>) -> Ignition = core::convert::identity;
#[test]
fn the_absence_of_a_runtime_is_a_marker() {
assert_eq!(core::mem::size_of::<Unruntimed>(), 0);
assert_eq!(Unruntimed, Unruntimed);
}