use super::*;
use std::sync::mpsc::channel;
struct A;
impl Probe for A {
type Event = u8;
}
struct B;
impl Probe for B {
type Event = &'static str;
}
#[skuld::test]
fn a_notification_reaches_the_installed_channel() {
let (tx, rx) = channel();
let _guard = install::<A>(tx);
notify::<A>(7);
assert_eq!(rx.try_iter().collect::<Vec<_>>(), [7]);
}
#[skuld::test]
fn a_guard_uninstalls_on_drop() {
let (tx, rx) = channel();
drop(install::<A>(tx));
notify::<A>(1);
assert_eq!(rx.try_iter().count(), 0);
assert!(!is_installed::<A>());
}
#[cfg(debug_assertions)]
#[skuld::test]
#[should_panic(expected = "nested on the same thread")]
fn installing_twice_on_one_thread_panics() {
let (tx, _rx) = channel();
let _outer = install::<A>(tx.clone());
let _inner = install::<A>(tx);
}
#[skuld::test]
fn distinct_probes_do_not_share_a_slot() {
let (a_tx, a_rx) = channel();
let (b_tx, b_rx) = channel();
let _a = install::<A>(a_tx);
let _b = install::<B>(b_tx);
notify::<A>(1);
notify::<B>("b");
assert_eq!(a_rx.try_iter().collect::<Vec<_>>(), [1]);
assert_eq!(b_rx.try_iter().collect::<Vec<_>>(), ["b"]);
}
#[skuld::test]
fn another_thread_does_not_see_this_threads_probe() {
let (tx, rx) = channel();
let _guard = install::<A>(tx);
std::thread::scope(|s| {
s.spawn(|| {
assert!(!is_installed::<A>());
notify::<A>(1);
});
});
assert_eq!(rx.try_iter().count(), 0);
}
#[skuld::test]
fn a_relay_carries_every_probe_for_the_guards_scope() {
let (a_tx, a_rx) = channel();
let (b_tx, b_rx) = channel();
let _a = install::<A>(a_tx);
let _b = install::<B>(b_tx);
let relay = capture();
std::thread::scope(|s| {
s.spawn(move || {
assert!(!is_installed::<A>() && !is_installed::<B>());
let guard = relay.reinstall();
notify::<A>(2);
notify::<B>("relayed");
drop(guard);
assert!(
!is_installed::<A>() && !is_installed::<B>(),
"the relay must not outlive its guard"
);
});
});
assert_eq!(a_rx.try_iter().collect::<Vec<_>>(), [2]);
assert_eq!(b_rx.try_iter().collect::<Vec<_>>(), ["relayed"]);
}
#[skuld::test]
fn capturing_leaves_the_arming_threads_probe_installed() {
let (tx, rx) = channel();
let _guard = install::<A>(tx);
drop(capture());
notify::<A>(3);
assert_eq!(rx.try_iter().collect::<Vec<_>>(), [3]);
}
#[skuld::test]
fn a_rejected_nested_install_leaves_the_outer_channel_receiving() {
let (outer_tx, outer_rx) = channel();
let (inner_tx, inner_rx) = channel();
let _outer = install::<A>(outer_tx);
let nested = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| drop(install::<A>(inner_tx))));
assert_eq!(nested.is_err(), cfg!(debug_assertions));
notify::<A>(5);
assert_eq!(outer_rx.try_iter().collect::<Vec<_>>(), [5]);
assert_eq!(inner_rx.try_iter().count(), 0);
}
#[skuld::test]
fn a_reinstall_onto_an_occupied_slot_touches_only_what_it_inserted() {
let (a_tx, a_rx) = channel::<u8>();
let (b_relay_tx, b_relay_rx) = channel::<&'static str>();
let (b_orig_tx, b_orig_rx) = channel::<&'static str>();
let relay = Relay(vec![
(TypeId::of::<A>(), Box::new(a_tx) as Box<dyn Entry>),
(TypeId::of::<B>(), Box::new(b_relay_tx) as Box<dyn Entry>),
]);
std::thread::scope(|s| {
s.spawn(move || {
let _b = install::<B>(b_orig_tx);
let reinstalled = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| relay.reinstall()));
assert_eq!(reinstalled.is_err(), cfg!(debug_assertions));
drop(reinstalled);
assert!(!is_installed::<A>(), "the inserted entry must be uninstalled");
notify::<B>("original");
});
});
assert_eq!(a_rx.try_iter().count(), 0);
assert_eq!(b_relay_rx.try_iter().count(), 0);
assert_eq!(b_orig_rx.try_iter().collect::<Vec<_>>(), ["original"]);
}
#[skuld::test]
fn a_slot_holding_the_wrong_sender_type_panics() {
let (wrong_tx, _wrong_rx) = channel::<&'static str>();
assert!(insert(TypeId::of::<A>(), Box::new(wrong_tx)));
let current = std::panic::catch_unwind(current::<A>);
SLOTS.with(|slots| slots.borrow_mut().remove(&TypeId::of::<A>()));
assert!(current.is_err(), "a mistyped slot must panic, not read as absent");
}