use crate::Context;
use core::{convert::Infallible, sync::atomic::Ordering, time::Duration};
use std::sync::{Arc, atomic::AtomicBool};
use jiff::SignedDuration;
use crate::{
Async, AsyncSource, Block, Buffered, Control, Declared, DeclaredOrigin, Delivery, Effect,
Emitter, Enablement, Feed, Fin, Operator, Resources, Source, Unchanneled, Upstream,
};
const PATIENCE: Duration = Duration::from_secs(30);
type Wound = Arc<AtomicBool>;
struct Parking {
wound: Wound,
}
impl Source for Parking {
type Options = Wound;
type Yield = u64;
type Mutation = ();
type Error = Infallible;
fn build(_ctx: &Resources, wound: Wound) -> Result<Self, Self::Error> {
Ok(Self { wound })
}
fn run(
self,
ctx: Context,
_giving: Feed<u64>,
_eating: Control<Infallible>,
) -> Result<Fin, Self::Error> {
ctx.shutdown().heard();
self.wound.store(true, Ordering::Release);
Ok(Fin::Stopped)
}
}
impl DeclaredOrigin for Parking {
type Mode = Block;
}
struct Looking {
wound: Wound,
}
impl Source for Looking {
type Options = Wound;
type Yield = u64;
type Mutation = ();
type Error = Infallible;
fn build(_ctx: &Resources, wound: Wound) -> Result<Self, Self::Error> {
Ok(Self { wound })
}
fn run(
self,
ctx: Context,
_giving: Feed<u64>,
_eating: Control<Infallible>,
) -> Result<Fin, Self::Error> {
while !ctx.shutdown().is_shutdown() {
std::thread::sleep(Duration::from_millis(1));
}
self.wound.store(true, Ordering::Release);
Ok(Fin::Stopped)
}
}
impl DeclaredOrigin for Looking {
type Mode = Block;
}
struct Awaiting {
wound: Wound,
}
impl AsyncSource for Awaiting {
type Options = Wound;
type Yield = u64;
type Mutation = ();
type Error = Infallible;
async fn build(_ctx: &Resources, wound: Wound) -> Result<Self, Self::Error> {
Ok(Self { wound })
}
async fn run(
self,
ctx: Context,
_giving: Feed<u64>,
_eating: Control<Infallible>,
) -> Result<Fin, Self::Error> {
ctx.shutdown().heard_async().await;
self.wound.store(true, Ordering::Release);
Ok(Fin::Stopped)
}
}
impl DeclaredOrigin for Awaiting {
type Mode = Async;
}
struct Ledger;
impl Operator for Ledger {
type Error = Infallible;
type In = u64;
type Out = u64;
fn accept(
&mut self,
_ctx: &Context,
_delivery: Delivery<u64>,
_emitter: &mut Emitter<u64>,
) -> Result<(), Self::Error> {
Ok(())
}
}
impl Declared for Ledger {
const EFFECT: Effect = Effect::Lands;
const HOLDS_LANE: bool = true;
const UPSTREAMS: &'static [Upstream] = &[
Upstream::of::<Parking>("crate::Parking"),
Upstream::of::<Looking>("crate::Looking"),
Upstream::of::<Awaiting>("crate::Awaiting"),
];
type Channels = Unchanneled;
type Mode = Block;
type Options = ();
type Pair = Buffered;
type Wires<O> = crate::Downstream<O>;
type Attribution<O> = ();
}
fn wired() -> crate::Wired {
let mut blueprint = crate::Blueprint::new();
let _ = blueprint.origin::<Parking>("parking");
let _ = blueprint.origin::<Looking>("looking");
let _ = blueprint.origin::<Awaiting>("awaiting");
let _ = blueprint.node::<Ledger>("ledger");
blueprint
.into_plan()
.expect("the edges join on the origins' own identities")
.with_enablement(
Enablement::new()
.launching("parking")
.launching("looking")
.launching("awaiting"),
)
.validate()
.expect("the deployment checks out")
}
fn flag() -> Wound {
Arc::new(AtomicBool::new(false))
}
#[test]
fn an_infinite_origin_is_wound_down_by_the_worlds_own_shutdown() {
let (parked, looked, awaited) = (flag(), flag(), flag());
let plan = wired();
let mut wiring = plan.create().wire();
let (one, two, three) = (
wiring.enrol("parking"),
wiring.enrol("looking"),
wiring.enrol("awaiting"),
);
let mut ignition = wiring.into_assembly().ignite();
let parking = ignition
.origin_declared::<Parking>(
"parking",
Parking {
wound: Arc::clone(&parked),
},
4,
one,
)
.expect("its engine");
let looking = ignition
.origin_declared::<Looking>(
"looking",
Looking {
wound: Arc::clone(&looked),
},
4,
two,
)
.expect("its engine");
let awaiting = ignition
.origin_declared::<Awaiting>(
"awaiting",
Awaiting {
wound: Arc::clone(&awaited),
},
4,
three,
)
.expect("its engine");
let stopped = ignition.into_app::<()>().shutdown(Some(
SignedDuration::try_from(PATIENCE).expect("a bounded wait"),
));
assert!(
!stopped.overran(),
"the wind-down reached every origin: {stopped}"
);
assert!(stopped.is_clean(), "{stopped}");
assert_eq!(stopped.settled(), 3, "{stopped}");
assert!(parked.load(Ordering::Acquire), "the park was released");
assert!(looked.load(Ordering::Acquire), "the look answered");
assert!(
awaited.load(Ordering::Acquire),
"the awaited park was released"
);
assert!(!parking.stopper().announce(), "its life is over");
assert!(!looking.stopper().announce(), "its life is over");
assert!(!awaiting.stopper().announce(), "its life is over");
}
#[test]
fn dropping_the_handle_stops_an_origin_while_the_world_runs() {
let wound = flag();
let plan = wired();
let mut wiring = plan.create().wire();
let enrolled = wiring.enrol("parking");
let mut ignition = wiring.into_assembly().ignite();
let parking = ignition
.origin_declared::<Parking>(
"parking",
Parking {
wound: Arc::clone(&wound),
},
4,
enrolled,
)
.expect("its engine");
let app = ignition.into_app::<()>();
drop(parking);
let end = std::time::Instant::now() + PATIENCE;
while !app.parties()[0].finished() {
assert!(
std::time::Instant::now() < end,
"the handle's own drop is what stops an origin the world has not stopped"
);
std::thread::sleep(Duration::from_millis(1));
}
assert!(wound.load(Ordering::Acquire), "the park was released");
let stopped = app.shutdown(Some(
SignedDuration::try_from(PATIENCE).expect("a bounded wait"),
));
assert!(stopped.is_clean(), "{stopped}");
}