tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

97 lignes · 3374 octetsBrut · Historique
1//! The program's transport (ADR 0072, *Lifecycle*): started from an actor,
2//! it switches the clock to real time and counts as something that can
3//! still wake an actor; the scheduler stops it, joining its thread, before
4//! its shared state is dropped.
5
6use std::sync::Arc;
7use std::sync::atomic::{AtomicBool, Ordering};
8
9use louise_rt_prims::wire::MessageHeader;
10
11use crate::abi::{Ctx, UNIT, Value};
12use crate::transport::{Config, Inbound, Node, Waiter, start_program, with_program};
13
14use super::super::{Exit, this};
15use super::{THREADS, run_root};
16
17/// Set when the handler is dropped: with the transport's thread.
18static DROPPED: AtomicBool = AtomicBool::new(false);
19
20struct Witness;
21
22impl Inbound for Witness {
23 fn node_up(&self, _: &Node) {}
24 fn node_down(&self, _: &Node) {}
25 fn message(&self, _: &Node, _: &MessageHeader, _: &[u8]) {}
26 fn signal(&self, _: &Node, _: &[u8]) {}
27 fn drained(&self, _: &Node, _: &[Waiter]) {}
28}
29
30impl Drop for Witness {
31 fn drop(&mut self) {
32 DROPPED.store(true, Ordering::SeqCst);
33 }
34}
35
36fn config() -> Config {
37 let fixture = |name: &str| {
38 format!(
39 "{}/../../crates/louise/tests/fixtures/tls/{name}",
40 env!("CARGO_MANIFEST_DIR")
41 )
42 };
43 let mut c = Config::new(
44 "node-a.cluster.test",
45 "test",
46 "127.0.0.1:0".parse().expect("an address"),
47 );
48 c.cert_file = fixture("node-a.pem");
49 c.key_file = fixture("node-a.key");
50 c.ca_files = vec![fixture("ca.pem")];
51 c
52}
53
54unsafe extern "C" fn starts_distribution(ctx: *mut Ctx) -> Value {
55 // SAFETY: the root actor's entry.
56 let (_, sched) = unsafe { this(ctx) };
57 assert!(!sched.real_time() && !crate::dist::running());
58 start_program(sched, config(), Arc::new(Witness)).expect("distribution starts");
59 assert!(sched.real_time(), "peers keep real time");
60 assert!(crate::dist::running(), "the deadlock check counts it");
61 let again = start_program(sched, config(), Arc::new(Witness));
62 assert!(again.is_err_and(|e| e.contains("already")));
63 assert_eq!(with_program(|t| t.connections()), Some(0));
64 UNIT
65}
66
67unsafe extern "C" fn starts_and_ends(ctx: *mut Ctx) -> Value {
68 // SAFETY: the root actor's entry.
69 let (_, sched) = unsafe { this(ctx) };
70 start_program(sched, config(), Arc::new(Witness)).expect("distribution starts");
71 UNIT
72}
73
74/// One test: the transport is global, and another test's program could
75/// start one between a run and the checks after it.
76#[test]
77fn the_scheduler_stops_the_transport() {
78 for threads in THREADS {
79 DROPPED.store(false, Ordering::SeqCst);
80 assert_eq!(run_root(threads, starts_distribution), Exit::Normal);
81 assert!(DROPPED.load(Ordering::SeqCst), "the thread was joined");
82 assert!(!crate::dist::running());
83 assert!(with_program(|_| ()).is_none());
84 }
85 // The root's exit races a thread that starts idling: with
86 // distribution running there is no deadlock check to end that
87 // thread's wait, so it must see the program finished.
88 let (tx, rx) = std::sync::mpsc::channel();
89 std::thread::spawn(move || {
90 for _ in 0..200 {
91 assert_eq!(run_root(4, starts_and_ends), Exit::Normal);
92 }
93 let _ = tx.send(());
94 });
95 rx.recv_timeout(std::time::Duration::from_secs(60))
96 .expect("every run ends");
97}