spanquist/lib.rs
1//! A Monty Python-inspired crate to abruptly stop an application after a random amount of time.
2//! Known uses :
3//! - Validate that would-be resilient systems properly handle spuriously terminating applications.
4//! - Take the piss out of your _upper class twit of the year_ boss, colleagues and customers.
5
6extern crate rand;
7
8use std::thread;
9use std::process;
10use std::time::Duration;
11use rand::{thread_rng,Rng};
12
13/// Randomly schedule self-termination of application.
14///
15/// `spanquist!` is used by wrapping the `fn main() {}` of the target application.
16/// Random termination delay is determined upon start.
17/// Use of `spanquist!` is transparent to the application and can not be controlled at runtime.
18///
19///
20/// Upon termination the message `NOBODY EXPECTS THE SPANISH INQUISITION!` is printed to stderr.
21/// The delay for termination is currently limited to an hour but could be changed in future
22/// releases so as to keep you on your toes.
23///
24/// ```rust,no_run
25/// #[macro_use] extern crate spanquist;
26/// use std::time;
27/// use std::thread;
28///
29/// spanquist! {
30/// fn main() {
31/// loop {
32/// println!("Hello, world!");
33/// thread::sleep(time::Duration::from_millis(1000));
34/// }
35/// }}
36/// ```
37///
38/// Any resulting hilarity, chaos or straight out murder is your sole responsibility.
39#[macro_export]
40macro_rules! spanquist {
41 (fn main() $body: block) => {
42 fn main() {
43 $crate::nobody_expects();
44 { $body }
45 }}
46}
47
48#[allow(unused_must_use)]
49pub fn nobody_expects() {
50 thread::Builder::new()
51 .name("spanquist".to_string())
52 .spawn(|| the_spanish_inquisition());
53}
54
55fn the_spanish_inquisition() {
56 let delay_ms = thread_rng().gen_range(0, 3600 * 1000);
57 thread::sleep(Duration::from_millis(delay_ms));
58 eprintln!("NOBODY EXPECTS THE SPANISH INQUISITION");
59 process::exit(-1);
60}