use core::{future::pending, time::Duration};
use std::{thread, time::Instant};
use super::{Arm, Intake, arm, park_on, select};
use crate::{Delivery, Item, ItemId, Payload, Wake, ack_channel, channel};
async fn ready<T>(value: T) -> T {
value
}
#[test]
fn a_select_over_nothing_is_the_honest_no_source_left() {
let mut arms: [Arm<'_, u8>; 0] = [];
assert_eq!(
park_on(select(0, &mut arms), Duration::from_millis(50)),
Some(None)
);
}
#[test]
fn a_select_answers_the_first_ready_arm_and_says_which() {
let mut first = core::pin::pin!(pending::<u8>());
let mut second = core::pin::pin!(ready(7u8));
let mut third = core::pin::pin!(ready(9u8));
let mut arms: [Arm<'_, u8>; 3] = [first.as_mut(), second.as_mut(), third.as_mut()];
assert_eq!(
park_on(select(0, &mut arms), Duration::ZERO),
Some(Some((1, 7)))
);
}
#[test]
fn the_turn_is_what_keeps_a_busy_arm_from_starving_a_quiet_one() {
let mut left = core::pin::pin!(ready(1u8));
let mut right = core::pin::pin!(ready(2u8));
let mut arms: [Arm<'_, u8>; 2] = [left.as_mut(), right.as_mut()];
assert_eq!(
park_on(select(0, &mut arms), Duration::ZERO),
Some(Some((0, 1)))
);
let mut left = core::pin::pin!(ready(1u8));
let mut right = core::pin::pin!(ready(2u8));
let mut arms: [Arm<'_, u8>; 2] = [left.as_mut(), right.as_mut()];
assert_eq!(
park_on(select(1, &mut arms), Duration::ZERO),
Some(Some((1, 2)))
);
}
#[test]
fn a_closed_arm_never_answers_so_a_hung_up_source_cannot_spin_the_select() {
let (outbox, inbox) = channel::<ItemId, u8>(4);
drop(outbox);
let mut closed = core::pin::pin!(arm(false, &inbox, core::convert::identity));
let mut arms: [Arm<'_, Option<Wake<ItemId, u8>>>; 1] = [closed.as_mut()];
assert!(
park_on(select(0, &mut arms), Duration::from_millis(30)).is_none(),
"the deadline expires, which is what a park with nothing to hear does"
);
}
#[test]
fn an_arm_answers_none_when_its_own_source_hangs_up() {
let (outbox, inbox) = channel::<ItemId, u8>(4);
drop(outbox);
let mut open = core::pin::pin!(arm(true, &inbox, core::convert::identity));
let mut arms: [Arm<'_, Option<Wake<ItemId, u8>>>; 1] = [open.as_mut()];
let answered = park_on(select(0, &mut arms), Duration::from_millis(200));
assert!(matches!(answered, Some(Some((0, None)))));
}
#[test]
fn a_park_that_times_out_leaves_the_message_where_the_next_park_finds_it() {
let (outbox, inbox) = channel::<ItemId, u8>(4);
let (ack, _answers) = ack_channel::<ItemId>();
let waited = park_on(inbox.park_async(), Duration::from_millis(30));
assert!(waited.is_none(), "nothing had arrived yet");
outbox
.deliver(Delivery::new(
Payload::One(Item::new(ItemId::new(0), 42u8)),
ack,
))
.expect("the consumer is here");
let Some(Some(Wake::Delivered(delivery))) = park_on(inbox.park_async(), Duration::from_secs(1))
else {
panic!("the next park sees it");
};
assert_eq!(delivery.payload().len(), 1);
}
#[test]
fn a_park_is_woken_by_a_send_from_another_thread_rather_than_by_its_deadline() {
let (outbox, inbox) = channel::<ItemId, u8>(4);
let (ack, _answers) = ack_channel::<ItemId>();
let sender = thread::spawn(move || {
thread::sleep(Duration::from_millis(20));
outbox
.deliver(Delivery::new(
Payload::One(Item::new(ItemId::new(1), 7u8)),
ack,
))
.expect("the consumer is here");
});
let started = Instant::now();
let woken = park_on(inbox.park_async(), Duration::from_secs(30));
assert!(matches!(woken, Some(Some(Wake::Delivered(_)))));
assert!(
started.elapsed() < Duration::from_secs(5),
"the wake resumed it, not the clock"
);
sender.join().expect("the sender");
}
#[test]
fn the_one_source_intake_is_the_inbox_itself() {
let (outbox, mut inbox) = channel::<ItemId, u8>(4);
let (ack, _answers) = ack_channel::<ItemId>();
outbox
.deliver(Delivery::new(
Payload::One(Item::new(ItemId::new(0), 5u8)),
ack,
))
.expect("the consumer is here");
let woken = Intake::park(&mut inbox, Duration::from_secs(1));
assert!(matches!(woken, Some(Wake::Delivered(_))));
let mut into = Vec::new();
Intake::drain(&mut inbox, &mut into);
assert!(into.is_empty(), "nothing else was queued");
drop(outbox);
assert!(
Intake::park(&mut inbox, Duration::from_secs(1)).is_none(),
"every sender gone is shutdown"
);
}