//! The program's transport (ADR 0072, *Lifecycle*): started from an actor, //! it switches the clock to real time and counts as something that can //! still wake an actor; the scheduler stops it, joining its thread, before //! its shared state is dropped. use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use louise_rt_prims::wire::MessageHeader; use crate::abi::{Ctx, UNIT, Value}; use crate::transport::{Config, Inbound, Node, Waiter, start_program, with_program}; use super::super::{Exit, this}; use super::{THREADS, run_root}; /// Set when the handler is dropped: with the transport's thread. static DROPPED: AtomicBool = AtomicBool::new(false); struct Witness; impl Inbound for Witness { fn node_up(&self, _: &Node) {} fn node_down(&self, _: &Node) {} fn message(&self, _: &Node, _: &MessageHeader, _: &[u8]) {} fn signal(&self, _: &Node, _: &[u8]) {} fn drained(&self, _: &Node, _: &[Waiter]) {} } impl Drop for Witness { fn drop(&mut self) { DROPPED.store(true, Ordering::SeqCst); } } fn config() -> Config { let fixture = |name: &str| { format!( "{}/../../crates/louise/tests/fixtures/tls/{name}", env!("CARGO_MANIFEST_DIR") ) }; let mut c = Config::new( "node-a.cluster.test", "test", "127.0.0.1:0".parse().expect("an address"), ); c.cert_file = fixture("node-a.pem"); c.key_file = fixture("node-a.key"); c.ca_files = vec![fixture("ca.pem")]; c } unsafe extern "C" fn starts_distribution(ctx: *mut Ctx) -> Value { // SAFETY: the root actor's entry. let (_, sched) = unsafe { this(ctx) }; assert!(!sched.real_time() && !crate::dist::running()); start_program(sched, config(), Arc::new(Witness)).expect("distribution starts"); assert!(sched.real_time(), "peers keep real time"); assert!(crate::dist::running(), "the deadlock check counts it"); let again = start_program(sched, config(), Arc::new(Witness)); assert!(again.is_err_and(|e| e.contains("already"))); assert_eq!(with_program(|t| t.connections()), Some(0)); UNIT } unsafe extern "C" fn starts_and_ends(ctx: *mut Ctx) -> Value { // SAFETY: the root actor's entry. let (_, sched) = unsafe { this(ctx) }; start_program(sched, config(), Arc::new(Witness)).expect("distribution starts"); UNIT } /// One test: the transport is global, and another test's program could /// start one between a run and the checks after it. #[test] fn the_scheduler_stops_the_transport() { for threads in THREADS { DROPPED.store(false, Ordering::SeqCst); assert_eq!(run_root(threads, starts_distribution), Exit::Normal); assert!(DROPPED.load(Ordering::SeqCst), "the thread was joined"); assert!(!crate::dist::running()); assert!(with_program(|_| ()).is_none()); } // The root's exit races a thread that starts idling: with // distribution running there is no deadlock check to end that // thread's wait, so it must see the program finished. let (tx, rx) = std::sync::mpsc::channel(); std::thread::spawn(move || { for _ in 0..200 { assert_eq!(run_root(4, starts_and_ends), Exit::Normal); } let _ = tx.send(()); }); rx.recv_timeout(std::time::Duration::from_secs(60)) .expect("every run ends"); }